Get row number from file1 and print that row of file2


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get row number from file1 and print that row of file2
# 1  
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
Code:
1:abc          
3:ghi          
6:pqr

file2
Code:
a abc
b def
c ghi
d jkl
e mno
f pqr


output should contain

Code:
1: a abc
3: c ghi
6: f pqr

Please help me with this case. i believe it can be done using awk

Last edited by bartus11; 02-27-2014 at 02:25 PM.. Reason: Please use [code][/code] tags.
# 2  
Try:
Code:
awk -F"[ :]" 'NR==FNR{a[$2]=1;next}$2 in a{print a[$2]": "$0}' file1 file2

# 3  
Approach with join if the original file has the same format. This omits the : though.

Code:
sed 's/:/ /' file2 | join -1 2 -2 2 -o 1.1,2.1,2.2 - file2

--ahamed
# 4  
but this limits to this fine only.. i want to read from a file and then do operations in another file

---------- Post updated at 02:06 PM ---------- Previous update was at 02:05 PM ----------

@bartus11
i get following output with your code

1: a
1: b
1: c
1: d
1: e
1: f
1: g
1: h
1:
# 5  
You mean you have one file as reference file1 and then based on the data in file1 you need to do the operations on other multiple files?

--ahamed
# 6  
i have many pairs of file1 and file2 each containing different values.. i want a solution so that based on values in file1 i get corresponding rows in file2
# 7  
Try this...

Code:
awk -F'[: ]' 'NR==FNR{a[$2]=$1;next} $2 in a{ print a[$2]":" $0 }' file1 file2

And identify the pair of files and pass it to this command thru some loop.

--ahamed

---------- Post updated at 11:15 AM ---------- Previous update was at 11:13 AM ----------

Which is your OS? If solaris use nawk.
bartus11 code works for me with a minor modification, he missed a $ by mistake. Infact, my code is same as his.

Code:
awk -F"[ :]" 'NR==FNR{a[$2]=$1;next}$2 in a{print a[$2]": "$0}' file1 file2

--ahamed
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #164
Difficulty: Easy
Developers began to abandon MINIX after free and open-source Unix-like operating systems such as Linux and 386BSD became available in the early 1990s.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print a row with the max number in a column

Hello, I have this table: chr1_16857_17742 - chr1 17369 17436 "ENST00000619216.1"; "MIR6859-1"; - 67 chr1_16857_17742 - chr1 14404 29570 "ENST00000488147.1"; "WASH7P"; - 885 chr1_16857_18061 - chr1 ... (5 Replies)
Discussion started by: coppuca
5 Replies

2. Shell Programming and Scripting

Bash script to print the smallest floating point number in a row that is not 0

Hello, I have often found bash to be difficult when it comes to floating point numbers. I have data with rows of tab delimited floating point numbers. I need to find the smallest number in each row that is not 0.0. Numbers can be negative and they do not come in any particular order for a given... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

3. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

This is a question that is related to one I had last August when I was trying to sort/merge two files by millsecond time column (in this case column 6). The script (below) that helped me last august by RudiC solved the puzzle of sorting/merging two files by time, except it gets lost when the... (0 Replies)
Discussion started by: aachave1
0 Replies

4. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

My original files are like this below and I distinguish them from the AP_ID (file1 has 572 and file2 has 544). Also, the header on file1 has “G_” pre-pended. NOTE: these are only snippets of very large files and much of the data is not present here. Original File 1: ... (36 Replies)
Discussion started by: aachave1
36 Replies

5. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

6. Shell Programming and Scripting

Reading and appending a row from file1 to file2 using awk or sed

Hi, I wanted to add each row of file2.txt to entire length of file1.txt given the sample data below and save it as new file. Any idea how to efficiently do it. Thank you for any help. input file file1.txt file2.txt 140 30 200006 141 32 140 32 200006 142 33 140 35 200006 142... (5 Replies)
Discussion started by: ida1215
5 Replies

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

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

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

10. Shell Programming and Scripting

How to print column based on row number

Hi, I want to print column value based on row number say multiple of 8. Input file: line 1 67 34 line 2 45 57 . . . . . . line 8 12 46 . . . . . . line 16 24 90 . . . . . . line 24 49 67 Output 46 90 67 (2 Replies)
Discussion started by: Surabhi_so_mh
2 Replies

Featured Tech Videos