Anyone still using 'sed'? I need guidance

rom90125

Banned
Banned
I'm a windows-based programmer who has recently been given the task of adding some simple functionality to a sister company product that is written in C and running on Fedore Project. My task is to modify the contents of a text file.

The data in the file is structured so that each unique value is delimited by the character |; as such:

value1|value2|value3|value4|value5|value6

it is possible that zero to all of the values could be null, in which case the data could look like this:
value1||value3|value4|value5|

The question is this: Using sed, is there a way to replace data between two specific delimiters in the string? For example, I want to completely replace the substring 'value4' with the string 'hereWeGo', as such:

value1|value2|value3|hereWeGo|value5|value6

or in the case of there being no data is this place, add it. See below;
BEFORE -> value1||value3||value5|value6
AFTER -> value1||value3|hereWeGo|value5|value6
 

log in or register to remove this ad

Vascant

Wanderer of the Underdark
It's been about.. well to long since I have even touched it so I can't say how but I can say what to do..lol

There is a substitution in sed and if I recall it is s/ and !s/ (depending on if you are looking for something to be true or false)

So basically you are looking for something like s/|[a-z A-Z 0-9]|| /NewValue2

Don't quote me just trying to give an idea, perhaps the only thing worse then my Sed is regular expressions.. *chuckles*
 

rom90125

Banned
Banned
Thanks Vas!

I was familar with the substitution command, but I never thought to embed the actual delimiters into the search string...

Once I've worn the rugrats out (or is it the other way around) and shuttled them to bed, I'll load up my linux distro and try this out.

Thanks again my friend.
 

azhrei_fje

First Post
Does it have to be sed? The reason I ask is that there is no way to guarantee that you're changing the 4th field.

For example, the following command (run at the shell prompt):

Code:
sed -e 's:old value:new value:'  < input  > output
Would read input and change any occurrence of 'old value' with 'new value'. But there's no way to specifically check for field 4.

Since you're going to be running this on Fedora, I would suggest using either awk or perl instead. They can count the fields for you and you can be sure of replacing just a particular field.

For example, to change 'old value' to 'new value' only in field 4, use one of the following:

Code:
awk F='|' '$4 == "old value" { $4 = "new value"; print }'  < input  > output
or

Code:
perl -pe 'split("|"); $F[3] =~ s:old value:new value:; print join("|", @F);'  < input  > output
If you want to put those into files and make them scripts, use these:

Code:
#!/usr/bin/awk -f
BEGIN { FS = "|" }
$4 == "old value" { $4 = "new value"; print }
or

Code:
#!/usr/bin/perl
while (<>) {
      split("|");
      $F[3] =~ s:old value:old value:;
      print join("|", @F);
}
After making the files executable, you can just run them and redirect the input and output (or use this in pipes or whatever, just like with grep, for example):

Code:
awkscript  < input  > output
or

Code:
perlscript  < input   > output
Good luck! You can PM me if you want more details. (I teach a bunch of Unix/Linux classes, including shell scripting and various programming classes. I'd be glad to help.)
 

rom90125

Banned
Banned
My man, I can not thank you enough!

I had allowed my brain to get so wrapped up in solving the issue with Sed, I never considered Perl (although I had looked at awk in passing). It never hurts to have a second (or third) pair of eyes looking at the problem, especially when there is more real-world experience right around the corner :)

Thanks again. If I have any questions, I'll PM you.
 

Remove ads

AD6_gamerati_skyscraper

Remove ads

Recent & Upcoming Releases

Top