LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   using sed to remove line in a comma-delimited file (https://www.linuxquestions.org/questions/programming-9/using-sed-to-remove-line-in-a-comma-delimited-file-710553/)

seefor 03-10-2009 01:58 PM

using sed to remove line in a comma-delimited file
 
Greetings all,
I have a script that output all my data in to a comma-delimited file separated by ";"

Current Output:
Quote:

"SAP 1117A";10.94.1.7;239.234.1.12;0;0;0;0;0;3;172.31.207.45;""
"";NA;239.192.1.50;0;0;0;0;0;3;172.31.207.61;""
"";NA;239.192.1.50;0;0;0;0;0;3;172.31.207.62;""
"";NA;239.192.1.50;0;0;0;0;0;3;172.31.207.63;""
"";NA;239.192.1.50;0;0;0;0;0;3;172.31.207.64;""
....
I would like to remove all lines that have an NA in the second field.
New Output
Quote:

"SAP 1117A";10.94.1.7;239.234.1.12;0;0;0;0;0;3;172.31.207.45;""
....
Would this be possible to do in sed?

Thanks in Advance,
SeeFor

jschiwal 03-10-2009 02:15 PM

I guess you would call this a semi-colon delimited file!

I think awk would be a better tool because the data is organized in records, and the NA needs to match in just the second field.

Code:

awk -F\; '$2 != "NA" { print }' sample
"SAP 1117A";10.94.1.7;239.234.1.12;0;0;0;0;0;3;172.31.207.45;""


crabboy 03-10-2009 02:25 PM

This could possibly match an NA not in the second field, but its quick.

Code:

| sed  '/^*;NA;*/d'

seefor 03-10-2009 02:42 PM

Thank you very much jschiwal and crabboy.:D:D:D

I couldn't use SED cause the rest of the data have NA in different fields.


Code:

awk -F\; '$2 != "NA" { print }'
Works great ;)

Thanks again,
SeeFor

jschiwal 03-10-2009 03:35 PM

You could use a matching expression such as :sed '/^[^;][^;]*;NA;/d' file. But it would be uuugly.
Awk is the program to use for text files that are organized in fields.
That is what it was written for.


All times are GMT -5. The time now is 11:59 AM.