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
Related
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..)
I have a file in which i have to find a String and replace it. It giving me error when i am using sed because the replace string contains "/".
replace_string=6W4kngjd3c7oOShnG7iWYQpZVzr4S88G20fujmP7cdM1m5Gw550WfMD38DA4g6O4qxUIJJwt2OtLTRmh7vWz+AWQVmIMajk3OylEfR/X+afrD6YOeGLYHU6Ef4DYv/3x
sed -i -e 's|string|'$replace_string'|g' $FILEPATH
This is the error which its giving -
sed: -e expression #1, char 38: unterminated `s' command
Is there any other method other than sed or any other way i can use sed?
sed doesn't understand literal strings (see Is it possible to escape regex metacharacters reliably with sed), only regexps and backreference-enabled replacements that can't contain whatever delimiters you use. If you want to replace a string then use a tool that understands strings such as awk, e.g. this will work for any characters in your strings (code would need tweaked to handle newlines within old or multiple substitutions on a single line):
old='string1' new='string2' awk '
BEGIN {
old = ENVIRON["old"]
new = ENVIRON["new"]
lgth = length(old)
}
s = index($0,old) { $0 = substr($0,1,s-1) new substr($0,s+lgth) }
{ print }
' file
ripgrep is another tool that allows to search and replace literally. The substitution capability is limited compared to sed (for ex: it always replaces all occurences like g flag)
$ cat ip.txt
2.3/[4]*6+[4]*2
foo
5.3-[4]*9
$ rg -N --passthru -F '[4]*' -r '\&/' ip.txt
2.3/\&/6+\&/2
foo
5.3-\&/9
For inplace editing:
rg --passthru -F 'old' -r 'new' ip.txt > tmp && mv tmp ip.txt
With shell variables:
rg --passthru -F "$old" -r "$new" ip.txt > tmp && mv tmp ip.txt
-N flag not required when redirecting output of rg
I want to append a string variable.
for e.g. WINDOW=WINDOW winName="fp_w_RetrocessionTrigger" winTitle="Retrocession Trigger" comp="FPGUI" funcId="14316" domainId="bancs" preLoad="true">
This is my string variable(WINDOW). I want this varible of string get append in another file using sed or awk cmd.
Although I have tried the cmd like sed -i ''$n'i "'$WINDOW'"' test.xml but it only prints space there. Please help me.
Your question is not very clear, however if u want to append a text in the beginning and/or at the end of each line of a file, you can try the below
$ cat 1.txt
1
2
3
4
5
Add the text in the beginning of the line :
$ sed -e 's/^/start_of_the_line/' -i 1.txt
Add the text at the end of the line :
$ sed -e 's/$/end_of_the_line/' -i 1.txt
Output
$cat 1.txt
start_of_the_line1end_of_the_line
start_of_the_line2end_of_the_line
start_of_the_line3end_of_the_line
start_of_the_line4end_of_the_line
start_of_the_line5end_of_the_line
I am new to shell script. I am sourcing a file, which is created in Windows and has carriage returns, using the source command. After I source when I append some characters to it, it always comes to the start of the line.
test.dat (which has carriage return at end):
testVar=value123
testScript.sh (sources above file):
source test.dat
echo $testVar got it
The output I get is
got it23
How can I remove the '\r' from the variable?
yet another solution uses tr:
echo $testVar | tr -d '\r'
cat myscript | tr -d '\r'
the option -d stands for delete.
You can use sed as follows:
MY_NEW_VAR=$(echo $testVar | sed -e 's/\r//g')
echo ${MY_NEW_VAR} got it
By the way, try to do a dos2unix on your data file.
Because the file you source ends lines with carriage returns, the contents of $testVar are likely to look like this:
$ printf '%q\n' "$testVar"
$'value123\r'
(The first line's $ is the shell prompt; the second line's $ is from the %q formatting string, indicating $'' quoting.)
To get rid of the carriage return, you can use shell parameter expansion and ANSI-C quoting (requires Bash):
testVar=${testVar//$'\r'}
Which should result in
$ printf '%q\n' "$testVar"
value123
use this command on your script file after copying it to Linux/Unix
perl -pi -e 's/\r//' scriptfilename
Pipe to sed -e 's/[\r\n]//g' to remove both Carriage Returns (\r) and Line Feeds (\n) from each text line.
for a pure shell solution without calling external program:
NL=$'\n' # define a variable to reference 'newline'
testVar=${testVar%$NL} # removes trailing 'NL' from string
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/"`