Re: modifying text using sed
am 16.11.2007 16:54:28 von jellybean stonerfish
On Thu, 15 Nov 2007 18:41:27 -0600, Moe Trin wrote:
This thread started in alt.os.linux
> The second way would also use addresses, but
>
> sed '/alpha/,/FOOBAR/ s/linux/LINUX/' file
>
> which says to start replacing on the line that contains the string 'alpha'
> and continue until you hit the line that contains the string 'FOOBAR' (or
> if you never find that line, continue to the end).
Can sed be made to work on parts of lines? A file has lines like this,
and I want to remove all of the '-', that fall between '&' and ';'
a-111 b-222 c-333 & d-444 e-444 ; f-555 g-666 & h-777 i-888 ; j-999
would become
a-111 b-222 c-333 & d444 e444 ; f-555 g-666 & h777 i888 ; j-999
stonerfish
Re: modifying text using sed
am 16.11.2007 17:34:41 von Ed Morton
On 11/16/2007 9:54 AM, jellybean stonerfish wrote:
> On Thu, 15 Nov 2007 18:41:27 -0600, Moe Trin wrote:
>
> This thread started in alt.os.linux
>
>
>
>>The second way would also use addresses, but
>>
>> sed '/alpha/,/FOOBAR/ s/linux/LINUX/' file
>>
>>which says to start replacing on the line that contains the string 'alpha'
>>and continue until you hit the line that contains the string 'FOOBAR' (or
>>if you never find that line, continue to the end).
>
>
> Can sed be made to work on parts of lines? A file has lines like this,
> and I want to remove all of the '-', that fall between '&' and ';'
>
> a-111 b-222 c-333 & d-444 e-444 ; f-555 g-666 & h-777 i-888 ; j-999
>
> would become
>
> a-111 b-222 c-333 & d444 e444 ; f-555 g-666 & h777 i888 ; j-999
>
> stonerfish
Like I said in your other recent thread, don't use sed for anything other than
simple substitutions. For the above input, this awk script is all you need:
BEGIN{FS=OFS=";"}
{
for (i=1;i
split($i,a,"&")
gsub(/-/,"",a[2])
$i=a[1] "&" a[2]
}
}
1
or, if your input has more variance than the one line you showed, maybe this:
BEGIN{FS=OFS=";"}
{
for (i=1;i<=NF;i++) {
if (split($i,a,"&") == 2) {
gsub(/-/,"",a[2])
$i=a[1] "&" a[2]
}
}
}
1
In either case, put the above in a file called whatever.awk and invoke it as
awk -f whatever.awk inputfile.
Ed.