Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have this file:

# more file.txt
 2c2
lns-ld-wall-01-t2 old:261,260
4c4
Prive_ORANGE old:258,259

I need to remove all lines like this one 2c2 and this one 4c4. How can I do in a shell script?

share|improve this question
3  
What did you try? –  cuonglm May 4 at 9:13

3 Answers 3

up vote 2 down vote accepted

This should work:

sed 's/.*[0-9]c[0-9].*//g' file.txt

However as mentioned in the comment, above command will not remove the complete lines, so a better way would be to use the d command on matching lines instead:

sed '/[0-9]c[0-9]/d' file.txt

Or with extended regular expressions with GNU sed (-E is the preferred flag for compatibility with BSD* seds):

sed -E '/^[[:blank:]]*[0-9]+[a-z]+[0-9]/d' file
share|improve this answer
2  
this will not remove line, and will not work with 12c12 like lines. –  Archemar May 4 at 9:21
    
@Archemar : Thanks. I've corrected the command. –  shivams May 4 at 9:27
3  
Before providing answers let's wait and see what sie already tried. Nobody is expecting to spoon feed anybody. –  val0x00ff May 4 at 9:30
    
Yes. I'll keep that in mind for future. –  shivams May 4 at 9:30
1  
\+, \? and \t are not POSIX. -E should be preferred over -r as it's supported by both GNU and some BSDs sed and is going to make it to the next version of the standard. –  Stéphane Chazelas May 4 at 10:34

grep (and egrep or its modern equivalent grep -E) is your friend:

man grep

See the -v option.

Here:

grep -Exv '[[:blank:]]*[0-9]+c[0-9]+[[:blank:]]*' file

would filter out lines consisting of c surrounded by decimal digits and optional blanks.

share|improve this answer
    
If you read the man page on grep you can see that using egrep is deprecated. –  Anthon May 4 at 9:52
1  
Mentioning egrep is certainly even unnecessary for the given question and doesn't contribute any necessary functionality. (Effectively egrep is anyway just grep -E.) That all said; Using grep -v is (compared with sed) certainly the more straightforward solution (so I don't think the "-1" is justified). –  Janis May 4 at 10:07
    
@Anthon: Sorry, I am not able to read your man page. I can only read mine. Now, seriously: I mentioned egrep in addition to grep because from the question (which gave just an example of strings to include), it might be that the OP wanted maybe have a list of pattern to be excluded. About the obsoletedness, this depends on which platform you are. On some platforms, -E is not supported. On some (mine for example), egrep is not mentioned as deprecated in the man-page. For this reason, I prefer egrep over grep -E (the scripts are more portable). –  user1934428 May 4 at 13:49

Take the perl way.

Test with:

perl -ne 's/^\s*[0-9]+c[0-9]+$//g; if ($_ !~ /^$/) {print }' file.txt

Change the file file.txt with:

perl -ne 's/^\s*[0-9]+c[0-9]+$//g; if ($_ !~ /^$/) {print }' -i file.txt
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.