Sponsored Content
Full Discussion: Help with sed substitution
Top Forums UNIX for Dummies Questions & Answers Help with sed substitution Post 302379844 by Scrutinizer on Saturday 12th of December 2009 03:32:03 PM
Old 12-12-2009
Hi, you could combine a function and a sed statement to create a way of changing a field value based on a field name. E.g.

Code:
replace_field() 
{
  sed "/^$1/s/\(${2%=*}=\)[^|]*/\1${2#*=}/" infile;
}
replace_field "title=Boston" "desc=bla bla"

Code:
output:
title=Boston|tcolor=green|desc=bla bla|url=http://www.boston.com
title=New York|tcolor=gold|desc=The largest city on the East Coast|url=http://www.newyork.com
title=Dallas|tcolor=silver|desc=A sprawling, cheery Texas city|url=http://www.dallas.com

This probably looks quite cryptic but it is based on the previous examples with the values replaced by $2 (and parameter expansions). The function takes the first key to locate the line on which certain fields need to be replaced.

We can expand this to take more fields and replace the $2 expansions with field names so it becomes a bit more readable..
Code:
replace_fields() 
{
  key="$1"; shift
  sedstr="/^$key/{"
  while [ $# -gt 0 ]; do
       field="${1%=*}"
       val="${1#*=}"
       sedstr="${sedstr}s/\b\($field=\)[^|]*/\1$val/;"
       shift
  done
  sedstr="$sedstr}"
  sed "$sedstr" infile
}

replace_fields "title=New York" "tcolor=green" "desc=blabla"

Output:
Code:
title=Boston|tcolor=green|desc=Large city in New England|url=http://www.boston.com
title=New York|tcolor=green|desc=blabla|url=http://www.newyork.com
title=Dallas|tcolor=silver|desc=A sprawling, cheery Texas city|url=http://www.dallas.com

If you want to edit the file directly
you could use:
Code:
sed -i "$sedstr" infile

if your sed supports it or
Code:
sed "$sedstr" infile > /var/tmp/infile.tmp.$$ && mv /var/tmp/infile.tmp.$$ infile

It builds a sed statement based on the inout fields that are being provided. Unfortunately I haven't been able to make it simpler than this.

Also, it has to be said that this will probably only work in very low intensity situations. If more users will be creating updates simultaneously then problems may occur and you will have to create some sort of locking-and-wait mechanism. If it is getting more serious you will probably be better off using a simple database.

Last edited by Scrutinizer; 12-12-2009 at 04:49 PM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Substitution using sed

I know we can substitute a string using sed but how? For example: sed 's/(old variable)/(new variable)/ details.dat Am I suppose to put $old variable or whatever? Because I tried many times, it didnt work by putting $old variable. Am I suppose to enclose it with "" or ''? Please help (3 Replies)
Discussion started by: Ohji
3 Replies

2. UNIX for Dummies Questions & Answers

sed substitution

Hi, I have a set of files containing strings like I.TEST1_TEST2 or B.ESSA_ESSB for example. Does somebody know how to substitute these strings whith the same name and an extension "_V1" (ie. I.TEST1_TEST2_V1) using sed command or else ? (3 Replies)
Discussion started by: jo_aze
3 Replies

3. Shell Programming and Scripting

Substitution using SED

Hi , I am stuck up in the below scenario:- I need to read a file name (eg A.txt) name frm another file (eg B.txt) and then I need to search for a particular expression in A.txt and substitute it with another expression. How can I use SED inside SHELL Scripting and command prompt as... (2 Replies)
Discussion started by: shubhranshu
2 Replies

4. Shell Programming and Scripting

SED Substitution

Hi , I am stuck up in the below scenario:- I need to read a file name (eg A.txt) name frm another file (eg B.txt) and then I need to search for a particular expression in A.txt and substitute it with another expression. How can I use SED inside SHELL Scripting and command prompt as well to... (1 Reply)
Discussion started by: shubhranshu
1 Replies

5. Shell Programming and Scripting

SED Substitution

Hi guys, Can u please help me to replace (-) with (/) in a file containing no of records using "sed " command in unix. thanks in advance. subhendu (5 Replies)
Discussion started by: subhendu81
5 Replies

6. Shell Programming and Scripting

sed substitution

Hi I am trying to do a text insertion in a text file at a particular line number in a shell script. However its not working. sed '122i\ > for j in \`echo $MyList\` ; do perl -pi -e\'s#01\/01\/2009#01\/01\/2011#\' $j ; done' $HOME/MyScript.ksh The Actual line to be inserted at line 122... (5 Replies)
Discussion started by: som.nitk
5 Replies

7. Shell Programming and Scripting

Substitution with sed

I have a file with some numbers having single quotes around them which I want to remove. i.e. '923930' -> 23930 If it can be done without using sed thats fine. I have tried with sed but can't think how to replace this pattern on only the numbers (13 Replies)
Discussion started by: user_invalid
13 Replies

8. Shell Programming and Scripting

sed substitution

Hello, I have two files. File1 is normal txt file and File2 contains list of line numbers. e.g. File2: 3 6 9 ..... I need to replace a character in File1 in lines (taken from File2). For that I am using a "for" loop: for i in $(cat File2) do sed "$i s/Y/N/" File1 done but my... (3 Replies)
Discussion started by: shekhar2010us
3 Replies

9. UNIX for Dummies Questions & Answers

sed substitution

How can you use sed with a line of code that reads: 67899:Bill:Williams:Maple Dr.:45908600 Let us say we want to replace Maple Dr. with Oak St. (1 Reply)
Discussion started by: yonkers062986
1 Replies

10. Shell Programming and Scripting

sed substitution

Hi everyone, I need very simple sed command to change a parameter in a text file. I have a line in this text which is like set xx 0.5 A program reads this file and does some algebraic calculations. So to make a parameter scan I need to change the value of xx. I thought I can do... (7 Replies)
Discussion started by: hayreter
7 Replies
All times are GMT -4. The time now is 04:45 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy