How to change row by order nr?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to change row by order nr?
# 1  
Old 06-30-2017
How to change row by order nr?

Hello,
I have a file with thousands of rows and I need to change sequence of lines.

Sample file:

Code:
#NAME
#SERVICE 112233
#DESCRIPTION AABBCCDD
#SERVICE 738292
#DESCRIPTION FFYYRRTT
...
...
...

Desired output:

Code:
#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

Last edited by baris35; 06-30-2017 at 03:31 PM.. Reason: [sorted]
# 2  
Old 06-30-2017
Try something like this:
Code:
awk '/#NAME/{print} /#SERVICE/{s=$0} /#DESCRIPTION/{print s; print}' file

or

Code:
sed -n '1{p;n;};h;n;G;p' file


Last edited by Scrutinizer; 06-30-2017 at 02:59 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 06-30-2017
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?
# 4  
Old 06-30-2017
Quote:
Originally Posted by 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 ----------

Quote:
Originally Posted by Scrutinizer
Try something like this:
Code:
awk '/#NAME/{print} /#SERVICE/{s=$0} /#DESCRIPTION/{print s; print}' file

or

Code:
sed -n '1{p;n;};h;n;G;p' file

Thanks Scrutinizer,

Below code gave the output that I needed.
Code:
sed -n '1{p;n;};h;n;G;p' file

Many thanks
Boris

Last edited by baris35; 06-30-2017 at 03:07 PM..
# 5  
Old 06-30-2017
Try also
Code:
awk '!(NR%2) {T = $0; next} {printf "%s%c%s" ORS, $0, TRS, T; TRS = ORS}' file

# 6  
Old 07-01-2017
Hello Rudic,
That's nice....


Thank You Scrutinizer & Rudic,
Both answers gave what I needed...

kind regards
Boris
Quote:
Originally Posted by RudiC
Try also
Code:
awk '!(NR%2) {T = $0; next} {printf "%s%c%s" ORS, $0, TRS, T; TRS = ORS}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Manipulation row order in file

Hello, I am trying to replace the position of each row by the next row. OS: Ubuntu 18.04, bionic I'd appreciate your help. input_file: -O fileA wget http://x.y.z./a -O fileB wget http://a.b.c./d -O fileC wget http://q.f.s/t .. .. .. -O fileZZ wget http://r.t.y/u I expect: (6 Replies)
Discussion started by: baris35
6 Replies

2. Shell Programming and Scripting

Change of fields order inline

Hi everyone, What is the best solution to check every line in the xml file and change order of found field along with its value without touching value. Pattern will be given i.e. one line can look like this one: <widget position="value,value" size="value,value" name="value"... (5 Replies)
Discussion started by: TiedCone
5 Replies

3. Shell Programming and Scripting

File in ascending order by row

Hi All I have a file like this: ID1 ref_A 10 ref_B 30 ref_C 5 ID2 ref_F 69 ref_G 12 ref_H 5 Every ID is followed by a string(ref_X) followed by a number(every number is referred to the previous ref) I would like to order the file like this(the column could be more, but always with the same... (4 Replies)
Discussion started by: giuliangiuseppe
4 Replies

4. Shell Programming and Scripting

awk - Why does output order change?

I have this END section in an awk script: END { for (j in littlebin) { print (j, int(log(j)/log(2)) , littlebin) lbsum+=littlebin } for (i in bins) ... (6 Replies)
Discussion started by: treesloth
6 Replies

5. UNIX for Dummies Questions & Answers

change service order

hi guys I have a service that depends on some shares (NFS shares ) that need to be mounted before before the service start so the service-app finds the NFS shares and starts correctly... I am confused here this is what I found but I am not sure what to do in order to change it BTW is Suse... (2 Replies)
Discussion started by: karlochacon
2 Replies

6. Shell Programming and Scripting

change order

I have inside a file 22 25 80 111 631 694 861 875 I need this in the form using awk or sed 22,25,80,111,631,694,861,875 (4 Replies)
Discussion started by: anil510
4 Replies

7. UNIX for Dummies Questions & Answers

Column to row - in order to create zones

i need a script that reads a wwn file like this: LOCAL_HOST (before) 50:01:24:82:00:6c:a9:04 21:00:00:24:ff:30:35:7b 21:00:00:24:ff:30:35:70and will run over the file with all the wwn's in the same row in order to create zones on a switch like so: LOCAL_HOST (after) zonecreate " (... (2 Replies)
Discussion started by: boaz733
2 Replies

8. Shell Programming and Scripting

Change order

Good evening I have a file as below and want to change the order, as in the second column, sed awk Pearl Thanks aaaaaaaaaa bbbbbbbbb cccccccc aaaaaaaaaa bbbbbbbbb cccccccc aaaaaaaaaa cccccccc bbbbbbbbb aaaaaaaaaa cccccccc bbbbbbbbb (8 Replies)
Discussion started by: Novice-
8 Replies

9. Shell Programming and Scripting

Change many columns position/order

Hi everyone, Please some help over here. (I´m using cygwing) I have files with 40 columns and 2000 lines in average. I´m trying to change the order position as follow. Original columns position:... (3 Replies)
Discussion started by: cgkmal
3 Replies

10. UNIX for Dummies Questions & Answers

How to change the order of a string ?

Hi , I want to change the order of a string using sed command . Is it possible ? $echo "abc123xyz" | sed 's/\()*\) \(*\)/\2\1/' abc123xyz $ echo "abc123xyz" |sed 's/\()*\) \(*\) \()*\)/\2\1\3/' abc123xyz I want to change the string , abc123xyz as xyz123abc . Is it... (5 Replies)
Discussion started by: rajavu
5 Replies
Login or Register to Ask a Question