LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-29-2013, 09:02 AM   #1
j-me
Member
 
Registered: Jan 2003
Location: des moines, ia
Distribution: suse RH
Posts: 129

Rep: Reputation: 17
remove a line from a comma delimited file that contains a single digit in position 2


I know this is very specific question. I have tried a few sed statements and grep with no luck. Here is my data:
Code:
Deploy_ACC_FBL.Legacy.ACV_Production,1,ACC_FBL.Legacy.ACV_2012.09.32,SUCCESS,
Deploy_ACC_FBL.Legacy.ACV_Production,2,ACC_FBL.Legacy.ACV_2012.09.44,FAILURE,
Deploy_ACC_FBL.Legacy.ACV_Production,2013-02-16_22-03-50,ACC_FBL.Legacy.ACV_2012.09.32,SUCCESS,
I need to remove the line that contains the single number in the second position while retaining the line that contains the valid date in second position.
Code:
sed '/[0-9]/d' filename.txt > fileout.txt
yielded nothing in the output file. I know why.
thank you
 
Old 05-29-2013, 09:24 AM   #2
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
Code:
awk -F, '($2 ~ /^[0-9]$/) { next } { print $0 }'
 
Old 05-29-2013, 09:26 AM   #3
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
Code:
sed -r '/^[^,]*,[0-9],/d'
 
Old 05-29-2013, 09:32 AM   #4
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
with bash:
Code:
while IFS=, read a b rest ; do [[ "$b" =~ ^[0-9]$ ]] && continue ; printf "%s,%s,%s\n" "$a" "$b" "$rest" ; done
 
Old 05-29-2013, 09:54 AM   #5
j-me
Member
 
Registered: Jan 2003
Location: des moines, ia
Distribution: suse RH
Posts: 129

Original Poster
Rep: Reputation: 17
ok. that works but could you possibly take a few minutes and explain the command please? I would like to understand this better. specifically the ^,[^,]* . that is so confusing to me.

Also, I have some lines w/ 2 digit numeric in the field 2. I just modified and ran thru the output file to remove those with [0,9][0-9].

Very much appreciate your help ... and explanation.
 
Old 05-29-2013, 10:20 AM   #6
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
seems like you'd better read something about regexps, but I'll try to explain this one: /^[^,]*,[0-9],/

values cannot include comma, then /[^,]*,/ is a general pattern for each column (excluding the last one, that does not have trailing comma: /[^,]*$/)

providing you need to match a single digit in the 13-th column it means that 12 columns go first followed by /[0-9],/

the line beginning matches with '^' at the 1st position in a whole regexp:
/^([^,]*,){12}[0-9],/

since you need to match the 2nd column, there is only one column goes before it, and no need to use repetition in this case:
/^([^,]*,)[0-9],/

and grouping as well:
/^[^,]*,[0-9],/
 
Old 05-30-2013, 08:25 AM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
Deploy_ACC_FBL.Legacy.ACV_Production,1,ACC_FBL.Legacy.ACV_2012.09.32,SUCCESS,
Deploy_ACC_FBL.Legacy.ACV_Production,2,ACC_FBL.Legacy.ACV_2012.09.44,FAILURE,
Deploy_ACC_FBL.Legacy.ACV_Production,2013-02-16_22-03-50,ACC_FBL.Legacy.ACV_2012.09.32,SUCCESS,
... this awk ...
Code:
awk -F, '($2!~"^[0-9]$")' $InFile >$OutFile
... produced this OutFile ...
Code:
Deploy_ACC_FBL.Legacy.ACV_Production,2013-02-16_22-03-50,ACC_FBL.Legacy.ACV_2012.09.32,SUCCESS,
Daniel B. Martin
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
output pid's from ps to a comma delimited file machielr Linux - General 12 06-26-2016 06:15 PM
using sed to remove line in a comma-delimited file seefor Programming 4 03-10-2009 03:35 PM
column re-alignment - space delimited to comma delimited hattori.hanzo Linux - Newbie 9 03-05-2009 12:54 AM
separating a comma delimited line mrobertson Programming 7 07-27-2005 01:56 PM
comma delimited file cdragon Programming 5 06-21-2002 07:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:10 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration