Using sed to change values after a specific string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sed to change values after a specific string
# 1  
Old 10-21-2015
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

Code:
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:
Code:
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.

Last edited by prodigious8; 10-21-2015 at 03:38 PM..
# 2  
Old 10-21-2015
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
Code:
sed -r 's/^(SITES )[^ ]* /\150 /' file

# 3  
Old 10-21-2015
If there are no leading spaces (that your awk code permits) and no further words on the line, you can try
Code:
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)
Code:
sed -r "s/^( *$site +)[^ ]*/\1$mx/"

or with a Unix sed
Code:
sed "s/^\( *$site  *\)[^ ]*/\1$mx/"


Last edited by MadeInGermany; 10-21-2015 at 03:16 PM..
# 4  
Old 10-21-2015
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:
Code:
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.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 10-21-2015
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find specific pattern and change some of block values using awk

Hi, Could you please help me finding a way to replace a specific value in a text block when matching a key pattern ? I got the keys and the values from a command similar to: echo -e "key01 Nvalue01-1 Nvalue01-2 Nvalue01-3\nkey02 Nvalue02-1 Nvalue02-2 Nvalue02-3 \nkey03 Nvalue03-1... (2 Replies)
Discussion started by: alex2005
2 Replies

2. Shell Programming and Scripting

awk to change specific string to new value if found in text file

I am trying to use awk to change a specific string in a field, if it is found, to another value. In the tab-delimited file the text in bold in $3 contains the string 23, which is always right before a ., if it is present. I am trying to change that string to X, keeping the formatting and the... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

Change some string in specific column with space

Hello All of Master Script , i need help to solve my problem before : mount /dev/rdsk/c1t69d0s6 /dev/rdsk/c1t69d0s6 /vol/cl123/PURGE1 ufs mount /dev/rdsk/c1t70d0s6 /dev/rdsk/c1t70d0s6 /vol/cl123/PURGE2 ufs expected : mount /dev/dsk/c1t69d0s6 /dev/rdsk/c1t69d0s6 /PURGE1 ufs mount ... (3 Replies)
Discussion started by: k0p0nkkk
3 Replies

4. Shell Programming and Scripting

Add character to specific columns using sed or awk and make it a permanent change

Hi, I am writing a shell script where I want that # should be added in all those lines as the first character where the pattern matches. file has lot of functions defined a.sh #!/bin/bash fn a { beautiful evening sunny day } fn b { } fn c { hello world .its a beautiful day ... (12 Replies)
Discussion started by: ashima jain
12 Replies

5. UNIX for Dummies Questions & Answers

How can I search and change an specific string in a file

Dear All, New to Linux/Unix OS, my Linux version is 2010 x86_64 x86_64 x86_64 GNU/Linux As titled, I wonder if you can help to provide a solution to find and change an specific string in a file The file include a lots of data in following configuration but might be various in... (3 Replies)
Discussion started by: axel
3 Replies

6. Shell Programming and Scripting

Replace blank values for string using sed

Hi everyone, I was wondering how could from a file where each row is separated by tabulations, the row values where are in blank replace them by a string or value. My file has this form: 26/01/09 13:45:00 0 0 26/01/09 14:00:00 1495.601318 0 26/01/09 14:15:00 1495.601318 0 ... (4 Replies)
Discussion started by: tonet
4 Replies

7. Shell Programming and Scripting

Change specific occurence with sed

Hello, I have this file. aaa port=1234 time bbb port=2233 name ccc port=4444 name Is there any way with sed to change only the occurence of "port" which comes after section to have as output : (12 Replies)
Discussion started by: rany1
12 Replies

8. UNIX for Dummies Questions & Answers

Using SED to change a specific word's color?

Ok so all i'm trying to do here is output a file and change the color of a specific word. I can't use grep with color because I need all lines of the file not just lines that match the pattern. I can get this substitution to work but when it displays it shows exactly what i'm putting it rather... (14 Replies)
Discussion started by: MrEddy
14 Replies

9. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

10. Shell Programming and Scripting

SED 4.1.4 - INI File Change Problem in Variables= in Specific [Sections] (Guru Help)

GNU sed version 4.1.4 on Windows XP SP3 from GnuWin32 I think that I've come across a seemingly simple text file change problem on a INI formatted file that I can't do with SED without side effects edge cases biting me. I've tried to think of various ways of doing this elegantly and quickly... (5 Replies)
Discussion started by: JakFrost
5 Replies
Login or Register to Ask a Question