Visit The New, Modern Unix Linux Community


Using awk to remove lines from file that match text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk to remove lines from file that match text
# 1  
Using awk to remove lines from file that match text

I am trying to remove each line in which $2 is FP or RFP. I believe the below will remove one instance but not both. Thank you Smilie.

file
Code:
12
123    FP
11
10    RFP

awk
Code:
awk -F'\t' '
$2 != "FP"' file

desired output
Code:
12
11


Last edited by cmccabe; 09-02-2016 at 01:31 PM.. Reason: added desired output
# 2  
Hello cmccabe,

Could you please try following and let me know if this helps you.
Code:
awk '($2 !="FP" && $2 !="RFP")'  Input_file
OR
awk '($2 =="FP" || $2 =="RFP"){next} 1' Input_file

Output will be as follows.
Code:
12
11

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
You could also try:
Code:
awk 'BEGIN{d["FP"];d["RFP"]}!($2 in d)' file

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.

Last edited by Don Cragun; 09-02-2016 at 02:49 PM.. Reason: Fixed typo noted in post #4: s/"RFP/"RFP"/
These 2 Users Gave Thanks to Don Cragun For This Post:
# 4  
Quote:
Originally Posted by Don Cragun
You could also try:
Code:
awk 'BEGIN{d["FP"];d["RFP]}!($2 in d)' file

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
Hello Don,

Thank you for nice code, I think as by typo a "is missed in "RFP".
Code:
awk 'BEGIN{d["FP"];d["RFP"]}!($2 in d)' file

Thanks,
R. Singh
These 3 Users Gave Thanks to RavinderSingh13 For This Post:
# 5  
Quote:
Originally Posted by RavinderSingh13
Hello Don,

Thank you for nice code, I think as by typo a "is missed in "RFP".
Code:
awk 'BEGIN{d["FP"];d["RFP"]}!($2 in d)' file

Thanks,
R. Singh
Yes. Thank you for noticing. I have updated post #3 to fix the typo.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 6  
Thank you both Smilie.
# 7  
Wouldn't
Code:
grep -v "FP$" file
12
11

do?
This User Gave Thanks to RudiC For This Post:

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #61
Difficulty: Easy
A computer on a LAN segment is uniquely identified by its MAC address.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. UNIX for Beginners Questions & Answers

awk function to remove lines that contain contents of another file

Hi, I'd be grateful for your help with the following. I have a file (file.txt) with 10 columns and about half a million lines, which in simplified form looks like this: ID Col1 Col2 Col3.... a 4 2 8 b 5 6 1 c 8 4 1 d... (4 Replies)
Discussion started by: aberg
4 Replies

3. Shell Programming and Scripting

Match all lines in file where specific text pattern is less than

In the below file I am trying to grep or similar, all lines where only AF= is less than 0.4.. Thank you :). grep grep "AF=" ,+ .4 file file 12 112036782 . T C 34.0248 PASS ... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

awk to remove field and match strings to add text

In file1 field $18 is removed.... column header is "Otherinfo", then each line in file1 is used to search file2 for a match. When a match is found the last four strings in file2 are copied to file1. Maybe: cut -f1-17 file1 and then match each line to file2 file1 Chr Start End ... (6 Replies)
Discussion started by: cmccabe
6 Replies

5. Shell Programming and Scripting

awk remove/grab lines from file with pattern from other file

Sorry for the weird title but i have the following problem. We have several files which have between 10000 and about 500000 lines in them. From these files we want to remove lines which contain a pattern which is located in another file (around 20000 lines, all EAN codes). We also want to get... (28 Replies)
Discussion started by: SDohmen
28 Replies

6. Shell Programming and Scripting

Remove multiple lines that match pattern

Not sure how I can accomplish this. I would like to remove all interfaces that have the commands I would like to see: switchport port-security, spanning-tree portfast. One line is no problem. interface FastEthernet0/8 spanning-tree portfast interface FastEthernet0/9 spanning-tree... (4 Replies)
Discussion started by: mrlayance
4 Replies

7. Shell Programming and Scripting

Remove multiple lines from a text file

Hi I have a text file named main.txt with 10,000 lines. I have another file with a list of line numbers (around 1000) of the lines to be deleted from main.txt file. I tried with sed but it removes only a range of line numbers. Thanks for any help!! (1 Reply)
Discussion started by: prvnrk
1 Replies

8. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

9. UNIX for Dummies Questions & Answers

Remove blank lines and comments from text file

Hi, I am using BASH. How can I remove any lines in a text file that are either blank or begin with a # (ie. comments)? Thanks in advance. Mike (3 Replies)
Discussion started by: msb65
3 Replies

10. Shell Programming and Scripting

sed/awk help to match list of patterns and remove from org file

Hi, From the pattern mentioned below remove lines based on pattern range. Conditions 1 Look For all lines starting with ALTER TABLE and Ending with ; and contains the word MOVE.I wanto to remove these lines from the file sample below. Note : The above pattern list could be found in... (1 Reply)
Discussion started by: rajan_san
1 Replies

Featured Tech Videos