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:
But I get the following error:
Can anybody help with this please ?
Cheers
Last edited by Scrutinizer; 05-14-2013 at 08:21 PM..
Reason: code tags
Usually awk would be be more suited for this:
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:
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...
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
Within my read line loop I actually did:
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.
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
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)