LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using file content as input for awk search patterns (https://www.linuxquestions.org/questions/programming-9/using-file-content-as-input-for-awk-search-patterns-902652/)

srn 09-12-2011 04:53 PM

Using file content as input for awk search patterns
 
HI,

I'm having trouble constructing a shell script using the awk command
to solve my problem.

Problem:
I have two csv-files which look something like the following

A small file (file1.csv)
Code:

$ head -2 file1.csv
"data13a","data13b","name13"
"data21a","data21b","name21"

And a large one (file2.csv)
Code:

$ head -3 file2.csv
"username1","name1"
"username2","name2"
"username3","name3"

and so on

file1.csv contains information about a group of users
file2.csv contains information about all users

Does anyone know howto create a new file (file3.csv) containing a list of usernames and names of the persons listed in file1.csv like this?

Code:

$ head -2 file3.csv
"username13","name13"
"username21","name21"

I'm thinking about using awk to find the right lines in file2.csv and print them to file3.csv, but I have some trouble using fields from the lines in file1.csv as an awk search pattern.

Can anybody offer me some help?
Thanks.

ramram29 09-12-2011 10:13 PM

paste file2.csv file1.csv | sed s/,/\ /g | awk '{print $1" "$5}'

grail 09-13-2011 02:49 AM

Or you could just use awk:
Code:

awk -F, 'FNR == NR{names[$3]++;next}$2 in names' file1 file2 > file3


All times are GMT -5. The time now is 01:20 AM.