sed replacement in file when line is in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed replacement in file when line is in a variable
# 1  
Old 05-14-2013
sed replacement in file when line is in a variable

Hi,

I have a file where I want to replace the 15th field separated by comma, only on specific lines matching lots of different conditions.

I have managed to read the file line by line, within the loop my line is held in a variable called $line

I assume this will be using sed (maybe incorrectly), but how do I replace the 15th field in my comma separated, condition matching line which is now contained within the $line variable and write it back into the file (not a new file), replacing the original line with the new one ?

Let's say in this example I want to replace 15th field seperated by comma value "APPLE" with "BANANA"

Many thanks in advance for any answers.

---------- Post updated at 06:16 PM ---------- Previous update was at 05:41 PM ----------

Ok, I'm nearly there.

I now have my amended line in $NEWLINE and my current un-amended line is in $line

So I have now tried writing it back to the same file using:

Code:
sed -i '/'$line'/'$NEWLINE'/g'

But I get the following error:

Code:
sed: -e expression #1, char 8: unknown command: `0'


Can anybody help with this please ?

Cheers

Last edited by Scrutinizer; 05-14-2013 at 08:21 PM.. Reason: code tags
# 2  
Old 05-14-2013
Usually awk would be be more suited for this:
Code:
awk '$15==s{$15=v}1' s="APPLE" v="BANANA" FS=, OFS=, file

But that would not do in-place editing, however you can easily write to a new file and if all is well replace the original with the new file.
If that is not what you are after, please specify input, and desire output as well as OS and version..

Last edited by Scrutinizer; 05-17-2013 at 05:27 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 05-17-2013
Many thanks Scrutinizer.

I did end up using your suggestion.

Although it placed the value I wanted to replace the 15th field with into the 14th field, leaving the old value in the 15th field also.

No probs though, changed it to 15 and all is good in the world.
# 4  
Old 05-17-2013
You're welcome... Tes no idea why I put 14 there. I edited it in my post
# 5  
Old 05-17-2013
Both your sed command (once it is corrected) and Scrutinizer's command will replace your "$oldvalue" with "$newvalue" in the ENTIRE file, not only in the lines "matching lots of different conditions". It'd be better to reconsider, and post the conditions you impose, so they can be put into the awk (or whatever) program...
# 6  
Old 05-24-2013
Quote:
Originally Posted by RudiC
Both your sed command (once it is corrected) and Scrutinizer's command will replace your "$oldvalue" with "$newvalue" in the ENTIRE file, not only in the lines "matching lots of different conditions". It'd be better to reconsider, and post the conditions you impose, so they can be put into the awk (or whatever) program...
Thanks RudiC,

I've already amended the files.

However, if true, I struck it lucky, as when I was finally given the files to amend only the lines I wanted to change had a 15th field Smilie



Within my read line loop I actually did:

Code:
echo $line | awk '$15==s{$15=v}1' s="1" v="0" FS=, OFS=, >> $a.tmp

Would this have meant that I would have avoided the pitfall you mention as I have piped the $line specifically through my awk command ?

---------- Post updated at 02:19 PM ---------- Previous update was at 02:06 PM ----------

Ok, I've tested it (I should have just done that instead of asking so sorry bout that).

I added 20 more fields to a line I didn't want to change.

Code:
echo $line | awk '$15==s{$15=v}1' s="1" v="0" FS=, OFS=, >> $a.tmp

only changes the $line and leaves the value in the 15th field on a line I do not want to change intact.


Happy days.

Last edited by Scrutinizer; 05-24-2013 at 04:21 PM.. Reason: code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacement of variable by their content in a file

Dear all, I have a "SQL request" in a file: that request include different "host variable" and I would like to substitute the different "host variable" by their respective content before executing the request. For example: $ echo $SHELL /bin/bash $ cat dae2.txt DELETE FROM ... (11 Replies)
Discussion started by: dae
11 Replies

2. Shell Programming and Scripting

Multiple Replacement in a Text File in one operation (sed/awk) ?

Hi all, Saying we have two files: 1. A "Reference File" whose content is "Variable Name": "Variable Value" 2. A "Model File" whose content is a model program in which I want to substitute "VariableName" with their respective value to produce a third file "Program File" which would be a... (4 Replies)
Discussion started by: dae
4 Replies

3. Shell Programming and Scripting

Sed: how to use file contents in replacement string

I want to replace a string by contents of file. I am trying the following sed command: cat sample | sed "s^<enter description here>^`cat details`^" But it is not working. a=`cat details` and using $a will not help since it will affect the whitespaces. What am I missing in the above sed... (5 Replies)
Discussion started by: anand_bh
5 Replies

4. Shell Programming and Scripting

sed - replacement file path with variable - Escaping / character

Hi,, I have the line below in a file: $!VarSet |LFDSFN1| = '"E:\APC\Trials\20140705_427_Prototype Trial\Data\T4_20140705_Trial_Cycle_Data_13_T_Norm.txt" "VERSION=100 FILEEXT=\"*.txt\" FILEDESC=\"General Text\" "+""+"TITLE{SEARCH=NONE NAME=\"New Dataset\" LINE=1I want to write a script to change... (2 Replies)
Discussion started by: carlr
2 Replies

5. Shell Programming and Scripting

Sed:- Supported variable replacement after string match?

Hi All, I am trying to replace the variable in the file after the particular match string. It is being replaced if i hardcode the value and with use of "&" with sed. sed -e "s/URL./& http:\\localhost:7223/g" But when am trying to pass the variable it is failing. I tried multiple... (9 Replies)
Discussion started by: sharsour
9 Replies

6. Shell Programming and Scripting

HELP Need in SED/PERL conditional line replacement

Hi , I need some help on perl/sed conditional replacement The situation is like below . I have a file contents like below . AAA|BBB|CCC|DDD AAA|BCF|CCC|HHH AAA|BVF|JJJ|KKK Here in the above file . I know my second column value (taking "|" as my delimited ) Basically I have to... (3 Replies)
Discussion started by: robin.r888
3 Replies

7. Shell Programming and Scripting

sed xml file multiple line replacement

I have a file called config.xml, it's a simple xml file, and I need use sed/awk to erase some lines. <machine xsi:type="unix-machineType"> <name>server1</name> <node-manager> <name>server1</name> <listen-address>server1</listen-address> </node-manager> ... (3 Replies)
Discussion started by: cbo0485
3 Replies

8. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

9. Shell Programming and Scripting

sed replacement in unicode file

Hi there, I have a file generated by a windows registry (it's unicode) and can't get to do some replacements on it. I want to join lines that end with backslash with the next one. santiago@ks354286:~$ cat win.reg ÿþWindows Registry Editor Version 5.00 ... (10 Replies)
Discussion started by: chebarbudo
10 Replies

10. Shell Programming and Scripting

Sed - Appending a line with a variable on it

Hi, I searched the forum for this but couldn't find the answer. Basically I have a line of code I want to insert into a file using sed. The line of code is basically something like "address=1.1.1.1" where 1.1.1.1 is an IP Address that will vary depending on what the user enters. I'll just refer... (4 Replies)
Discussion started by: eltinator
4 Replies
Login or Register to Ask a Question