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

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

Related

How to replace string-literal that takes the form of shell variable in bash/with sed? [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
sed command not replacing ip address via variables [duplicate]
(1 answer)
Closed 1 year ago.
I'm familiar with sed's ability to replace in-place in a file, as answered here: sed edit file in place.
I.e. sed -i 's/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g' filename
I'm having trouble expanding this concept to replacing a string-literal that takes the form of a shell-variable -- how does one do this?
To illustrate with an example:
Given file file.txt:
# file.txt
set(FOO ${FOO})
...and shell environment variable ${FOO}:
$ FOO=bar
$ echo ${FOO}
bar
...how can I use sed to replace string-literal "${FOO}" in file.txt with the value of shell-variable ${FOO} i.e. "bar"? I.e. I'd like the resulting content of file.txt to be:
# file.txt
set(FOO bar)
I have a mental block thinking past the obviously-incorrect sed -i 's/${FOO}/${FOO}/g' file.txt
I gravitate towards sed because of past experience, and might prefer a sed-centric answer for the same reason. But any solution is probably okay, but with a preference for POSIX-compliance, if shell-native. To be even more specific, this is going to be run in a docker container with a debian-10.3 base...so I suppose solutions that work with any tools included in that distro should be okay as well.

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?

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

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.

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

Bash loop - tokenize on lines rather than words [duplicate]

This question already has answers here:
Bash and filenames with spaces
(6 answers)
Closed 8 years ago.
I'm writing a script to do variable substitution into a Java properties file, of the format name=value. I have a source file, source.env like this:
TEST_ENV_1=test environment variable one
TEST_ENV_2=http://test.environment.com/one
#this is a comment with an equal sign=blah
TEST_ENV_3=/var/log/test/env/2.log
My script will replace every occurence of TEST_ENV_1 in the file dest.env with "test environment variable one", and so on.
I'm trying to process a line at a time, and having problems because looping on output from a command like sed or grep tokenizes on white space rather than the entire line:
$ for i in `sed '/^ *#/d;s/#.*//' source.env`; do
echo $i
done
TEST_ENV_1=test
environment
variable
one
TEST_ENV_2=http://test.environment.com/one
TEST_ENV_3=/var/log/test/env/2.log
How do I treat them as lines? What I want to be able to do is split each line apart on the "=" sign and make a sed script with a bunch of substitution regex's based on the source.env file.
sed '/^ *#/d;s/#.*//' source.env | while read LINE; do
echo "$LINE"
done
An alternative is to change $IFS as per #Jim's answer. It's better to avoid backticks in this case as they'll cause the entire file to be read in at once, whereas piping the output of sed to while above will allow the file to be processed line by line without reading the whole thing in to memory.

Resources