How to evaluate variable within `sed` command? [duplicate] - bash

This question already has answers here:
Environment variable substitution in sed
(12 answers)
SED not working [unterminated `s' command]
(3 answers)
Closed 7 years ago.
I have a variable called "num", that holds a number. I want to use it in "sed" like so:
sed '1,$(num)d' file.txt
also tried:
sed '1,($num)d' file.txt
...so that I can delete lines from 1 until the line num.
It gives me an error like this:
sed: -e expression #1, char 4: unknown command: `('
What is the correct way to do this? thank.

Your shell variable usage was incorrect. First, using the double quote ensures that the shell will expand the variable. Second, surrounding the variable in question (num) with the braces ensures that the variable will be seen by the shell as $num, instead of the subsequent d getting glommed on.
Here is how you should specify what you want to do:
sed "1,${num}d" file.txt

You can use single quotes but need to concatenate the script
num=42; seq 1 45 | sed '1,'$num'd'
will print
43
44
45
as expected.

Related

Applying sed in bash variable and assigning it to another [duplicate]

This question already has answers here:
Concat numbers from JSON without doublequotes using jq [duplicate]
(1 answer)
How to remove double-quotes in jq output for parsing json files in bash?
(2 answers)
Closed 1 year ago.
I have:
MY_FOLDER=`jq '.results_folder' ./conf.json`
FOLDER_WITHOUT_QUOTES=$MY_FOLDER | sed 's/"//g'
python my_code.py > $FOLDER_WITHOUT_QUOTES/log.log
So, there is a json file with a folder name. But jsons demand strings to be inside ". And reading the json with bash returns me ", which I want to remove
Passing it to a variable and then applying sed isn't working. What's the correct syntax for doing it?
Thank you!
Posix shell would need:
FOLDER_WITHOUT_QUOTES="$(printf '%s\n' "$MY_FOLDER" | sed 's/"//g')"
With Bash you can use the here-document syntax:
FOLDER_WITHOUT_QUOTES=$(sed 's/"//g' <<< "$MY_FOLDER")
... and you can even get rid off a call to sed with the special substitution:
FOLDER_WITHOUT_QUOTES=${MY_FOLDER//\"}
Note: prefer the $(command) syntax to the backquotes which are less readable and cannot be nested as easily.

I want to remove a line from a file using sed [duplicate]

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

Using sed with ${parameter} [duplicate]

This question already has answers here:
sed substitution with Bash variables
(6 answers)
Closed 4 years ago.
Problem
How do we use variables in a sed edit string?
Example
The file statement.txt is the sentence
I like my pet bird.
Given a variable ${myPet}, how can we use sed to replace bird with the value in ${myPet}?
What doesn't work
sed -ie 's/bird/${myPet}/g' statement.txt
The result is
I like my pet ${myPet}.
' single quotes don't expand value of a shell variable so you need to use " double quotes here.
myPet="your_value"
sed -ie "s/bird/${myPet}/g" statement.txt

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?

sed. passing a variable in sed command [duplicate]

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

Resources