Grep if the name exists, otherwie leave blank


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep if the name exists, otherwie leave blank
# 1  
Old 11-29-2012
Grep if the name exists, otherwie leave blank

I have two files,
file1:
Code:
name1
name2
name3
name4
name5
name6

file2
Code:
name1 group1
name3 group2
name4 group3
name6 group4
name9 group5
name10 group6
name11 group7
name12

I need a code that will match the name column in file1 with the group column in file2, if that exists, otherwise will leave it blank. In other words, I want this output:

Code:
name1 group1
name2
name3 group2
name4 group3
name5
name6

# 2  
Old 11-29-2012
Code:
awk 'NR==FNR{A[$1]=$2;next}{print $1,A[$1]}' file2 file1

OR

Code:
join file1 file2 -a 1

This User Gave Thanks to pamu For This Post:
# 3  
Old 11-29-2012
Thanks! The first code works great, the second works only in sorted files but it seems quick and useful anyways
# 4  
Old 11-29-2012
Quote:
Originally Posted by FelipeAd
Thanks! The first code works great, the second works only in sorted files but it seems quick and useful anyways
Yes i forgot to mention this.

for using join we need files to be in sorted order.Smilie

pamu
# 5  
Old 11-29-2012
Do you know how to modify your first solution if file2 has more columns?
# 6  
Old 11-30-2012
Quote:
Originally Posted by FelipeAd
Do you know how to modify your first solution if file2 has more columns?
Try

Code:
$ cat file1
name1
name2
name3
name4
name5
name6

Code:
$ cat file2
name1 group1 good
name3 group2 bad success
name4 group3 good
name6 group4 good success success
name9 group5 good
name10 group6 bad
name11 group7 good

Code:
$ awk 'NR==FNR{A[$1]=$0;next}{print A[$1]?A[$1]:$1}' file2 file1
name1 group1 good
name2
name3 group2 bad success
name4 group3 good
name5
name6 group4 good success success

I hope this helps Smilie

pamu
This User Gave Thanks to pamu For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed/grep: check if line exists, if not add line?

Hello, I'm trying to figure out how to speed up the following as I want to use multiple commands to search thousands of files. is there a way to speed things up? Example I want to search a bunch of files for a specific line, if this line already exists do nothing, if it doesn't exist add it... (4 Replies)
Discussion started by: f77hack
4 Replies

2. Shell Programming and Scripting

To check Blank Lines, Blank Records and Junk Characters in a File

Hi All Need Help I have a file with the below format (ABC.TXT) : «┐┐ABCDHEJJSJJ|XCBJSKK01|M|7348974982790 HDFLJDKJSKJ|KJALKSD02|M|7378439274898 KJHSAJKHHJJ|LJDSAJKK03|F|9898982039999 (cont......) I need to write a script where it will check for : blank lines (between rows,before... (6 Replies)
Discussion started by: chatwithsaurav
6 Replies

3. Shell Programming and Scripting

grep to remove blank space

Hi All, Need help to grep blank and copy to file. I have a file in below format dns1dm06_10, dns2dm02_04, dbidub,10000000c9a46d0c gbpuhci,10000000c948b00a ibtur001,10000000c9a1ccda yubkbtp1,10000000c93fec5b I need to copy to all lines which doesn't have wwn >> no-wwn.txt 1... (2 Replies)
Discussion started by: ranjancom2000
2 Replies

4. UNIX for Dummies Questions & Answers

Grep command to remove blank lines

The following grep command grep -v "^$" filename > newfilename does not populate the new file with any data. I see it search the entire input file but the output file never gets filled. Is this not the correct command for what Im looking to do? (2 Replies)
Discussion started by: aispg8
2 Replies

5. Shell Programming and Scripting

Insert blank line if grep not found

Hi all, I've googling around forum regarding my prob, the nearest would same as thread tittled Insert blank line if grep not found, but she/he did not mention the solution, so I would like to request your help I've this task, to search in file2 based on pattern in file1 and output it to... (4 Replies)
Discussion started by: masterpiece
4 Replies

6. Shell Programming and Scripting

Grep pattern from different file and display if it exists in the required file

Hi, I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which contains rubbish information just to fill the page which is of no use. this is... (3 Replies)
Discussion started by: abinash
3 Replies

7. UNIX for Dummies Questions & Answers

Matching exact string with blank space using grep

hi! i'm trying to get grep to do an exact match for the following pattern but..it's not quite working. I'm not too sure where did I get it wrong. any input is appreciated. echo "$VAR" | grep -q '^test:]name' if ; then printf "test name is not found \n" fi on... (4 Replies)
Discussion started by: jazzaddict
4 Replies

8. Shell Programming and Scripting

Help me! grep the log file without blank lines in column

Hi, I have log file like this: i want grep the log file without blank lines in column 4. So the file is become like this : What is the command? please help me. (1 Reply)
Discussion started by: justbow
1 Replies

9. UNIX for Dummies Questions & Answers

how to grep for blank records (space,tab,/n)?

I want to exclude (-v) blank records from a file before analysing it. I know I can use '^]$' for spaces and tabs but how do you look for lines that have nothing (/n or line feed) ? (2 Replies)
Discussion started by: Browser_ice
2 Replies

10. Shell Programming and Scripting

Insert blank line if grep not found

Hello everyone... please help if you can -- I'm stumped. Making this work will save me hours of manual labor: I need to search file2 for pattern in file1. If pattern found append file2 line to file3. If pattern not found append a blank line to file3. file1 contents example: 123 456 789... (6 Replies)
Discussion started by: michieka
6 Replies
Login or Register to Ask a Question

Featured Tech Videos