Remove rows containing commas with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove rows containing commas with awk
# 1  
Old 12-09-2014
Remove rows containing commas with awk

Hello everyone, I have a dataset that looks something like:

Code:
1 3
2 2
3 4,5
4 3:9
5 5,9
6 5:6

I need to remove the rows that contain a comma in the second column and I'm not sure how to go about this. Here is an attempt.

Code:
awk 'BEGIN {FS=" "} { if ($2!==,) print }'

Any help is appreciated.

Last edited by bartus11; 12-09-2014 at 12:02 PM.. Reason: Please use [code][/code] tags.
# 2  
Old 12-09-2014
Code:
awk '$2~/,/ {next} {print}'  oldfile > newfile

You can use the ~ (match operator) on a field like $2
# 3  
Old 12-09-2014
Try

Code:
awk  '{ split($2, subfield, ",");   if ( length(subfield)<2) print $0 }'

# 4  
Old 12-09-2014
Code:
awk '$2 !~ /,/' file

or
Code:
egrep -v " .*," file

# 5  
Old 12-09-2014
Quote:
Originally Posted by Yoda
Code:
egrep -v " .*," file

Your egrep approach will also delete lines with commas other than 2nd col?
# 6  
Old 12-09-2014
Quote:
Originally Posted by senhia83
Your egrep approach will also delete lines with commas other than 2nd col?
Yes, egrep approach will work only on the sample input posted.
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 remove unwanted commas from a .csv file?

how to remove unwanted commas from a .csv file Input file format "Server1","server-PRI-Windows","PRI-VC01","Microsoft Windows Server 2012, (64-bit)","Powered On","1,696.12","server-GEN-SFCHT2-VMS-R013,server-GEN-SFCHT2-VMS-R031,server-GEN-SFCHT2-VMS-R023"... (5 Replies)
Discussion started by: ranjancom2000
5 Replies

2. Shell Programming and Scripting

Remove quotes and commas from field

In the attached file I am trying to remove all the "" and , (quotes and commas) from $2 and $3 and the "" (quotes) from $4. I tried the below as a start: awk -F"|" '{gsub(/\,/,X,$2)} 1' OFS="\t" enhancer.txt > comma.txt Thank you :). (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Shell Code required -Output in Multiple Rows to be in single row separated by Commas -

Hola Greetings Experts , I have records spreaded across multiple lines. in attached log.txt i want output to be in 1 line like this below Atached as Output.txt. In brief Output related to 1 line is spreaded across multiple row I wanted it to be in 1 row . Please opem the file in notepad... (4 Replies)
Discussion started by: manishK
4 Replies

4. Shell Programming and Scripting

Remove leading commas in the file

Hi , I have a file with below records 965382,10310858,1,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 767010,10217614,3,10217616,10217622,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,... (4 Replies)
Discussion started by: vputtas@gmail.c
4 Replies

5. Shell Programming and Scripting

remove commas if touching any letters

I have a csv file that I am trying to edit. I need to remove any comma that is touching a letter. I used this command to find them. grep , moz_places_good.csv | head -n 10 | grep ,I would think I could use some form of this to just delete the commas and not the letter before it. Just not sure... (5 Replies)
Discussion started by: cokedude
5 Replies

6. Shell Programming and Scripting

Remove rows with first 4 fields duplicated in awk

Hi, I am trying to use awk to remove all rows where the first 4 fields are duplicates. e.g. in the following data lines 6-9 would be removed, leaving one copy of the duplicated row (row 5) Borgarhraun FH9822 ol24 FH9822_ol24_m20 ol Deformed c Borgarhraun FH9822 ol24 ... (3 Replies)
Discussion started by: tomahawk
3 Replies

7. Shell Programming and Scripting

shell script to remove extra commas from CSV outp file

Name,,,,,,,,,,,,,,,,,,,,Domain,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Contact,Phone,Email,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Location -----------------------,------------------------------------------------,-------,-----,---------------------------------,------------------------------------ ----... (1 Reply)
Discussion started by: sreenath1037
1 Replies

8. Shell Programming and Scripting

Remove duplicate commas after exporting excel file to csv

Hello everyone I'm new here and this is my first post so first of all I want to say that this is a great forum and I have managed to found most of my answers in these forums : ) So with that I ask you my first question: I have an excel file which I saved as a csv. However the excel file... (3 Replies)
Discussion started by: Spunkerspawn
3 Replies

9. Shell Programming and Scripting

awk script to remove duplicate rows in line

i have the long file more than one ns and www and mx in the line like . i need the first ns record and first www and first mx from line . the records are seperated with tthe ; i am try ing in awk scripting not getiing the solution. ... (4 Replies)
Discussion started by: kiranmosarla
4 Replies

10. Linux

How do i remove commas(,) & spaces

Hey guys, I am very much new to shell scripts. So you ppl may feel that i am asking stupid question here. :D 1. I am using command line argument as an input variable. The user gets this value in his mail from client which has commas n spaces (Eg. 12,34,56,789) and the scripts... (5 Replies)
Discussion started by: anushree.a
5 Replies
Login or Register to Ask a Question