Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 446
  • Last Modified:

Bourne Shell Script - Problem with sed

I'm using sed on a formatted file that ends in the following:

year
since
I+had
last
seen
Simonov.+

Simonov.+ is the end of the file.

I then use:  sed 's/+\{2,\}/        /g'

to get rid of all instances of more than one '+' and replace them with a tab. This works in cases throughout the file, however, at the
end of the file, it removes the Simonov.+ completely.

So I get this:

It
was
almost
a
year
since
I+had
last
seen


What's wrong with my filter that at the end of file it removes the complete line? sed 's/+\{2,\}/        /g'

Thanks.
0
vupadhya
Asked:
vupadhya
1 Solution
 
sunnycoderCommented:
Why do you need the { } group?

simply use  sed 's/+/
0
 
sunnycoderCommented:
ooopss .. posted too soon

use sed 's/+/           /g'
0
 
ahoffmannCommented:
sed 's/+++*/        /g'

> end of the file, it removes the Simonov.+ completely.
either a buggy sed, or a corrupted file
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
NovaDenizenCommented:
You need to remember that + is a special character in regexp's.  Also, it isn't the best idea to include significant whitespace in your code (i.e. an inline tab).

Many versions of sed can't handle the {2,} syntax.  That is a gnuism, and also in perl, but not in many other utilities.  Also, putting the tab in the middle of the s/// expression can confuse sed.  I added an extra \ before the tab.

I use printf to generate the tab, so there will be no mysterious whitespace in the script file.  Using printf causes an extra level of escapes to be necessary, though.
So, this is the command I would use:
sed `printf 's/\\+\\++/\\\t/g'`  < data

This causes the command 's/\+\++/\[tab]/g' to be run in sed, with a tab char replacing the [tab].  If you don't care about mysterious whitespace in your script file, you can skip printf and just do this (replace [tab] with an actual tab):
sed 's/\+\++/\[tab]/g' < data



0
 
ahoffmannCommented:
>  You need to remember that + is a special character in regexp's.
GNU sed only

> Many versions of sed can't handle the {2,} syntax.
ranges are GNU sed only
0
 
sunnycoderCommented:
sed -V
GNU sed version 4.0.5

and

sed 's/+/\t/g'
works fine
0
 
manav_mathurCommented:
vupadhya
your command works perfectly when I try on my system (Hp-UX)

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now