Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk "if not exists in (cat filename)" 2

Status
Not open for further replies.

theo67

Technical User
Jul 17, 2008
31
DE
Hello,
i don't know if i am blaming me with my question, but i have the following "problem".
File one looks like:
gdsfa9078470840 uiupqepquwerop8973070780570
öjllsj1790751785901oweuopoop8ß1908ß182ß3488...

file two is a list with numbers.
998
678
334 ...

What i need to do is, to check every line in file one, if on Position 10-12, there is one of the numbers, listed in file 2. Then this line should be ignored.
The rest of the lines schould be wrinten in a new file output.txt


Maybe i am trying it with the wrong tool i don't know...

I would apreciate if someone could give me an advice..

Thanks..
 
A starting point:
Code:
awk 'NR==FNR{++n[$1];next]!(substr($0,10,3) in n)' filetwo fileone >output.txt

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Great, you have your answer. To keep your mind opened, I give you two other ways:

Code:
sed 's/.*/.........&/' pattern.txt|grep -vf - filepattern.txt
Code:
sed 's/.*/.\\{9\\}&/' pattern.txt|grep -vf - filepattern.txt

Where filepattern.txt is the first file you described, and pattern.txt, the second.
 
Erratum:

Code:
sed 's/.*/^.........&/' pattern.txt|grep -vf - filepattern.txt
Code:
sed 's/.*/^.\\{9\\}&/' pattern.txt|grep -vf - filepattern.txt
 
Hi FlorianAwk.. thanks a lot for your solution too! REGEXs are impressing me everytime :)

@PHV for me to understand it better.. what exactly is the funktion of
++n?
I can imagine what, but i could not find an examble which i can say i could understand to 100% and be absolutely sure about the function of it..

I hope you have 5 min to explain it.
 
++n is equivalent to n=n+1
But, the really important thing is n[$1]

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
n contains the entries from filetwo=$1..

and the second "n" in (substr($0,10,3) in n) reffers to fileone

ok i have it..

Thanks!!
 
is it possible to put this in an IF request?

the whole thing changed a little bit, and i have now to check, if the current line from fileone, whichis in access from awk, if the substring has a value which exists in filetwo, then i have to do something....

so i tried over ...
Bash:
cat fileone | awk 'val=substring($0.10,3); if (grep -q val "filetwo"){printf(val"\n")}'

so i thougt if the IF request is true, i will get an output. But i get ALL as result. No filtering.
it don't works...

I would apreciate every idea!

thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top