I want to replace a string in linux file with a variable value using Shell Script:
Tried doing this in shell script, but not working.
sed -i "s/$StrVal1/$StrVal2/" "$TargetFile"
Please help.
I think you forgot the "g" in the sed command. Works for me on my linux shell. You can also cat the file and then run the sed command.
First I define variables as shown below, on the prompt:
a='class'; b='CLASS'
Then I cat the file with a sed command:
cat a.txt | sed "s/$a/$b/g"
So my input file is like
class="pending">Count
class="completed">Count
My output with that command is like
CLASS="pending">Count:
CLASS="completed">Count:
Related
I need to use sed instead of echo and append to the end of a file: list.txt
list.txt has a list of directories:
/desktop/test1/file1
/desktop/test2/file1
I need to append another directory with slashes to this list.txt at the end of the file if that directory already doesn't exist in the list. Such as: /desktop/file1
The result should be:
/desktop/test1/file1
/desktop/test2/file1
/desktop/file1
I've tried using this script but am running into syntax errors with the a command which I've been seeing could be a mac issue?:
#!/bin/bash
if ! grep -q "/desktop/file1" user/admin/Desktop/list.txt; then
sed -i -e '$a/desktop/file1' user/admin/Desktop/list.txt
fi
The a command is used as a\ in standard sed and should be followed by a newline and the text to be written. The form you use is a GNU extension. So a standard sed command to do the job could be:
sed -i '' '$a\
/desktop/file1' user/admin/Desktop/list.txt
or
sed -i '' '$a\'$'\n''/desktop/file1' user/admin/Desktop/list.txt
using ANSI-C quoting in bash.
I am using sed command in a shell script to edit & replace some file content of an xml file in a osx(unix) environment. Is there some way I can avoid sed creating the temporary files with -e ? I am doing the following in my script to edit a file with sed.
sed -i -e 's/abc/xyz/g' /PathTo/MyEditableFile.xml
Above sed command works great but creates an extra file as /PathTo/MyEditableFile.xml-e
How can I avoid creation of the the extra file with -e there ?
I tried some options like setting a temporary folder path to sed so that it creates the temporary file in /tmp/. Like so:
sed -i -e 's/abc/xyz/g' /PathTo/MyEditableFile.xml >/tmp
But doesnt seem to work
As you are editing the file in place (-i), OS X sed requires a mandatory backup filename.
You can use -i "" to get around this:
sed -i "" -e 's/abc/xyz/g' /PathTo/MyEditableFile.xml
sed -i '$a\curl -s http://whatismyip.org/' file
Trying to find a way to pull the WAN IP and insert it into the last line of a file as illustrated above (not working of course). This will be utilized via command line.
sed -i '$a\test' file
This will insert "test" after the last line in "file" as utlilized but how could I output the result of a function or command in it's place within Sed's syntax? Any suggests (awk, perl, bash script?) are welcome!
sed isn't required here. Just use this:
curl -s http://whatsmyip.org >> your.file
Note that bash supports the >> redirection operator which appends a program's output to a file
hek2mgl has shown you how to solve this specific problem. To address the more general question, you can do:
var=$(some command line)
This sets the shell variable $var to the output of the command. Then you can subsitute this into sed with:
sed -i "\$a\\$var" file
I am a new bie to shell scripting, here i am trying to find the text and replace the text using the shell script.
what i am trying to do is, actually i have a text file which has 2 strings separated by ":
"
Like this
lorem:ipsum
dola:meru
etc....
my script will take 2 parameters while running.
now the script should check if first parameter is found or not if not found it should add it to text file.
if the first parameter is found, then it should replace the second parameter.
for example
The text file has data like this
lorem:ipsum
dola:meru
caby:cemu
i am running my script with 2 parameters like this
./script.sh lorem meru
So when i run the script it should check if the first parameter found in the file if found, the script should replace the second string..
i.e
I ran the script like this
./script.sh lorem meru
so in the file
lorem:ipsum
dola:meru
caby:cemu
after running the script, in the line
lorem:ipsum
should get replaced to
lorem:meru
here is what i have tried..
#!/bin/sh
#
FILE_PATH=/home/script
FILE_NAME=$FILE_PATH/new.txt
echo $1
echo $2
if [] then
else
echo $1:$2 >> $FILE_NAME
fi
Using sed might be simpler.
$ cat inputfile
lorem:ipsum
dola:meru
caby:cemu
$ pattern="lorem"
$ word="meru"
$ sed "/^$pattern:/ s/:.*/:$word/" inputfile
lorem:meru
dola:meru
caby:cemu
try this line in your script:
awk -F: -v OFS=":" -v s="$1" -v r="$2" '$1==s{$2=r}7' file > newFile
You can try this as well,
input file : new.txt
lorem:ipsum
dola:meru
caby:cemu
Script file: script.sh
#!/bin/sh
sed -i "s/$1:.*/$1:$2/" new.txt
if you run the script as u wished "./script.sh lorum meru"
Output: new.txt, will be displayed as
lorem:meru
dola:meru
caby:cemu
Explanation:
sed is a powerful text processing tool, where u can manipulate the text as you wish with the options it provides.
Brief explanation of the code,
sed -i > used to replace the contents of the original file with the new changes ( if u dont give -i in sed it will just print the modified contents without affecting the original file)
"s/old:.*/old:new/"
"s" is used for substitution. Syntax is "s/old/new/'.
Here * specifies anything that is present after the ":"
So by executing this you will get the desired output.
I have file File1.txt in which i have to replace a text using sed command
File1.txt contents
EURAMOUNTTOBEREPLACED
I have a AIX shell script for replacing the text AMOUNTTOBEREPLACED
Contents of the shell script
sum=27
sed 's/AMOUNTTOBEREPLACED/"$sum"/g' File1.txt >> temp
mv temp FileNew.txt
After executing the script, the contents of FileNew.txt is as below
EUR"$sum"
But Expected output should be
EUR27
Please help how to do?
I think the one you want is like this:
sed 's/AMOUNTTOBEREPLACED/'$sum'/g' File1.txt >> temp
Basically single qouting takes the string to sed and skips shell which is wrong. You want the shell to interpret the variable so thats what i did. And further if you happen to have gnu version of sed. Then you can do
sed -i 's/AMOUNTTOBEREPLACED/'$sum'/g' File1.txt
which compressed these two statement in your code to one in above:
sed 's/AMOUNTTOBEREPLACED/'$sum'/g' File1.txt >> temp
mv temp FileNew.txt
use the below sed command instead:
sed -e "s/AMOUNTTOBEREPLACED/\"${sum}\"/g"