Visit Our UNIX and Linux User Community


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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search row by row from one file to another file if match is found print few colums of file 2
# 1  
Old 04-10-2014
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
Code:
table1
table2
table3


testfile.txt
Code:
name#place#data#select * from table1
name2#place2#data2#select * from table 10 innerjoin table3
name2#place2#data2#select * from table 10

output
Code:
name place table1
name2 place table3


i tried using awk


Code:
#! /bin/ bash 
while read line           
do  
x=`echo "$line"`
awk /'$x'/ > "file"($x~/"$line"/?2:1)}' testfile.txt 
done<list.txt

kindly help.

Last edited by vamsekumar; 04-10-2014 at 07:51 PM.. Reason: Add CODE tags.
# 2  
Old 04-10-2014
It's not clear what you're doing. First of all get rid of the shell script and just use awk.
# 3  
Old 04-11-2014
Code:
awk 'NR==FNR{a[$0]; next} {for(x in a) {if($0 ~ x) {print $1, $2, x}}}' FS='#' list.txt testfile.txt

This User Gave Thanks to SriniShoo For This Post:
# 4  
Old 04-12-2014
Partially working

Thanks sahoo but its partially working.

I need $1,$2,$3 of test file and $0/$1 of the list file.
but currently i am getting $4 of the test file in which i want to search for string but get rid of it in output.

Please help.
# 5  
Old 04-12-2014
Your example said you need $1, $2 from test file and $0 from list file and I have provided the code for the same.

If you want to print $3 as well from test file
Code:
awk 'NR==FNR{a[$0]; next} {for(x in a) {if($0 ~ x) {print $1, $2, $3, x}}}' FS='#' list.txt testfile.txt

This User Gave Thanks to SriniShoo For This Post:
# 6  
Old 04-12-2014
This should be more efficient:
Code:
awk -F"[# ]" 'NR==FNR{a[$1]=$1;next}$NF in a {print $1, $2, a[$NF]}' list.txt testfile.txt

This User Gave Thanks to Franklin52 For This Post:
# 7  
Old 04-12-2014
Hi Franklin...you are correct
But the reason why I went with this approach is - since there are select statements with joins at the end, the table we are searching for need not be the last one in query. Below is an example.
Code:
name2#place2#data2#select * from table3 innerjoin table 10


Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print every alternate column in row in a text file

Hi, I have a comma separated file. I would like to print every alternate columns into a new row. Example input file: Name : John, Age : 30, DOB : 30-Oct-2018 Example output: Name,Age,DOB John,30,30-Oct-2018 (3 Replies)
Discussion started by: Lini
3 Replies

2. Shell Programming and Scripting

Perl script to fill the entire row of Excel file with color based on pattern match

Hi All , I have to write one Perl script in which I need to read one pre-existing xls and based on pattern match for one word in some cells of the XLS , I need to fill the entire row with one color of that matched cell and write the content to another excel Please find the below stated... (2 Replies)
Discussion started by: kshitij
2 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. 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

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

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

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

Featured Tech Videos