How to remove a line based on contents of the first column?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove a line based on contents of the first column?
# 1  
Old 07-15-2014
How to remove a line based on contents of the first column?

Good day all.

Using basic UNIX/Linux tools, how would you delete a line based on a character found in column 1?

For example, if the CITY name contains an 'a' or 'A', delete the line:

Code:
New York City; New York
Los Angeles; California
Chicago; Illinois
Houston; Texas
Philadelphia; Pennsylvania
Phoenix; Arizona

should become:

Code:
New York City; New York
Houston; Texas
Phoenix; Arizona

Thanks in advnace,
BRH
# 2  
Old 07-15-2014
Code:
awk -F";" '{if($1 !~ /[aA]/){print $0}}' file

# 3  
Old 07-15-2014
Try also
Code:
sed '/^[^;]*[aA]/d' file
New York City; New York
Houston; Texas
Phoenix; Arizona

or

grep -v '^[^;]*[aA]' file
Los Angeles; California
Chicago; Illinois
Philadelphia; Pennsylvania

# 4  
Old 07-15-2014
Quote:
Originally Posted by protocomm
Code:
awk -F";" '{if($1 !~ /[aA]/){print $0}}' file

With implicit if and default print:
Code:
awk -F";" '$1 !~ /[aA]/' file


Last edited by MadeInGermany; 07-15-2014 at 05:36 PM..
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove or rename based on contents of file

I am trying to use the two files shown below to either remove or rename contents in one of those files. If in file1.txt $5 matches $5 of file2.txt and the value in $1 of file1.txt is not "No Match" then that value is substituted for all values in $5 and $1 of file2.txt. If however in $1 ... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Computing difference based on line contents

I have the following awk script set up to copy the contents of a line that contains 0008 in each line that contains values of 1895 through 2012. awk -v OFS=" " '{val=0+substr($1,length($1)-3,4);if(val==0008){print;$1=x;y=$0}else{if(val>=1895&&val<=2012){print $1 y}else{print}}}' Output... (7 Replies)
Discussion started by: ncwxpanther
7 Replies

3. Shell Programming and Scripting

remove column based on the same value

Hello, I have some problem to remove the columns which have the duplicate value of -9 which is in every row except -9 in some row. Input file showed in below : Col1 Col2 Col3 Col4 Col5 Col6 A 1 A -9 0 -9 B 2 T -9 -9 -9 C 3 D -9 1 -9 D 4 R -9 2 -9 Output should... (6 Replies)
Discussion started by: awil
6 Replies

4. Shell Programming and Scripting

Concatenating many files based on a specific column contents

Dear all, I have many files(.csv) in a directory. I want to concatenate the files which have similar entries in a particular column and save into a new file like result_datetime.csv etc. One example file is like below. Sno,Step,Data1,Data2,Data3 etc. 1,0,2,3,4 2,1,3,4,5 3,2,0,1,1 ... (4 Replies)
Discussion started by: ks_reddy
4 Replies

5. Shell Programming and Scripting

Remove lines based on column value

Hi All, I just need a quick fix here. I need to delete all lines containing "." in the 6th column. Input: 1 1055498 . G T 5.46 . 1 1902377 . C T 7.80 . 1 1031540 . A G 34.01 PASS 1 ... (2 Replies)
Discussion started by: Hkins552
2 Replies

6. Shell Programming and Scripting

remove duplicates based on single column

Hello, I am new to shell scripting. I have a huge file with multiple columns for example: I have 5 columns below. HWUSI-EAS000_29:1:105 + chr5 76654650 AATTGGAA HHHHG HWUSI-EAS000_29:1:106 + chr5 76654650 AATTGGAA B@HYL HWUSI-EAS000_29:1:108 + ... (4 Replies)
Discussion started by: Diya123
4 Replies

7. Shell Programming and Scripting

need to remove duplicates based on key in first column and pattern in last column

Given a file such as this I need to remove the duplicates. 00060011 PAUL BOWSTEIN ad_waq3_921_20100826_010517.txt 00060011 PAUL BOWSTEIN ad_waq3_921_20100827_010528.txt 0624-01 RUT CORPORATION ad_sade3_10_20100827_010528.txt 0624-01 RUT CORPORATION ... (13 Replies)
Discussion started by: script_op2a
13 Replies

8. Shell Programming and Scripting

move contents from one file to another based on line number or content

I want a script that will move everything beyond a certain line number or beyond a certain content word into another file. For example, if file A has this: first line second line third line forth line fifth line sixth line I want to run a script that will move everything beyond the third... (4 Replies)
Discussion started by: robp2175
4 Replies

9. Shell Programming and Scripting

Remove duplicate line detail based on column one data

My input file: AVI.out <detail>named as the RRM .</detail> AVI.out <detail>Contains 1 RRM .</detail> AR0.out <detail>named as the tellurite-resistance.</detail> AWG.out <detail>Contains 2 HTH .</detail> ADV.out <detail>named as the DENR family.</detail> ADV.out ... (10 Replies)
Discussion started by: patrick87
10 Replies

10. Shell Programming and Scripting

Remove lines based on contents of another file

So, this issue is driving me nuts! I was hoping to get a lending hand here... I have 2 files: file1.txt contains: this is example1 this is example2 this is example3 this is example4 this is example5 file2.txt contains: example3 example5 Basically, I need a script or command to... (4 Replies)
Discussion started by: bashshadow1979
4 Replies
Login or Register to Ask a Question