tags:

views:

56

answers:

3

How would i join two lines usung awk or sed?

for eg:

i have data like below:

abcd
joinabcd
efgh
joinefgh
ijkl
joinijkl

i need oupput like below:

joinabcdabcd
joinefghefgh
joinijklijkl
A: 
awk 'BEGIN{i=1}{line[i++]=$0}END{j=1; while (j<i) {print line[j+1] line[j]; j+=2}}' yourfile

No need for sed.

bashflyng
Good answer....but i suppose i asked for complete line not just the first field.so now it need to be changed as `line[i++]=$0` instead of `line[i++]=$1`.But a very good answer thanks:)
Vijay Sarathi
+1  A: 

Here it is in sed:

sed 'h;s/.*//;N;G;s/\n//g' < filename
Beta
The redirection is unnecessary.
Dennis Williamson
@Dennis Williamson: you're right, it's force of habit.
Beta
+4  A: 
awk '!(NR%2){print$0p}{p=$0}' infile
radoulov