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
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:
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..
If there are no leading spaces (that your awk code permits) and no further words on the line, you can try
Otherwise you can take the RudiC solution (with optional space at the beginning, but without the extra space at the end)
or with a Unix sed
Last edited by MadeInGermany; 10-21-2015 at 03:16 PM..
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:
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:
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)