This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Replace a word with multiple lines using sed?
(11 answers)
Closed 4 years ago.
I've got a command that I run that outputs to stdio that I need to redirect to an existing file, replacing the word replaceme with the two lines output by my command.
The command mycommand outputs like this:
somedata=this
somedata=that
I'm able to get the output into an environment variable:
export TWOLINES=$(mycommand)
When I echo it back out it comes out as a single line
echo $TWOLINES
Returns:
somedata1=this somedata2=that
I need to get this content into my file as two lines
Attempting to do:
sed -e "s,replaceme,${TWOLINES}," -i file
Returns: unterminated 's' command
Thoughts or other tools besides sed that could assist?
Related
This question already has answers here:
Shell script - search and replace text in multiple files using a list of strings
(5 answers)
Difference between single and double quotes in Bash
(7 answers)
When to wrap quotes around a shell variable?
(5 answers)
How do I use shell variables in an awk script?
(7 answers)
Closed 4 months ago.
I tried to find and replace certain strings in text file and the replace text contains "/" and sed commond did not work.
then I tried with awk command and it works fine with terminal with fixed variables.
but when I tried to use it with parameters in the loop it dose not work.
I have attached the code that I have write so far. could you please find me a solution.
this works find in terminal
awk '{sub(/input_image/,"https://www.abcd.com/images/XPDDL_R1_20161007.jpg")}1' format2.svg > format2.html
x=1
for param in ${paramO[#]}
do
awk '{sub(/${param}/,"${paramN[x]}")}1' $output_file > temp.txt
cp -rp temp.txt $output_file
let x=x+1
done
This question already has answers here:
Concise and portable "join" on the Unix command-line
(10 answers)
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 2 years ago.
I am trying to read a text file which contains the following sample values:
Apple
Banana
Carrot
...I will then need write them to a variable as a single line delimited by comma.
var1=Apple,Banana,Carrot
How does one do it using bash script? Thank you in advance.
Using sed:
sed -rn 's/\n/;/g;s/^/var1=/p' file
Consume the file as one line (-z). Substitute every newline for ";" and then replace the start of the line with "var1="
This question already has an answer here:
How to delete a line by passing line number as variable?
(1 answer)
Closed 3 years ago.
The line is specicied by the user so I have the number of the line in a variable
sed $input 'd' file.txt > file.txt
The problem here is that I don't know where and how to put the variable $input. I have tried lot's of combinations and there are all wrong.
I know that if I put a single integer it works but I don't know the way with a variable
sed -i "${input}d" file.txt
The variable needs to be in braces, use double quotes to prevent matching problems, and use the -i switch to act directly on the file
This question already has answers here:
Use a variable's value in a sed command [duplicate]
(6 answers)
sed substitution with Bash variables
(6 answers)
Closed 4 years ago.
i am just trying to append a line from my bash script:
sed -i '$ a TEXT' "filename"
but when TEXT is a variable it does not work
sed -i '$ a $VARIABLE' "filename"
i have tested endless iterations with ',''," before/after $ and ${}, escape with \ etc., but i always get a literal $VARIABLE in my file
ps. please do not propose to use echo:)
This question already has answers here:
Replace a string in shell script using a variable
(12 answers)
Closed 7 years ago.
I want to use sed command in a loop passing a variable say a such that it searches for a and in the line it gets a it replaces "true" to "false".
I have a text file containing 3000 different names and another xml file containing 15000 lines. in the lines in which these 3000 entries are there i need to make changes.
I have written a code snippet but that is not giving expected output. Can anyone help. Thanks in advance.
for i in {1..3000}; do
a=`awk NR==$i'{print $1}' names.txt`
# echo $a
sed -e '/$\a/ s/true/false/' abc.xml > abc_new.xml
done
You have to replace single-quotes(') around sed's parameters with double-quotes("). In bash, single-quote won't allow variable expansion. Also, you might want to use sed's in-place edit (pass -i option) in your for loop.
So the one liner script will look like:
for a in `cat names.txt`; do sed -i.bak -e "/$a/s/true/false/" abc.xml ; done