Manipulation row order in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Manipulation row order in file
# 1  
Old 09-19-2018
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:
Code:
-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:
Code:
wget http://x.y.z./a
-O fileA
wget http://a.b.c./d
-O fileB
wget http://q.f.s/t
-O fileC
..
..
..
wget http://r.t.y/u
-O fileZZ

or

Code:
wget http://x.y.z./a -O fileA
wget http://a.b.c./d -O fileB
wget http://q.f.s/t -O fileC
..
..
..
wget http://r.t.y/u -O FileZZ

I tried different combinations of below command but not as expected.

Code:
sed -n '{p;n;};h;p;n;p' input_file

many thanks
Boris

------ Post updated at 06:13 AM ------

Weird method but okay for me:

Code:
awk 'NR % 2 == 1' input_file > odd_lines
awk 'NR % 2 == 0' input_file > even_lines
paste -d'\n' even_lines odd_lines > expected_output

many thanks
Boris

Last edited by baris35; 09-19-2018 at 11:42 AM..
# 2  
Old 09-19-2018
Would this come close?
Code:
sed -n 'h;n;G;p' file
wget http://x.y.z./a
-O fileA
wget http://a.b.c./d
-O fileB
wget http://q.f.s/t
-O fileC
wget http://r.t.y/u
-O fileZZ

This User Gave Thanks to RudiC For This Post:
# 3  
Old 09-19-2018
Dear Rudic,
Could you please teach me how to catch the fish.
I looked up sed -help before opening this thread, but there was no info regarding n and p and other letters.
I'd appreciate if you would let us know how to find more info regarding functions of this notations

many thanks
Boris
# 4  
Old 09-19-2018
My wisdom comes from the man pages: man sed, plus, infrequently, info sed.
And, DON'T forget, regular consumption of these fora! Many a trick I learned in here. Above little one copies a line into the hold buffer (h), reads the next line (n), appends from the hold buffer (G), and prints (p). It depends on the lines always coming in pairs.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 09-19-2018
Dear Rudic,
It gives expected output. learnt that --help and man have not the same concept.

Many thanks
Boris
# 6  
Old 09-19-2018
If, instead of switching every pair of lines in a file, you wanted to switch every line containing the string wget with the line preceding it, you could try:
Code:
ed -s input_file <<-EOF
	g/wget/ .-1m.
	,p
	Q
EOF

And, if after switching the order of those pairs of lines you also want to join them, you could try:
Code:
ed -s input_file <<-EOF
	g/wget/ .-1m.\\
		s/^/ /\\
		.-1,.j
	,p
	Q
EOF

or, if as in this example, you don't need to perform any parameter expansions in the here-document:
Code:
ed -s input_file <<-"EOF"
	g/wget/ .-1m.\
		s/^/ /\
		.-1,.j
	,p
	Q
EOF

This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 09-19-2018
one awk:
Code:
awk 'FNR%2{f=$0;next}{print $0, f}' myFile

This User Gave Thanks to vgersh99 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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: #NAME #SERVICE 112233 #DESCRIPTION AABBCCDD #SERVICE 738292 #DESCRIPTION FFYYRRTT ... ... ... Desired output: #NAME (5 Replies)
Discussion started by: baris35
5 Replies

2. 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

3. Shell Programming and Scripting

Search row by row from one file to another file if match is found print few colums of file 2

this is the requirement list.txt table1 table2 table3 testfile.txt name#place#data#select * from table1 name2#place2#data2#select * from table 10 innerjoin table3 name2#place2#data2#select * from table 10 output name place table1 name2 place table3 i tried using awk (7 Replies)
Discussion started by: vamsekumar
7 Replies

4. Shell Programming and Scripting

Read row number from 1 file and print that row of second file

Hi. How can I read row number from one file and print that corresponding record present at that row in another file. eg file1 1 3 5 7 9 file2 11111 22222 33333 44444 55555 66666 77777 88888 99999 (3 Replies)
Discussion started by: Abhiraj Singh
3 Replies

5. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

6. 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

7. Shell Programming and Scripting

String manipulation row by row

Dear masters, I stuck again in a very tricky situation,so need your valuable inputs. I have a file having rows as below: _Db _Database 1023 1 1 1 17.0B 0.2 1.0 _Field _Field-Name 3 2 2 11 56.2K 64.1 ... (5 Replies)
Discussion started by: patric2326
5 Replies

8. Shell Programming and Scripting

Get a group of line from different file and put them into one file row by row

hi guys, today i'm stuck in a new problem. the title explain more or less but a particular had been omitted. So i'm going to describe below the situation with an example. I have different html files and each of them have a consecutive lines group inside that i want to extract. example: ... (8 Replies)
Discussion started by: sbobotex
8 Replies

9. Shell Programming and Scripting

Append Second Row to First Row @ end in a file

Hi I want to append line 2n to 2n-1 line where n=1...LastLine in my file. eg: Actual File: Hello Everyone Welcome TO Unix I need Your help Required File: HelloEveryone WelcomeTO Unix I needYour help (5 Replies)
Discussion started by: dashing201
5 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question