sed with argument variables in shell script [duplicate] - bash

This question already has answers here:
How can I extract a predetermined range of lines from a text file on Unix?
(28 answers)
Shell variables in sed script [duplicate]
(5 answers)
Closed 6 years ago.
I want to write a script that extract lines from line n to m of the input.
doit.sh:
sed -n -e '$1,$2p' input.txt
./doit.sh 3 10
But it gave me an error sed: -e expression #1, char 2: unknown command: `2'. What went wrong?

Related

shell script Awk find and replace loop [duplicate]

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

How to use sed command with variable [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
How to use variables in a command in sed?
(4 answers)
Closed 5 months ago.
when I try to delete particular lines in my file using shellscrpit I use below command, It's not working, So can anyone help me on same.
sed -i '${lineNumber}d' <filename>

Using bash, read each line from file and write it to a variable delimited by comma [duplicate]

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="

How to use sed to append a line in bash script with a variable [duplicate]

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:)

Linux env variable with newlines + sed [duplicate]

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?

Resources