Regular Expression not working with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Regular Expression not working with sed
# 1  
Old 11-02-2011
Regular Expression not working with sed

Hi All,

I am facing some problems with regular expression with sed.

I have a .txt file with the contents as below:

This is a dummy file

# File created to test execution of regular expression.

Hope it works out.

As in the above contents there is a blank line which does not contain anything other than a new line (Enter Key of keyboard).

I execute the command

sed '/^$/d' regexp.txt

And it prints the complete text along with the blank lines. I cannot figure it out. Is there anything wrong in the syntax?

Next thing I tried is with the text

This is a dummy file
# File created to test execution of regular expression.
Hope it works out.

I tried the following command

sed '/^#/d' regexp.txt

And it prints the complete text with the line which has # in the begining.

My requirement is that if on every line the first character is any special character such as _,-,,(comma),',",$,^,#,~ etc, it should delete the line along with all the empty lines should be deleted.

help is highly appreciated.
# 2  
Old 11-02-2011
Did you create the .txt file in Windows? It will contain garbage carriage returns which sed will consider as part of the text.
# 3  
Old 11-02-2011
yes I did.

So what would you suggest?
# 4  
Old 11-02-2011
Use dos2ux or dos2unix command...
# 5  
Old 11-02-2011
isn't this thread the same as this thread?
Please don't double-post - stick to one thread.
And also, please, use code tags when posting data/code samples.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

SHELL: UNIX : Ls regular expression not working when used with variables

If i do below command in unix prompt which static values (ie 27..97), it is working fine and gives desired output >ls -d $WORKDIR/batch/somefilename_{27..97}.* 2>/dev/null somefilename_27.sometxt somefilename_28.sometxt somefilename_29.sometxt .. somefilename_97.sometxt But if i want... (2 Replies)
Discussion started by: haiderali
2 Replies

2. Shell Programming and Scripting

Grep with Regular expression now working on file directories

Hello Everyone, I have a file sam1 with the below content SYSYSID;MANDT;/SIE/AD_Z0M_INDX;/SIE/AD_Z0M_KEY1 echo $Regex \bSYSYSID\b|\bMANDT\b|\b/SIE/AD_Z0M_INDX\b|\b/SIE/AD_Z0M_KEY1\b cat sam1 | grep -Eo $Regex I expect the result as SYSYSID MANDT /SIE/AD_Z0M_INDX /SIE/AD_Z0M_KEY1... (4 Replies)
Discussion started by: sam99
4 Replies

3. Shell Programming and Scripting

sed regular expression

Hi , I need to remove pipe character from a |^ delimeted file. Something like |^tran|sformers||^|revenge |of fallen|^ to |^transformers|^revenge of fallen|^... Cold anybody please help to build the regular expression using sed . many thanks. Please use code tags next time for... (1 Reply)
Discussion started by: kokjek
1 Replies

4. Shell Programming and Scripting

I am learning regular expression in sed,Please help me understand the use curly bracket in sed,

I am learning SED and just following the shell scripting book, i have trouble understanding the grep and sed statement, Question : 1 __________ /opt/oracle/work/antony>cat teledir.txt jai sharma 25853670 chanchal singhvi 9831545629 anil aggarwal 9830263298 shyam saksena 23217847 lalit... (7 Replies)
Discussion started by: Antony Ankrose
7 Replies

5. UNIX for Advanced & Expert Users

sed: -e expression #1, char 0: no previous regular expression

Hello All, I'm trying to extract the lines between two consecutive elements of an array from a file. My array looks like: problem_arr=(PRS111 PRS213 PRS234) j=0 while } ] do k=`expr $j + 1` sed -n "/${problem_arr}/,/${problem_arr}/p" problemid.txt ---some operation goes... (11 Replies)
Discussion started by: InduInduIndu
11 Replies

6. Programming

Perl: How to read from a file, do regular expression and then replace the found regular expression

Hi all, How am I read a file, find the match regular expression and overwrite to the same files. open DESTINATION_FILE, "<tmptravl.dat" or die "tmptravl.dat"; open NEW_DESTINATION_FILE, ">new_tmptravl.dat" or die "new_tmptravl.dat"; while (<DESTINATION_FILE>) { # print... (1 Reply)
Discussion started by: jessy83
1 Replies

7. UNIX for Dummies Questions & Answers

Regular Expression In Sed

Hi , I am learing sed echo abc 123 def 456 | sed 's|\(*\) \(*\)|\1|' is returning abc def 456 i was hoping abc def "\1" should only print the occurence of the first pattern but according to my understanding it is just removing the first occurence of the second pattern... (7 Replies)
Discussion started by: max_hammer
7 Replies

8. Shell Programming and Scripting

Regular expression (sed)

Hi I need to get text that are within "" For example File: asdasd "test test2" sadasds asdda asdasd "demo demo2" Output: test test2 demo demo2 Any help is good Thank you (12 Replies)
Discussion started by: blito_loco
12 Replies

9. Shell Programming and Scripting

regular expression not working.

Hi, I have to match a certain pattern of string in my shell script: 6.0.4.11.9 7.5.1.7.1 First Number can be 6 or 7 Second number can be 0 or 5 Rest all numbers can be between 1-99 I am using following egrep: egrep ^\.\.\.\.\$ filename But why is the above regular... (1 Reply)
Discussion started by: som.nitk
1 Replies
Login or Register to Ask a Question