Remove lines with sed
Hi
I have a large file and need to remove all the lines containing symbol/symbols. For example: . , ! " # $ % & / ( ) = ? ¡ ¿ ' ´ + * ¨ { } ] [ - _ : ; , > < (maybe more) Thanks in advance! |
Quote:
Some tutes here from IBM. The first one may be the one. what you want to do is remove all chars except [A-Z] [a-z] [0-9] regexp would look like...[^A-Z] [^a-z] [^0-9] http://www.ibm.com/developerworks/li...ry/l-sed2.html http://www.ibm.com/developerworks/vi...for+a+new+user http://www.regular-expressions.info http://sed.sourceforge.net/sed1line.txt Regards Glenn |
Hi GlennsPref :)
I don't want to remove chars, I want to remove the whole lines containing one or more symbols. I already read about sed at some spanish sites, by the way I can't understand english tutorials then it's a bit hard to learn at these sites. Anyway, I will take a look. |
using the regexp, [A-Z][a-z][0-9]
Code:
# print only lines which match regular expression (emulates "grep") Code:
sed -n '/[A-Z][a-z][0-9]/p' > ~/filename Or the other way 'round. Code:
# print only lines which do NOT match regexp (emulates "grep -v") Hope that helps, you'll have to experiment. Cheers Glenn |
The 'd' flag is indeed the easier option and the easier regex is a character class:
Code:
sed '/[^[:alnum:]]/d' file |
Hmm, I got this, still looking for something more succinct.
Code:
bash-4.1$ sed -e '/[^A-Z][^a-z][^0-9]/d' -e '/:/d; / *#/d; /^ *$/d' /home/glenn/build/scripting/filename1 Code:
http://sed.sourceforge.net/sed1line.txt @Grail. On my system that command erased every thing except blank lines. I'm still learning these tools. Hope this wasn't homework!!! Regards Glenn |
Quote:
Code:
sed '/[^[:alnum:][:space:]]/d' filename |
@colucix, That one works well.
Quote:
I'm having trouble appending another regexp to remove leading spaces. Cheers Glenn |
You can also use the -v option in grep to exclude lines matching the patterns, leaving the rest of the lines.
|
You can put together multiple expressions using multiple -e options. For example:
Code:
sed -e '/[^[:alnum:][:space:]]/d' -e 's/^[[:space:]]*//' -e '/^$/d' filename |
Beautiful! colucix.
I hope the OP likes it. Glenn |
does it have t be sed? grep -v and then the regex for your symbols will give you what you want.
edit:ah, just saw where someone already mentioned it |
Thanks everyone, this help me so much :)
|
Glad you got a solution. Please mark as SOLVED once you are satisfied.
|
All times are GMT -5. The time now is 04:00 PM. |