|
Home > Archive > Unix Shell > February 2007 > Sed question, please help
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Sed question, please help
|
|
|
| Is there any way with sed 's', that you can specifiy text on the line based
on the position of the characters from the start of the line?
I.e. file has lines of 10 alpha numeric characters each, I want to replace
just characters 6 through to 8 (which could be anything) with dashes, on
each line? There must be an easy way of doing this.
It's becuase I have a comma seperated text data file, each line has the same
number of characters on each field, but field 20 is blank, and i want to
insert a value into field 20 (which is say, characters 50-55 into each
line). Is there a better way to do this than with sed?
Any responses much appreciated. Thank you.
| |
| Bill Marcum 2007-02-22, 1:18 pm |
| On Thu, 22 Feb 2007 17:31:03 GMT, Ben H
<ben@noone.com> wrote:
>
>
> Is there any way with sed 's', that you can specifiy text on the line based
> on the position of the characters from the start of the line?
>
> I.e. file has lines of 10 alpha numeric characters each, I want to replace
> just characters 6 through to 8 (which could be anything) with dashes, on
> each line? There must be an easy way of doing this.
>
s/^\(.....\).../\1---/
> It's becuase I have a comma seperated text data file, each line has the same
> number of characters on each field, but field 20 is blank, and i want to
> insert a value into field 20 (which is say, characters 50-55 into each
> line). Is there a better way to do this than with sed?
>
awk -F, -v OFS=, '{$20="------"}'
--
Some stirring may be necessary to achieve proper consistency.
| |
|
| Thank you.
So I'm assuming, using sed for the second example (assuming there's 21
fields, and field 21 is the last one and is 10 chars long, and field 20
starts at char 50 and ends at char 55)), I could also use
sed 's/ ^\(.{50}\) ..... \(.{10}\) / \1 whatever \2 /g'
?
Also, I don't recall ever using backslashes (\) to escape brackets using
EREs with Grep on a Sun box?
"Bill Marcum" <marcumbill@bellsouth.net> wrote in message
news:qs21b4-ao1.ln1@don.localnet...
> On Thu, 22 Feb 2007 17:31:03 GMT, Ben H
> <ben@noone.com> wrote:
> s/^\(.....\).../\1---/
>
> awk -F, -v OFS=, '{$20="------"}'
>
>
> --
> Some stirring may be necessary to achieve proper consistency.
| |
| Michael Tosch 2007-02-22, 7:15 pm |
| Ben H wrote:
> Thank you.
>
> So I'm assuming, using sed for the second example (assuming there's 21
> fields, and field 21 is the last one and is 10 chars long, and field 20
> starts at char 50 and ends at char 55)), I could also use
>
> sed 's/ ^\(.{50}\) ..... \(.{10}\) / \1 whatever \2 /g'
>
Your idea is correct. But:
sed uses RE: you must further escape the curly brackets.
The /g option tries to repeat the action; not needed here.
Do not add any spaces (the only one allowed is after the s).
The trailing 10 chars enforce a minimum of 10,
unless followed by a $ (=line end).
Unlike awk, sed is not aware of fields.
Taking only the phrase "replace char 50-55" (=6):
sed 's/^\(.\{49\}\)....../\1whatever/'
Taking only the phrase "replace 6 chars before the last 10":
sed 's/......\(.\{10\}\)$/whatever\1/'
--
Michael Tosch @ hp : com
| |
|
| "Michael Tosch" <eedmit@NO.eed.SPAM.ericsson.PLS.se> wrote in message
news:erl27c$39c$1@aken.eed.ericsson.se...
> Ben H wrote:
>
> Your idea is correct. But:
>
> sed uses RE: you must further escape the curly brackets.
> The /g option tries to repeat the action; not needed here.
> Do not add any spaces (the only one allowed is after the s).
> The trailing 10 chars enforce a minimum of 10,
> unless followed by a $ (=line end).
>
> Unlike awk, sed is not aware of fields.
>
> Taking only the phrase "replace char 50-55" (=6):
>
> sed 's/^\(.\{49\}\)....../\1whatever/'
>
> Taking only the phrase "replace 6 chars before the last 10":
>
> sed 's/......\(.\{10\}\)$/whatever\1/'
>
>
> --
> Michael Tosch @ hp : com
Thanks. Yes, it should have been {49} and not {50} now I think about it. But
I thought /g at the end of a sed s meant to repeat the action for every line
in the file? (which is what I want to do). I was aware that sed uses reg
ex, but I thought the backslash was to escape the character after it, i.e.
take that character literally, and as curly brackets are a special
character, I thought that escaping them meant that sed would look for actual
curly brackets in the file (same is true of the { } brackets). Is it the
other way round (i.e. if you don't escape them then the sed will look for
the actual character)? I recall using regex with grep -E and not escaping
the curly brackets and it working... ? (on a Sun Solaris box).
| |
| Michael Tosch 2007-02-22, 7:15 pm |
| Ben H wrote:
....
> Thanks. Yes, it should have been {49} and not {50} now I think about it.
> But I thought /g at the end of a sed s meant to repeat the action for
> every line in the file? (which is what I want to do). I was aware that
> sed uses reg ex, but I thought the backslash was to escape the character
> after it, i.e. take that character literally, and as curly brackets are
> a special character, I thought that escaping them meant that sed would
> look for actual curly brackets in the file (same is true of the { }
> brackets). Is it the other way round (i.e. if you don't escape them then
> the sed will look for the actual character)? I recall using regex with
> grep -E and not escaping the curly brackets and it working... ? (on a
> Sun Solaris box).
>
sed defaults to do the action(s) for every line.
/g repeats the substitute action within a line. s/X// removes the first
X in every line in the entire file. s/X//g removes all X from the entire
file.
RE: { } ( ) are regular chars and \{ \} \( \) are special
ERE is the other way round.
Moreover, only ERE has special chars + and |
You have a comprehensive manual on most Unix systems:
man regexp
man re_syntax
--
Michael Tosch @ hp : com
|
|
|
|
|