Variables within sed command - variables

I am trying to append to the first line only of a file using sed which includes a variable.
This is the command that I am trying:
data=foo
sed -i "1 s/$/$data/" myfile.csv
This is the result I am getting:
sed: -e expression #1, char 8: unknown option to `s'
(I also would like to add a comma along with the data since it is a csv..)

Related

Substituting Variable in sed command

I have ./cpptest.sh to which I am passing a command line parameter
For e.g.
$./testcps.sh /srv/repository/Software/Wind_1.0.2/
The above command line parameter, is stored in variable $1
when I echo $1, the output is correct (the path)
Actual issue...
There is another file let's say abc.properties file. In this file there is a key-value field something like location.1=stg_area.
I want to replace the 'stg_area' with the value stored in $1 (the path) so that the substitution looks like location.1=/srv/repository/Software/Wind_1.0.2/
Now, to achieve this, I am tried all option below with sed and none worked
sed -i "s/stg_area/$1/" /srv/ppc/abc.properties //output is sed: -e expression #1, char 17: unknown option to `s'
sed -i 's/stg_area/'"$1'"/' /srv/ppc/abc.properties //output is sed: -e expression #1, char 18: unknown option to `s'
sed -i s/stg_area/$1/ /srv/ppc/abc.properties //output is sed: -e expression #1, char 17: unknown option to `s'
I think I have tried all possible ways... Any answer on this is appreciated. Thanks in advance.
You know that sed is using / as a special separator in the command s/pattern/replacement/, right? You've used it yourself in the question.
So obviously there's a problem when you have a replacement string containing /, like yours does.
As the documentation says:
The / characters may be uniformly replaced by any other single character within any given s command. The / character (or whatever other character is used in its stead) can appear in the regexp or replacement only if it is preceded by a \ character.
So the two available solutions are:
use a different separator for the s command, such as
s#stg_area#$1#
(although you still need to check there are no # characters in the replacement string)
sanitize the replacement string so it doesn't contain any special characters (either /, or sequences like \1, or anything else sed treats as special), for example by escaping them with \
sanitized=$(sed 's#/#\\/#g' <<< $1)
(and then used $sanitized instead of $1 your sed script)

SUBSTITUTING VARIBALE IN SED ESCAPING $

I have 20 lines in file i want to remove all lines after nth line
nth line number is set using varible
eg var1=5 then remove line 5 to 20 using sed
Tried
sed ""$var1",$d" file -i
But it produces following error
sed: -e expression #1, char 5: unexpected `,'```
We can try
var="3"
awk -v line="$var" '1; FNR==line{exit}' file > temp && mv temp file
Details:
Here we are creating a shell variable var with value 3, then we are passing that variable to awk program, now the awk program is printing every line till the line number is 3 then the program is exited.
Use this Perl one-liner:
export var1=5
perl -i.bak -pe 'last if $. == $ENV{var1}' in_file
The Perl one-liner uses these command line flags:
-e : Tells Perl to look for code in-line, instead of in a file.
-p : Loop over the input one line at a time, assigning it to $_ by default. Add print $_ after each loop iteration.
-i.bak : Edit input files in-place (overwrite the input file). Before overwriting, save a backup copy of the original file by appending to its name the extension .bak.
$. : Current input line number.
SEE ALSO:
perldoc perlrun: how to execute the Perl interpreter: command line switches
I was beginner to linux thats why i asked such question.
Since no one answered.
I kept trying.
Now i learned how to debug linux shells and hence while using set -x i encountered mistake i was doing while writing up this command.
Now it is solved.
var=2
sed ""$(echo "$var")",\$d" file

How to replace value from one column with value from another column in the same file

I have a file with thousands of lines and columns, two of the columns are IP1 and IP2. IP1 is always the same 192.168.100.1
*example.com,192.168.100.1,10.10.1.1,,5effd70e9d99b1acf,10,63,58,42,0,21,84055280,0
example2.com,192.168.100.1,10.10.1.50,,255b2l429c8f23ee,10,63,37,42,1,21,1451066297,0
example3.com,192.168.100.1,10.10.1.58,,589b7a5f8677b,11,68,37,42,1,20,1451066297,0
.............*
I want to replace the value of IP1 with value of IP2, and delete the value of IP2.
I tried this:
sed -i 's/192\.168\.100\.1/$(grep 192\.168\.100\.1 file | awk -F',' '{print $2}')/' file
The following error occured:
sed: -e expression #1, char 68: unterminated `s' command
Please help.
awk to the rescue! eliminate sed, this should do...
awk -F, -v OFS=, '{$2=$3;$3=""}1' file
sed to the rescue! eliminate awk, this should do...
sed -i 's/192\.168\.100\.1,//' file
This sed command finds the string "192.168.100.1," and replace it by nothing.
. has a special meaning in a sed command, so it requires backslash. More info on the man pages.

sed: add an unusual header with quotes

I have following variables:
STR1="CD45RA_naive"
STR2="CD45RO_mem"
STR3="CD127_Treg"
STR4="IL17_Th_stim_MACS"
STR5="IL17_Th17_stim"
names=($STR1 $STR2 $STR3 $STR4 $STR5)
and the files with the same names as variables with a ".bed" extension.
in each file I need to add a following header:
track name='enhancer region' description='${names[$i]}'"
My idea was the following:
for ((i=0; i<${#names[#]}; i++))
do
echo "output/annotation/"${names[$i]}".bed"
sed -i '' "1 i \track name='enhancer region' description='"${names[$i]}"'" "output/annotation/"${names[$i]}".bed"
done
However I get an error:
sed: 1: "1 i \track name='enhanc ...": extra characters after \ at the end of i command
What is a proper way to add a corresponding header to each file (gawk,sed)?
You typically need the line that is inserted for the i command to be on its own line. In other words, you need to add a newline after the backslash. For example:
$ cat input
line 1
$ sed -e '1i\
foo' input
foo
line 1
$ sed -e '1i\ foo' input
sed: 1: "1i\ foo
": extra characters after \ at the end of i command

sed replace variables while read lines

I'm working on a shell script and i need to change some strings from different lines of a file into a while read statement. The structure need to be like this, because the String_Search and String_result will be calculated on each line.
while read line
do
varA="String_Search"
resA="String_Result"
line=`echo $line | sed -e "s/$varA/$resA"`
echo $line >> outputFile.txt
done < "inputFile.txt"
The script doesn't works and its showing to me this error message:
sed: -e expression #1, char 31: unterminated `s' command
Anyone Can Help Me?
Thanks to All
You need to end the substitution pattern by a slash /
line=`echo $line | sed -e "s/$varA/$resA/"`