awk to remove line if field has symbols in it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to remove line if field has symbols in it
# 1  
Old 11-16-2016
awk to remove line if field has symbols in it

Trying to use awk to remove a line only if $1 contains either ; or :. Thje awk below runs but no lines are removed. Thank you Smilie.

awk
Code:
awk '$1 !~ /;/ || $1 !~ /:/ { print }' file

file
Code:
AARS2;TMEM151B 1
AASS 2
ABAT 3
ABCA1 3
ABCA10 1
ABCA12 2 
ABCA13 1
ABCA13:AX746840 2
ABCA2 5

desired output
Code:
AASS 2
ABAT 3
ABCA1 3
ABCA10 1
ABCA12 2 
ABCA13 1
ABCA2 5

# 2  
Old 11-16-2016
Code:
awk '$1 !~ /;|:/' file

Code:
awk '$1 !~ /[;:]/' file

This User Gave Thanks to Aia For This Post:
# 3  
Old 11-16-2016
Quote:
Originally Posted by cmccabe
Trying to use awk to remove a line only if $1 contains either ; or :. Thje awk below runs but no lines are removed. Thank you Smilie.

awk
Code:
awk '$1 !~ /;/ || $1 !~ /:/ { print }' file

file
Code:
AARS2;TMEM151B 1
AASS 2
ABAT 3
ABCA1 3
ABCA10 1
ABCA12 2 
ABCA13 1
ABCA13:AX746840 2
ABCA2 5

desired output
Code:
AASS 2
ABAT 3
ABCA1 3
ABCA10 1
ABCA12 2 
ABCA13 1
ABCA2 5

Hi cmccabe,
The code you have above will print any line where $1 does not contain a semicolon OR does not contain a colon which will print any line where $1 does not contain both a semicolon and a colon. Either of the suggestions Aia provided should do what you want. Given the way you stated your problem, I would usually use Aia's second suggestion.

It could also be written as:
Code:
awk '$1 ~ /;/ || $1 ~ /:/ { next } 1' file

This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 11-16-2016
Another approach is to only print a line if it does not contain ; AND it does not contain :

Code:
awk '$1 !~ /;/ && $1 !~ /:/ { print }' file

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 11-17-2016
Thank you all Smilie
# 6  
Old 11-17-2016
Quote:
Originally Posted by cmccabe
Trying to use awk to remove a line only if $1 contains either ; or :. Thje awk below runs but no lines are removed. Thank you Smilie.
awk
Code:
awk '$1 !~ /;/ || $1 !~ /:/ { print }' file

file
Code:
AARS2;TMEM151B 1
AASS 2
ABAT 3
ABCA1 3
ABCA10 1
ABCA12 2 
ABCA13 1
ABCA13:AX746840 2
ABCA2 5

desired output
Code:
AASS 2
ABAT 3
ABCA1 3
ABCA10 1
ABCA12 2 
ABCA13 1
ABCA2 5

Hello cmccabe,

Not sure your complete moto of this script but in case you don't want to have those lines which have either ; or | not just only first field then you could use following command too for same.
Code:
grep -v '[;:]'  Input_file

Output will be as follows.
Code:
AASS 2
ABAT 3
ABCA1 3
ABCA10 1
ABCA12 2 
ABCA13 1
ABCA2 5

Thanks,
R. Singh

Last edited by RavinderSingh13; 11-17-2016 at 02:00 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. 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

3. UNIX for Dummies Questions & Answers

How to remove first field of the next line?

Hi, I have an output file like below: php1od711,db1,2300 php1od711,db2,2400 php1od711,db3,2500 php1od711,db4,2600 php1od712,dba,3300 php1od712,dbb,3400 php1od712,dbc,3500 php1od712,dbd,3600 php1od713,zz1,4300 php1od713,zz2,4300 php1od713,zz3,4300Is there an awk/sed trick to... (1 Reply)
Discussion started by: newbie_01
1 Replies

4. UNIX for Dummies Questions & Answers

Using awk to remove duplicate line if field is empty

Hi all, I've got a file that has 12 fields. I've merged 2 files and there will be some duplicates in the following: FILE: 1. ABC, 12345, TEST1, BILLING, GV, 20/10/2012, C, 8, 100, AA, TT, 100 2. ABC, 12345, TEST1, BILLING, GV, 20/10/2012, C, 8, 100, AA, TT, (EMPTY) 3. CDC, 54321, TEST3,... (4 Replies)
Discussion started by: tugar
4 Replies

5. Shell Programming and Scripting

How to remove line feeder in one field.?

Hi Gurus, I have another question (sorry, too many questions). I need remove the line feeder in one field. the file is comma delimted with double quote option which means the comma within double quote is not delimiter. "abc", "cde", "abc, cde", "def" "dgh", "ded", "cdd, ", "ghd" ... (1 Reply)
Discussion started by: ken6503
1 Replies

6. Shell Programming and Scripting

Awk to remove carriage return from 65th field

Hi, I have a pipe delimited file. There are around 700 columns in the file. The 65th column has carriage return which is causing read issue with our ETL process. I would like to replace the new line characters in 65th field with "nothing" i have return the following code and need help to... (7 Replies)
Discussion started by: pinnacle
7 Replies

7. Shell Programming and Scripting

Remove duplicate lines (the first matching line by field criteria)

Hello to all, I have this file 2002 1 23 0 0 2435.60 131.70 5.60 20.99 0.89 0.00 285.80 2303.90 2002 1 23 15 0 2436.60 132.90 6.45 21.19 1.03 0.00 285.80 2303.70 2002 1 23 ... (6 Replies)
Discussion started by: joggdial3000
6 Replies

8. Shell Programming and Scripting

how to remove weird symbols

Hi my file is suffering from some weird text symbols like a question mark inside of a square. I don't know how to remove these. I tried to remove UTF accents with command sed -e 's/^*Width:*//' -e 's/*$//'` but no use . Could u guyz plz help me. (4 Replies)
Discussion started by: repinementer
4 Replies

9. Shell Programming and Scripting

remove accents and symbols with sed

Hi, I would like to know how could I remove accentes and the symbols: º and ª of a text file with sed. Whis this command doesn't works :-( sed "s/í/i/g" filename Many thanks and sorry for my english! (7 Replies)
Discussion started by: mierdatuti
7 Replies

10. Shell Programming and Scripting

Awk scrip to remove empty field

Hi I have a file which looks like this name: Sally group: Group4 name: Tim group: Group1 name: Dan group: Group2 name: Chris group: Group3 name: Peter group: name: Fred group: name: Mary group: Group2 Well I want to get rid of the... (4 Replies)
Discussion started by: bombcan
4 Replies
Login or Register to Ask a Question