Using sed to change values after a specific string

Hello I have a script that searches a file for a specific string and then changes the nth column after that string. I have searched online for how to do this with sed but have not seemed to find a solution that works for me. I am using bash.

Some background info:

  • Currently I am using awk to do this successfully.
  • I was informed that sed with --in place set would be faster

the awk command I use is as follows

site='SITES'
mx=50
awk -v n="$site" -v a="$mx" '$1==n{$2=a}{print $0}' FS=' ' OFS=' ' $FILENAME &> tmp && mv tmp $FILENAME

What this does is finds the line where 'SITES' is written in the file (a label for that row) then changes the second element (column) of that row to mx (which is 50).

  1. I want to know if its true that using sed will be faster (considering this is looped for thousands of files)
  2. Also want to know how to implement with sed if thats the case. So far I have tried (and failed) the following:
sed -i {$site}'/s/\S\+/'"${mx}"'/2' $FILENAME

I think there might be a misconception that sed is faster and from what I researched awk is the better option. The person who told me sed is faster has much more experience than me but I have a feeling he is incorrect.

What does "nth value of that string" mean when n is set to "SITES"?

sed is leaner than awk , but not THAT much faster. And, it's not that good at taking parameters. Try

sed -r 's/^(SITES )[^ ]* /\150 /' file

If there are no leading spaces (that your awk code permits) and no further words on the line, you can try

sed "/^$site / s/ .*/ $mx/" $FILENAME &> tmp && mv tmp $FILENAME

Otherwise you can take the RudiC solution (with optional space at the beginning, but without the extra space at the end)

sed -r "s/^( *$site +)[^ ]*/\1$mx/"

or with a Unix sed

sed "s/^\( *$site  *\)[^ ]*/\1$mx/"

Note that MadeInGermany's suggestion only works if there are only two fields on lines that start with SITES . It will replace the 2nd and any following fields the way it is currently written.

I will assume you're using bash as your shell (since cmd &> tmp runs cmd in the background and creates an empty file named tmp on most other shells).

And, I'll assume you're using GNU sed (since the -i option is one of the GNU extensions to sed). I'm not a fan of the -i option to sed (especially when a backup file option-argument is not supplied). But, I think you'll get a syntax error for a mismatched closing parenthesis on the command you showed us.

Assuming you wanted to use $site as an option-argument to the -i option, you might try:

sed --posix -i $site '/s/\("$site"'[[:space:]]+\)[^[:space:]]+/\1'"${mx}/" $FILENAME

This is untested, and I don't have a GNU sed available to test it. I think you'll need the --posix to get the RE and replacement processing in the substitute command to work correctly and I don't think that will disable the -i option, but the man page doesn't give nearly enough information for me to determine that.

On most systems sed will be faster than awk for small jobs because it is smaller and takes less time to exec. And, if you're running it thousands of time, the difference may matter. You'll have to run some performance tests to see which is faster on your configuration.

Note that this sed command will leave backup files laying around whether or not the command was successful and may leave the source file in an undefined state if an error was encountered (but you'll be able to manually restore it from the backup file) while your awk script with the mv command will get rid of tmp if the command completes successfully and leave the source file unchanged if it failed.

1 Like

I think for what I am trying to do it is safer and simpler to use awk. when trying to implement with sed it appears to be more complicated and error prone especially when there are multiple columns of information on a line I want changed. I don't think the marginal increase in speed is worth it. Thanks to all those who replied.