Hello,
I have a file with thousands of rows and I need to change sequence of lines.
Sample file:
#NAME
#SERVICE 112233
#DESCRIPTION AABBCCDD
#SERVICE 738292
#DESCRIPTION FFYYRRTT
...
...
...
Desired output:
#NAME
#DESCRIPTION AABBCCDD
#SERVICE 112233
#DESCRIPTION FFYYRRTT
#SERVICE 738292
...
...
...
First line of the file should remain its position.
I'd appreciate if you could lead me how I can get required output .
Kind regards
Boris
Try something like this:
awk '/#NAME/{print} /#SERVICE/{s=$0} /#DESCRIPTION/{print s; print}' file
or
sed -n '1{p;n;};h;n;G;p' file
1 Like
RudiC
June 30, 2017, 1:54pm
3
Not clear. So you have records of five lines each, and, within each record, you just want to swap line 2 with 3, and line 4 with 5?
rudic:
Not clear. So you have records of five lines each, and, within each record, you just want to swap line 2 with 3, and line 4 with 5?
Hello Scrutinizer,
Thanks, I am gonna test your code now.
Hello Rudic,
"#NAME " phrase contains only in one line. I need to replace #SERVICE line with #DESCRIPTION line.
Line 2 & 3 , 4 & 5, 6 & 7, .... , 998&999 , so on..
Kind regards
Boris
---------- Post updated at 01:08 PM ---------- Previous update was at 12:55 PM ----------
Thanks Scrutinizer,
Below code gave the output that I needed.
sed -n '1{p;n;};h;n;G;p' file
Many thanks
Boris
RudiC
June 30, 2017, 5:06pm
5
Try also
awk '!(NR%2) {T = $0; next} {printf "%s%c%s" ORS, $0, TRS, T; TRS = ORS}' file
Hello Rudic,
That's nice....
Thank You Scrutinizer & Rudic,
Both answers gave what I needed...
kind regards
Boris