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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read row number from 1 file and print that row of second file
# 1  
Old 02-28-2014
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
Code:
1
3
5
7
9

file2
Code:
11111
22222
33333
44444
55555
66666
77777
88888
99999

output should contain

Code:
11111
33333
55555
77777
99999

i want to do this on files having millions of records. This is just the scenario. i am trying using awk but unable to do.
Moderator's Comments:
Mod Comment Please use CODE tags to mark all sample code, sample input, and sample output.

Last edited by Don Cragun; 02-28-2014 at 04:53 AM.. Reason: Add CODE tags.
# 2  
Old 02-28-2014
Are the line numbers in file1 always in sorted order as they are in your sample?
# 3  
Old 02-28-2014
yes. They are in sorted order. i want output like this
Code:
line1:11111
line3:33333
line5:55555
line7:77777
line9:99999

I am trying the following code but it is partly correct
Code:
awk 'NR==FNR{a[$0]++}FNR in a {print "line"FNR":"$0}' file1 file2

i get the following output
Code:
line1:1
line3:5
line5:9
line1:11111
line3:33333
line5:55555
line7:77777
line9:99999

# 4  
Old 02-28-2014
Quote:
Originally Posted by Abhiraj Singh
yes. They are in sorted order. i want output like this
Code:
line1:11111
line3:33333
line5:55555
line7:77777
line9:99999

I am trying the following code but it is partly correct
Code:
awk 'NR==FNR{a[$0]++}FNR in a {print "line"FNR":"$0}' file1 file2

i get the following output
Code:
line1:1
line3:5
line5:9
line1:11111
line3:33333
line5:55555
line7:77777
line9:99999

You're very close. Try:
Code:
awk 'NR==FNR{a[$0]++;next}FNR in a {print "line"FNR":"$0}' file1 file2

Note that the "++" slows down the process, but doesn't change the results. You should get the same output with:
Code:
awk 'NR==FNR{a[$0];next}FNR in a{print "line"FNR":"$0}' file1 file2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print row on 4th column to all row

Dear All, I have input : SEG901 5173 9005 5740 SEG902 5227 5284 SEG903 5284 5346 SEG904 5346 9010 SEG905 5400 5456 SEG906 5456 5511 SEG907 5511 9011 SEG908 5572 9015 SEG909 5622 9020 SEG910 5678 5739 SEG911 5739 5796 SEG912 5796 9025 ... (3 Replies)
Discussion started by: attila
3 Replies

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

3. Shell Programming and Scripting

Get row number from file1 and print that row of file2

Hi. How can we print those rows of file2 which are mentioned in file1. first character of file1 is a row number.. for eg file1 1:abc 3:ghi 6:pqr file2 a abc b def c ghi d jkl e mno f pqr ... (6 Replies)
Discussion started by: Abhiraj Singh
6 Replies

4. UNIX for Dummies Questions & Answers

Finding row number along with length of row

I have a fixed length file and I want to find out row number along with row length. I have a program that give me the line length if it satisfy the condition; but i would like to add row number as well? How do I do that? while IFS= read -r line; do if ; then echo ${line} echo... (8 Replies)
Discussion started by: princetd001
8 Replies

5. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

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

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

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

9. UNIX for Dummies Questions & Answers

How to read and write a random row from a file?

Lets say I have a file abc.txt and it has about 35 million rows. I would like to take a sample of 100 random rows from that file for my testing purpose and write it to a file say test.txt. How do I do this operation? Thanks, Sashank (9 Replies)
Discussion started by: sashankkrk
9 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