Skip to main content
Rollback to Revision 1
Source Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 117

If always the second lines should be joined to previous lines (based on the given input by you regardless of the start character):

awk '{ printf "%s", $0(NR%2?"":ORS) }' infile

or otherwise using below command instead for general solution and to control the all of those correctly while all other existing answers fails on that:

awk '{ printf "%s", (/^@/?(NR>1?ORS:""):"")$0 } END{ print "" }' infile

Sample input:

iiii
@A
bbb
xyz
@B
bbb
@D
ccc

Output:

iiii
@Abbbxyz
@Bbbb
@Dccc

If always the second lines should be joined to previous lines (based on the given input by you regardless of the start character):

awk '{ printf "%s", $0(NR%2?"":ORS) }' infile

or otherwise using below command instead for general solution and to control the all of those correctly while all other existing answers fails on that:

awk '{ printf "%s", (/^@/?(NR>1?ORS:""):"")$0 } END{ print "" }' infile

Sample input:

iiii
@A
bbb
xyz
@B
bbb
@D
ccc

Output:

iiii
@Abbbxyz
@Bbbb
@Dccc
awk '{ printf "%s", (/^@/?(NR>1?ORS:""):"")$0 } END{ print "" }' infile

Sample input:

iiii
@A
bbb
xyz
@B
bbb
@D
ccc

Output:

iiii
@Abbbxyz
@Bbbb
@Dccc
added 263 characters in body
Source Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 117

If always the second lines should be joined to previous lines (based on the given input by you regardless of the start character):

awk '{ printf "%s", $0(NR%2?"":ORS) }' infile

or otherwise using below command instead for general solution and to control the all of those correctly while all other existing answers fails on that:

awk '{ printf "%s", (/^@/?(NR>1?ORS:""):"")$0 } END{ print "" }' infile

Sample input:

iiii
@A
bbb
xyz
@B
bbb
@D
ccc

Output:

iiii
@Abbbxyz
@Bbbb
@Dccc
awk '{ printf "%s", (/^@/?(NR>1?ORS:""):"")$0 } END{ print "" }' infile

Sample input:

iiii
@A
bbb
xyz
@B
bbb
@D
ccc

Output:

iiii
@Abbbxyz
@Bbbb
@Dccc

If always the second lines should be joined to previous lines (based on the given input by you regardless of the start character):

awk '{ printf "%s", $0(NR%2?"":ORS) }' infile

or otherwise using below command instead for general solution and to control the all of those correctly while all other existing answers fails on that:

awk '{ printf "%s", (/^@/?(NR>1?ORS:""):"")$0 } END{ print "" }' infile

Sample input:

iiii
@A
bbb
xyz
@B
bbb
@D
ccc

Output:

iiii
@Abbbxyz
@Bbbb
@Dccc
Source Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 117

awk '{ printf "%s", (/^@/?(NR>1?ORS:""):"")$0 } END{ print "" }' infile

Sample input:

iiii
@A
bbb
xyz
@B
bbb
@D
ccc

Output:

iiii
@Abbbxyz
@Bbbb
@Dccc