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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-29-2013, 09:02 AM
|
#1
|
Member
Registered: Jan 2003
Location: des moines, ia
Distribution: suse RH
Posts: 129
Rep:
|
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
|
|
|
05-29-2013, 09:24 AM
|
#2
|
Member
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666
Rep:
|
Code:
awk -F, '($2 ~ /^[0-9]$/) { next } { print $0 }'
|
|
|
05-29-2013, 09:26 AM
|
#3
|
Member
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666
Rep:
|
Code:
sed -r '/^[^,]*,[0-9],/d'
|
|
|
05-29-2013, 09:32 AM
|
#4
|
Member
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666
Rep:
|
with bash:
Code:
while IFS=, read a b rest ; do [[ "$b" =~ ^[0-9]$ ]] && continue ; printf "%s,%s,%s\n" "$a" "$b" "$rest" ; done
|
|
|
05-29-2013, 09:54 AM
|
#5
|
Member
Registered: Jan 2003
Location: des moines, ia
Distribution: suse RH
Posts: 129
Original Poster
Rep:
|
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.
|
|
|
05-29-2013, 10:20 AM
|
#6
|
Member
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666
Rep:
|
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],/
|
|
|
05-30-2013, 08:25 AM
|
#7
|
Senior Member
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881
|
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
|
|
|
All times are GMT -5. The time now is 12:10 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|