LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-12-2011, 04:53 PM   #1
srn
LQ Newbie
 
Registered: Sep 2011
Posts: 1

Rep: Reputation: Disabled
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.
 
Old 09-12-2011, 10:13 PM   #2
ramram29
Member
 
Registered: Jul 2003
Location: Miami, Florida, USA
Distribution: Debian
Posts: 848
Blog Entries: 1

Rep: Reputation: 47
paste file2.csv file1.csv | sed s/,/\ /g | awk '{print $1" "$5}'
 
Old 09-13-2011, 02:49 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Or you could just use awk:
Code:
awk -F, 'FNR == NR{names[$3]++;next}$2 in names' file1 file2 > file3
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk or sed to use CSV as input and XML as template and output to a single file bridrod Linux - Newbie 6 03-13-2012 07:00 PM
[SOLVED] Awk varying patterns to different file Tauro Linux - Newbie 6 07-29-2011 04:57 PM
search / count unique patterns in text file logicalfuzz Linux - Newbie 2 10-14-2006 07:58 AM
how to search file's content with shell? arcow Linux - Software 2 02-08-2006 10:00 AM
awk and change file content Ayman.mashal Programming 4 06-02-2005 06:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration