Hi I have used many times the various methods to append two lines together in a file.
This time I want to append the 1st line to the second and repeat for the complete file.... an example
This is the file
owns the big brown dog
joe
owns the small black dog
jim
What I want is
joe owns the big brown dog
jim owns the small black dog
Thanks. Dave
# awk '{a=$0;getline;print $0,a}' file
joe owns the big brown dog
jim owns the small black dog
Thanks for your help. That is exactly what I wanted.
Can you explain what happens please.
Thanks Tytalus
Using sed
$ sed -n 'N; s/\(^.*\)\n\(.*$\)/\2 \1/p' file
joe owns the big brown dog
jim owns the small black dog
$
BMDan
5
Just for fun, pure shell solution:
i=0
while read line
do if [ $(($i%2)) == 1 ]; then
echo "$line $lastline"
else
lastline=$line
fi
let i=$i+1
done
Or:
perl -00 -pe's/(.*\n)(.*)\n/$2 $1/g' filename
Or:
while IFS= read -r y;IFS= read -r x;do
printf "%s\n" "$x $y"
done<filename
perl -ne 'if($.%2==1){$t=$_;} if($.%2==0){$_=~tr/\n//d;print $_," ";print $t; $t="";}' file
BMDan
8
perl -ne 'if($.%2){$t=$_;} if(!$.%2){chomp;print "$_ $t";}' <file
Shortened that a bit for you.