I am using this command with variables
sed '/${LN_NM}/d' file.txt
I am using it to delete a line from file.txt
.
But when this command is used without the variable it works fine.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI am using this command with variables
sed '/${LN_NM}/d' file.txt
I am using it to delete a line from file.txt
.
But when this command is used without the variable it works fine.
/regular expression/d
will delete lines that match that regular expression (So if $LN_NUM
is just a number like 5 or 18, every line with that number somewhere in it). If you want to instead delete the given line number:
sed "${LN_NM}d" file.txt
If you want to save the file after deleting that line from it, use ed
, not sed
:
ed -s file.txt <<EOF
${LN_NM}d
w
EOF
sed -i
to edit the file in place, instead of using ed
.
Mar 25, 2020 at 20:09
sed -i
is non standard and different implementations that support it as an extension have different behaviors - OS X sed
, for example, requires it to have an argument, while one is optional with GNU sed
. Better to stay within POSIX especially when the asker doesn't say what OS they're using.
As @plumo already mentioned, variables wont get substituted while using '
(single quotes)
Change your command by using "
double-quotes, which should do the trick.
sed "/${LN_NM}d/" file.txt
$LN_NUM
, not the exact line number...