LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-10-2009, 01:58 PM   #1
seefor
Member
 
Registered: Mar 2006
Posts: 34

Rep: Reputation: 15
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
 
Old 03-10-2009, 02:15 PM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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;""
 
Old 03-10-2009, 02:25 PM   #3
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,822

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

Code:
| sed  '/^*;NA;*/d'
 
Old 03-10-2009, 02:42 PM   #4
seefor
Member
 
Registered: Mar 2006
Posts: 34

Original Poster
Rep: Reputation: 15
Thank you very much jschiwal and crabboy.

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
 
Old 03-10-2009, 03:35 PM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
column re-alignment - space delimited to comma delimited hattori.hanzo Linux - Newbie 9 03-05-2009 12:54 AM
Manipulating comma delimited CSV - newbie help with sed etc jonnymorris Programming 16 09-19-2008 06:14 AM
separating a comma delimited line mrobertson Programming 7 07-27-2005 01:56 PM
Comma-Delimited Website Filenames Apocalypse General 1 11-09-2003 09:05 AM
comma delimited file cdragon Programming 5 06-21-2002 07:55 PM


All times are GMT -5. The time now is 02:39 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration