how to fgrep -f two files and get only one instance of each matched line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to fgrep -f two files and get only one instance of each matched line
# 8  
Old 11-17-2011
I get the same thing

---------- Post updated at 04:24 AM ---------- Previous update was at 04:18 AM ----------

if anyone is interested,
a combination of jayan jay's solution and rdcwayx's solutions works:
Code:
 
nawk -F"[=,]" 'NR==FNR{a[$1];next} $4 in a{print;delete a[$4]}' file1 file2

# 9  
Old 11-17-2011
deleted the content ..

Last edited by jayan_jay; 11-17-2011 at 05:27 AM.. Reason: just saw the update
# 10  
Old 11-17-2011
hey jayan jay can you explain to me why field $4?

---------- Post updated at 04:31 AM ---------- Previous update was at 04:25 AM ----------

jayan jay,

all possible combinations:

Code:
 
> fgrep -f not_upgraded_sites.txt not_upgraded.sel  | nawk -F"[=,]" "!x[$4]++"
x[: Event not found.
> fgrep -f not_upgraded_sites.txt not_upgraded.sel  | nawk -F'[=,]' "!x[$4]++"
x[: Event not found.
> fgrep -f not_upgraded_sites.txt not_upgraded.sel | nawk -F'[=,]' '!x[$4]++'
x[: Event not found.
>

# 11  
Old 11-17-2011
Hope it clears .. Smilie
Code:
$ echo "SubNetwork=XXX,MeContext=GG123,Element=1" | nawk -F'[=,]' '{print $4}'
GG123

Try this combination also .. Smilie
Code:
$ fgrep -f not_upgraded_sites.txt not_upgraded.sel | nawk -F"[=,]" '!x[$4]++'

This User Gave Thanks to jayan_jay For This Post:
# 12  
Old 11-17-2011
Code:
 
file 1
FC1
FC3
FC4
FC2
FC5

actual file2:
Code:
 
Network=ONR,Context=FC6,Element=1
Network=ONR,Context=FC7,Element=1
Network=ONR,Context=FC0,Element=1
Network=ONR,Context=FC1
Network=ONR,Context=FC1,Element=1
Network=ONR,Context=FC2,Element=1
Network=ONR,Context=FC0,Element=1
Network=ONR,Context=FC6,Element=1

# 13  
Old 11-17-2011
Quote:
Originally Posted by smarones
Hi rdcwayx,
I think your solution is what I need however my lines are a bit more complicated than the example I gave and your solution is based on that which is my fault. But I was looking for something general and it seems like there is not something you can apply to everycase.

the expressions I want is more like this:
Code:
 
Network=XXX,Context=GG123,Element=1

and I want what ever comes after the second equal sign before the last comma, in this case would be GG123.


Hi funksen,

I have thousands of lines not sure if while would be a good idea as you mentioned.
Ok, my understand, your file1 will be something like:

Code:
GG123
GG124
XX123

file2
Code:
Network=XXX,Context=GG123,Element=1
Network=XXX,Context=GG123,Element=2
Network=XXX,Context=XX123,Element=1
Network=XXX,Context=GG124,Element=1
Network=XXX,Context=GG123,Element=1

Code:
awk -F , 'NR==FNR{a[$1];next} {split($2,b,"=");if (b[2] in a){print;delete a[b[2]]}}' file1 file2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

2. Shell Programming and Scripting

awk line instance counter

I Have a text file with several thousand lines of text. Occasionally there will be a "sysAlive" line of text (every so often) What would be an awk command to print every line of text, and to put in incrementing counter ONLY on the "sysAlive" lines For example: >cat file.txt lineAAA a b c d... (4 Replies)
Discussion started by: ajp7701
4 Replies

3. Shell Programming and Scripting

Help required on joining one line above & below to the pattern matched string line.

Hi Experts, Help needed on joining one line above & below to the pattern matched string line. The input file, required output is mentioned below Input file ABCD DEFG5 42.0.1-63.38.31 KKKK iokl IP Connection Available ABCD DEFG5 42.0.1-63.38.31 ... (7 Replies)
Discussion started by: krao
7 Replies

4. Shell Programming and Scripting

Using sed can you specify the last instance of a character on a line?

I was told a way to do this with awk earlier today but is there a way with sed to specify the last instance of a character on a line? You will know what character you're looking for but there could be none or one hundred instances of it on a line say and you ONLY want to specify the last one for... (3 Replies)
Discussion started by: Bashingaway
3 Replies

5. Shell Programming and Scripting

Replace all but skip first instance in a line

I have a record like the one given below. 010000306551~IN ~N~ |WINDWARD PK|Alpharetta| If ~ is present more than instance in a line,then I need to delete those instances. Any ideas? I am working in Solaris (7 Replies)
Discussion started by: prasperl
7 Replies

6. Shell Programming and Scripting

awk get matched line's previous line

hi everyone, a b in c d e f in g output is: a e so awk search for "in", then print out the matched line's previuos line. Please advice. (11 Replies)
Discussion started by: jimmy_y
11 Replies

7. Shell Programming and Scripting

Append instance count to each line

Hello forum, I need help with a script for displaying the number of instances/times a particular line appears in a tab-delimited file and append that number to the end of the line. Example text file: aaa bbb ccc ddd ggg hhh kkk nnn aaa bbb ccc ddd aaa bbb ccc ddd ppp qqq nnn sss ggg... (1 Reply)
Discussion started by: jaysean
1 Replies

8. Shell Programming and Scripting

awk script to move a line after the matched pattern line

I have the following text format in a file which lists the question first and then 5 choices after that the explanantion and finally the answer. 1.The amount of time it takes for most of a worker’s occupational knowledge and skills to become obsolete has been declining because of the... (2 Replies)
Discussion started by: nanchil_guy
2 Replies

9. Shell Programming and Scripting

replace first instance(not first instance in line)

Alright, I think I know what I am doing with sed(which probably means I don't). But I cant figure out how to replace just the first occurance of a string. I have tried sed, ed, and grep but can't seem to figure it out. If you have any suggestions I am open to anything! (3 Replies)
Discussion started by: IronHorse7
3 Replies

10. Shell Programming and Scripting

How to get a next line of a matched word?

Hi , If I match a word in a file, I want to pick the next line of that matched word. My file is a below format- The ntrag trace has auditError Line5005 contains transaction Ntrag data ------------ Here I wanted if I match a word auditError, I need to get the next line "Line5005... (10 Replies)
Discussion started by: Muktesh
10 Replies
Login or Register to Ask a Question