I am writing a script in bash and I can remove a string from a sentence using the code below: echo 'This is a foo test' | sed 's/\&lt;foo\&gt;//g'   OUTPUT: 'This is a test'  However, I want to be able to replace the foo with a variable. I tried the command below but it does not remove the values from the sentence. temp=&quot;foo&quot; echo 'This is a foo test' | sed 's/\&lt;$temp\&gt;//g'   OUTPUT: 'This is a foo test'
19480
I tried the following code to replace QQ with ZZ, but it doesn't do what I want:  var1=QQ sed -i 's/$var1/ZZ/g' $file   However, this code does what I want:  sed -i 's/QQ/ZZ/g' $file   How do I use variables in sed?
83121
I am trying to replace multiple words in a file by using   sed -i #expression1 #expression2   file  Something  123 item1 Something  456 item2 Something  768 item3 Something  353 item4   Output (Desired)  anything  123 stuff1 anything  456 stuff2 anything  768 stuff3 anything  353 stuff4   Try-outs  I can get the following output by using sed -i two times.   sed -i 's/Some/any/g' file  sed -i 's/item/stuff/g' file   Can I have any possible way of making this as a single in-place command like   sed -i 's/Some/any/g' -i 's/item/stuff/g' file   When I tried the above code it takes s/item/stuff/g as a file and tries working on it.
42231
One of my coworkers has provided me with a Bash syntax that I am unfamiliar with.  My Google foo has failed me on figuring out what it does and why/when I should use it.    The command that he sent me was of this form:  someVariable=something command   Initially, I thought that this was equivalent to the following:   someVariable=something ; command   Or  someVariable=something  command   But this doesn't appear to the be case.  Examples:   [Jan-03 11:26][~]$ # Look at the environment variable BAZ. It is currently empty [Jan-03 11:26][~]$ echo $BAZ  [Jan-03 11:27][~]$ # Try running a command of the same format     [Jan-03 11:27][~]$ BAZ=jake echo $BAZ  [Jan-03 11:27][~]$     [Jan-03 11:27][~]$ # Now, echo BAZ again. It is still empty:     [Jan-03 11:27][~]$ echo $BAZ  [Jan-03 11:27][~]$     [Jan-03 11:28][~]$     [Jan-03 11:28][~]$ # If we add a semi-colon to the command, we get dramatically different results: [Jan-03 11:28][~]$ BAZ=jake ; echo $BAZ jake [Jan-03 11:28][~]$ [Jan-03 11:28][~]$ # And we can see that the variable is actually set: [Jan-03 11:29][~]$ echo $BAZ jake [Jan-03 11:29][~]$   What does this syntax do?  What happens to the variable that has been set?  Why does this work?
15365
In Perl, what is a good way to perform a replacement on a string using a regular expression and store the value in a different variable, without changing the original?  I usually just copy the string to a new variable then bind it to the s/// regex that does the replacement on the new string, but I was wondering if there is a better way to do this?  $newstring = $oldstring; $newstring =~ s/foo/bar/g;
65627
It looks like 'find', 'bash' and 'sed' in some cases does not work as one expects.  The following example should first create file 'sample.txt', then find the file and finally process it by '-exec' command. The executed command prints found filename, test specimens, and modified filename. The 'sed' command itself is used to replace 'txt' to 'TXT'.  touch sample.txt find ./ -maxdepth 1 -name "*.txt" -exec echo {} $(echo Specimen_before.txt {} Specimen_after.txt |sed -e "s/txt/TXT/g") \;   The expected output is:     ./sample.txt Specimen_before.TXT ./sample.TXT Specimen_after.TXT   Instead it produces:     ./sample.txt Specimen_before.TXT ./sample.txt Specimen_after.TXT   (the example has been tested also with old-school command substitution through backquotes '`' with the same result)  What am I doing wrong?
45221
&gt; echo "hi" hi &gt; VAR='echo "hi"' &gt; $VAR "hi"   Why is the output of the above commands different?  A similar thing occurs with single quotes:  &gt; VAR="echo 'hi'" &gt; $VAR &gt; 'hi'
77756
I am trying to write a bash script which run a command and compare the result with another string.   #!/bin/bash -x  STATUS=`/root/setup_ha show --password-file=/root/password | grep "&gt;HA State" | awk '{print $3}' |  cut -c 2-`  TEST=`echo $STATUS` if [[ "$TEST" == "ON Master" ]]; then echo CLUSTER CRITICAL else   echo CLUSTER OK MASTER fi   As the Original string is on two lines, I echo it in a new variable TEST. The New variable have the output of the command on one line.  Here is the Bash debug output :  ++ /root/setup_ha show --password-file=/root/password ++ grep '&gt;HA State' ++ awk '{print $3}' ++ cut -c 2- + STATUS='ON Master' ++ echo 'ON' Master + TEST='ON Master' + [[ ON Master == \O\N\ \M\a\s\t\e\r ]] + echo CLUSTER OK MASTER CLUSTER OK MASTER   I also tried the following test :  if [[ "$TEST" =~ "ON Master" ]]   The thing is bash is not able to compare the strings it is always false.  Any idea ?   EDIT :  Here is the output with the first answer :  + STATUS='ON Master' ++ echo 'ON' Master + TEST='ON Master' + '[' 'ON Master' == 'ON Master' ']' + echo CLUSTER OK MASTER CLUSTER OK MASTER   Still  not working ON seems weird on line 3, plus in my bash it takes green color !
44760
I need to repeatedly remove the first line from a huge text file using a bash script.  Right now I am using sed -i -e "1d" $FILE - but it takes around a minute to do the deletion.  Is there a more efficient way to accomplish this?
104860
I am trying to delete two lines ( 7 and 8) from a .txt file. For this I am using following code:-  #!/bin/sh Column="7" sed '"$Column",8d' myfile.txt &gt; result.txt   on running this script, I am getting this error:-  sed: -e expression #1, char 1: unknown command: `"'   Please tell me how to use variable as a part of sed command.
24174
I'm trying to create an interactive bash script where I can collect user input variables to be used as arguments in another command. Can someone provide me an example of this?  Currently i want to do:   what type of content you want to delete? what is the status?   then feed it into a mysql delete query command.
87408
Oh, hi; So I've got this big json file and I'm trying to find and replace a few things but when I've used sed I get an error and I need a sed or awk string to find and replace it, without getting an error.  prajwal  Current: {&quot;telephone_numbers&quot;:[&quot;1-  Future: {&quot;telephone_numbers&quot;:[&quot;+1-  This is the command i tried and the error i got. sed -i 's/{&quot;telephone_numbers&quot;:[&quot;1-/{&quot;telephone_numbers&quot;:[&quot;+1-/g' output.json sed: -e expression #1, char 56: unterminated `s' command
28663
I found this line in a korn shell script:  var=${var:-/temp}   I am able to figure it out what it is doing   $ var=/data $ var=${var:-/temp} $ echo $var /data $ var= $ echo $var  $ var=${var:-/temp} $ echo $var /temp   Does it have advantage over checking the value and then assigning?
37813
I want to split 'hello' into h e l l o in an array using only bash, I could do it in sed with sed 's/./&amp; /g' but I want to know how to split a string into an array in Bash when I do not know what the delimiter would be, or the delimiter is any single character.  I don't think I can use ${i// /} without some creativity because the delimiter is an unknown, and I don't think that expression accepts regex.  I tried using BASH_REMATCH with [[ string =~ ([a-z].).* ]] but it doesn't work as I expected.  What is the proper way to use only bash to accomplish a string.split() type of behavior?  The reason is that I am trying to write the rev utility in all bash:    while read data; do   word=($(echo $data|tr ' ' '_'|sed 's/./&amp; /g'))   new=()   i=$((${#word[@]} - 1))   while [[ $i -ge 0 ]]; do     new+=(${word[$i]})     (( i-- ))   done   echo ${new[@]}|tr -d ' '|tr '_' ' '   done   But I used tr and sed, I want to know how to do the split properly and then I will fix it to be all bash.  Just for fun.
11588
I'm trying to use Bash string substitution to combine an array. Desired output: cat,dog,fish  This works, but isn't there a way to do with without piping? #!/bin/bash  words=(cat dog fish)  echo &quot;${words[@]}&quot; | sed 's/ /,/g'  I've been trying this, which doesn't work. echo &quot;${words[@]// /,/}&quot;
85669
I have requirement that i need to extract string using quotes. My string as follows.  "abcd efgh" "ijkl mnop" "qrst uvwxyz"   can you help me to get the string between second double quotes(ijkl mnop) using sed or grep command.  In other words if I say give me the string in first quotes I want the  first string; if I say second string it should give me string between second double quotes and similarly third one also.
87431
I use sed -n '/string/,$p' file &gt;&gt; otherfile to copy text from a line with certain string to another file.  Now I want the copied text to be deleted in the original file. I tried different methods with sed and awk but nothing worked.  How to use sed to delete the text of a file from the beginning line with a certain string in it?  texta  texta1  textb    &lt;- string, delete rest of the text from here  textb1  textc  textc1   or would it be easier to cut instead of copying with sed and what would be the command instead?
38442
I have two different files:  File1  /home/user1/   /home/user2/bin   /home/user1/a/b/c   File2  &lt;TEXT1&gt; &lt;TEXT2&gt;   I want to replace the &lt;TEXT1&gt; of File2 with the contents of File1 using sed. I tried this command, but not getting proper output:  cat File2|sed "s/&lt;TEXT1&gt;/$(cat File1|sed 's/\//\\\//g'|sed 's/$/\\n/g'|tr -d "\n")/g"   You can use other tools also to solve this problem.