Matching and appending


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching and appending
# 1  
Old 05-29-2012
Matching and appending

I have two files

FILEA
Code:
a/b/c/d
e/f/g/h
i/j/k/l
m/n/o/p

FILEB
Code:
i/j/k/l

i want that if lines of FILEB matches lines of FILEA then matching lines in FILEA to be appended by word "MATCH"

NEW FILEA
Code:
a/b/c/d
e/f/g/h
i/j/k/l "MATCH"
m/n/o/p

I have tried following script .but not getting desired result.Please Help
Code:
for i in `cat FILEB`
do
  if [ `grep "$i" FILEA ]; then
    `sed '/$i/ s/$/ "MATCH" FILEA`
  fi
done

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 05-29-2012 at 03:20 AM.. Reason: Please use code tags
# 2  
Old 05-29-2012
Try:
Code:
awk 'NR==FNR{a[$0];next}$0 in a{$0=$0 " MATCH"}1' FILEB FILEA > NEWFILEA

# 3  
Old 05-29-2012
Code:
# awk 'NR==FNR{a[x++]=$0}END{for(i=0;i<x;i++)if($0==a[i])print a[i]FS"\"MATCH\"";else print a[i]}' FILEA FILEB
a/b/c/d
e/f/g/h
i/j/k/l "MATCH"
m/n/o/p

# 4  
Old 05-29-2012
Quote:
Originally Posted by ygemici
Code:
# awk 'NR==FNR{a[x++]=$0}END{for(i=0;i<x;i++)if($0==a[i])print a[i]FS"\"MATCH\"";else print a[i]}' FILEA FILEB
a/b/c/d
e/f/g/h
i/j/k/l "MATCH"
m/n/o/p

---------- Post updated at 12:50 PM ---------- Previous update was at 12:48 PM ----------

Tried it.It is not generating any error message.But also not generating desired output
# 5  
Old 05-29-2012
How about this ?
Code:
 awk 'NR==FNR{a[$0]++;next} a[$0]{$0=$0 FS "\"MATCH\""}1' FileB FileA

# 6  
Old 05-29-2012
Yet another one:
Code:
awk 'NR==FNR{A[$0]="\"MATCH\""; next}{print $0,A[$0]}' FileB FileA


--
On Solaris use /usr/xpg4/bin/awk rather than awk
# 7  
Old 05-29-2012
Thanks to all

Code:
/usr/xpg4/bin/awk  'NR==FNR{A[$0]="\"MATCH\""; next}{print $0,A[$0]}' FileB FileA

Working.

Last edited by Scrutinizer; 05-29-2012 at 06:55 AM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine all matching dates and remove non-matching

Using the awk below I am able to combine all the matching dates in $1, but I can not seem to remove the non-matching from the file. Thank you :). file 20161109104500.0+0000,x,5631 20161109104500.0+0000,y,2 20161109104500.0+0000,z,2 20161109104500.0+0000,a,4117... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Appending to file with new ID

I have a file that looks like this: 1|A 2|B 3|C ... ... 100|A I would like to take the last line in the file and add +1 to the number so the output looks like this 1|A (4 Replies)
Discussion started by: BeefStu
4 Replies

3. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

4. Shell Programming and Scripting

Insert lines above matching line with content from matching

Hi, I have text file: Name: xyz Gender: M Address: "120_B_C; ksilskdj; lsudlfw" Zip: 20392 Name: KLM Gender: F Address: "65_D_F; wnmlsi;lsuod;,...." Zip:90233I want to insert 2 new lines before the 'Address: ' line deriving value from this Address line value The Address value in quotes... (1 Reply)
Discussion started by: ysrini
1 Replies

5. Shell Programming and Scripting

awk: matching and not matching

Hello all, simple matching and if not match problem that i can't figure out. file1 hostname: 30 10 * * * /home/toto/start PROD instance_name1 -p 00 9 * * * /home/toto/start PROD instance_name2 -p 15 8 * * * /home/toto/start PROD instance_name3 -p hostname2: 00 8 * * *... (5 Replies)
Discussion started by: maverick72
5 Replies

6. Shell Programming and Scripting

appending with AWK

Hi all, I have two files called INPUT1 and INPUT2 and i am trying to append the values of INPUT1 to INPUT2 using AWK. The INPUT1 file will always has a single field and the INPUT2 file has 3 fields but both files will have same number of records always. Following is the format of the... (5 Replies)
Discussion started by: harris2107
5 Replies

7. UNIX for Dummies Questions & Answers

Appending the two files

hi, I want to append to two files into a third file without new line like this: file 1: I am learning the unix file 2: Unix is very intersting When I am trying cat file1 file2 >> file3 I am getting: I am learning the unix Unix is very interesting But I want that to be in... (3 Replies)
Discussion started by: harish409
3 Replies

8. Shell Programming and Scripting

Appending to a variable?

Hey, I'm creating a custom useradd script, and I'm giving the option to add secondary groups. Basically what I want to do is ask for the name of the group, you type in the group you want to add, it assigns that group name to the variable $sgroup. Then the scripts asks if you want add another. If... (0 Replies)
Discussion started by: paqman
0 Replies

9. Shell Programming and Scripting

appending

hi, a1.txt ------ xyz zxc xya xdf a2.txt ------ a1 a2 b1 b2 cat a1.txt >> a2.txt -->its appending at the end (6 Replies)
Discussion started by: mohan705
6 Replies

10. Shell Programming and Scripting

appending

i've been fiddling around with append and I know how to append a word into a file, but how would you go about appending a word to a filename? (5 Replies)
Discussion started by: codewarrior7
5 Replies
Login or Register to Ask a Question