troubleshooting Question

sed problems - cant delete blank line, etc.

Avatar of lfrohman
lfrohman asked on
Linux OS Dev
7 Comments1 Solution284 ViewsLast Modified:
I am using the cygwin version of sed with windows.
I have several problems.

1) deleting a blank line
sed 's/\n\n/\n/ x1.txt > x2.txt --- doesn't work
sed '/^$/d' x1.txt > x2.txt --- deletes everything
sed '/^[ ]*$/d' x1.txt > x2.txt --- deletes everything

2) can't include single quote in selection
sed 's/\.put(\([a-zA-Z0-9_\"+ ]*\), \([a-zA-Z0-9_\"+ ]*\))/\[\1\] = \2/g' x1.txt > x2.txt --- works
sed 's/\.put(\([a-zA-Z0-9_\"\'+ ]*\), \([a-zA-Z0-9_\"\'+ ]*\))/\[\1\] = \2/g' x1.txt > x2.txt --- gives error
sed: -e expression #1, char 25: unterminated `s' command

3) can't include optional minus sign in selection
sed 's/\.put(\([a-zA-Z0-9_\"+ ]*\), \([a-zA-Z0-9_\"+ ]*\))/\[\1\] = \2/g' x1.txt > x2.txt
sed 's/\.put(\([a-zA-Z0-9_\"+ ]*\), \(?[a-zA-Z0-9_\"+ ]*\))/\[\1\] = \2/g' x1.txt > x2.txt
sed 's/\.put(\(-[a-zA-Z0-9_\"+ ]*\), \([a-zA-Z0-9_\"+ ]*\))/\[\1\] = \2/g' x1.txt > x2.txt
sed 's/\.put(\(-[a-zA-Z0-9_\"+ ]*\), \(?[a-zA-Z0-9_\"+ ]*\))/\[\1\] = \2/g' x1.txt > x2.txt

all work, but I would like to replace all four with one statement - which doesn't work:
sed 's/\.put(\(-?[a-zA-Z0-9_\"+ ]*\), \(-?[a-zA-Z0-9_\"+ ]*\))/\[\1\] = \2/g' x1.txt > x2.txt

Can anyone explain what I am doing wrong?


Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 7 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros