Sponsored Content
Top Forums Shell Programming and Scripting Using sed to change values after a specific string Post 302958368 by Don Cragun on Wednesday 21st of October 2015 02:05:05 PM
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:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 06:19 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy