Hello community,
what I need to do is read 2 rows at time from a file. I have this simple solution:
File to read:
LINE1
LINE2
LINE3
LINE4
LINE5
LINE6
LINE7
LINE8
Read routine:
#!/bin/ksh
sed '1,3d' /out.txt | while read line; do
read line2
echo $line $line2
done
Result:
LINE1 LINE2
LINE3 LINE4
LINE5 LINE6
LINE7 LINE8
That's ok for me, but, just to be sure, do you have any other advice? Or any other faster methods to do that?
Thanks
Lucas
paste -sd' \n' file
Hey, sorry for not paying attention to those blank lines.
Hi
$ sed '1,3d;N;s/\n/ /' file
LINE1 LINE2
LINE3 LINE4
LINE5 LINE6
LINE7 LINE8
Guru.
Wait, wait, wait!
I forgot to mention that variables $line and $line2 should be used for create other output file inside the loop.
Something like:
#!/bin/ksh
sed '1,3d' /out.txt | while read line; do
read line2
echo "ABC ; DEF ; $line ; GHI ; $line2 ; ZZZ" >> finaloutput.txt
echo "XXX ; YYY ; $line ; UUU ; $line2 ; PPP" >> finaloutput2.txt
done
Though this looks a bit intimidating, it might be faster than the loop:
sed -n '${s/$/\
/;b proc
}
/^[ \t]*$/d;N
:proc
h;
s/\n/ ; GHI ; /
s/^/ABC ; DEF ; /
s/$/ ; ZZZ/
w finaloutput.txt
g
s/\n/ ; UUU ; /
s/^/XXX ; YYY ; /
s/$/ ; PPP/
w finaloutput2.txt' /out.txt
awk 'NF>0{x=$0;getline y;print x FS y}' filename