How to use variable in Linux sed command - sed

I have already read some topic but there is no similar to mine.
I need to pass to sed command with a $value but its too hard :
sed "1,$valued" filename.txt
like
sed "1,4d" filename.txt
How can i do that ?

That's trying to use a variable called $valued.
So you can either:
1,${value}d.
Or set $valued to 4d.

Easiest way:
sed "1,$value"d filename.txt
$value is a shell variable (I assume you were already doing that). So you just needed a way to separate the variable name from what comes next (to avoid evaluating $valued). Quote marks do the job just fine since the shell removes them before calling sed.

Related

How to remove the last dot from a string in bash?

I have a string as below:
domain="abc-xyz.com."
I need to remove the . at the last so it would look like abc-xyz.com
I tried this and it doesn't work as it is expecting a file
sed 's/\.$//' $domain
Can someone please help me?
Answering OP's issue's fix: Since you are reading input from a variable, then that's not the correct way to pass value from variable to sed what you are using shown in your question(you must be getting No such file or directory error while executing your code).
Try using:
echo "$domain" | sed 's/\.$//'
OR use:
sed 's/\.$//' <<<"$domain"
Ideal way to deal is could be: Try following if you are ok to use parameter expansion as shown by #anubhava sir in comments.
var="${domain%.}"
This might work for you (GNU sed):
sed 's/\(.*\)\./\1/' file
Use greed to locate the last period and replace it by what came before it.

sed backreference not being found

I am trying to use 'sed' to replace a list of paths in a file with another path.
An example string to process is:
/path/to/file/block
I want to replace /path/to/file with something else.
I have Tried
sed -r '/\s(\S+)\/block/s/\1/new_path/'
I know it's finding the matching string but I'm getting an invalid back reference error.
How can I do this?
This may do:
echo "/path/to/file/block" | sed -r 's|/\S*/(block)|/newpath/\1|'
/newpath/block
Test
echo "test=/path/file test2=/path/to/file/block test3=/home/root/file" | sed -r 's|/\S*/(block)|/newpath/\1|'
test=/path/file test2=/newpath/block test3=/home/root/file
Back-references always refer to the pattern of the s command, not to any address (before the command).
However, in this case, there's no need for addressing: we can apply the substitution to all lines (and it will change only lines where it matches), so we can write:
s,\s(\S+)/block/, \1/new_path,
(I added a space to the RHS, as I'm guessing you didn't mean to overwrite that; also used a different separator to reduce the need for backslashes.)

Issue assigning a SED to a variable

I am working on a BSD machine and what I am attempting to do is assign a variable the output from a SED command that uses a variable as input. Been working on this for 3 days, tried multiple different things, and always end up with './subscript: ${sed ...}: Bad substitution' error. Any help is greatly appreciated.
TMPEX=${sed "s/\\\\\\/\\\\/g" <<$TMPEX}
TEMPEX originally contains
C:\\Windows\\System32\\wininit.exe
and I would like to replace the double backslashes with single backslashes so that TMPEX contains:
C:\Windows\System32\wininit.exe
What am I missing?
Using sed and a shell which understands here-strings:
TMPEX="$(sed 's/\\\\/\\/g' <<< "$TMPEX")"
Or, still using sed, for a shell which does not understand here-strings:
TMPEX="$(echo "$TMPEX" | sed 's/\\\\/\\/g')"
Or even better, if the shell understands pattern substitution:
TMPEX="${TMPEX//\\\\/\\}"

How to use sed to delete lines which have certain pattern and at some specific line range?

This is the command working for me:
sed "50,99999{/^\s*printf/d}" the_file
So this command delete all the lines between 50 and 99999 which have "printf" in it and there is only whitespace before printf at the line.
Now my questions are:
how to replace 99999 with some meta symbol to indicate the real line number
I tried sed "50,${/^\s*PUTS/d}" the_file, but it is not right.
how to replace "printf" with an environment variable? I tried
set pattern printf
sed "50,99999{/^\s*$pattern/d}" the_file
but it is not right.
Assuming a Bourne-like shell such as bash:
Simply define shell variables and splice them into your sed command string:
endLine=99999
pattern='printf'
sed '50,'"$endLine"'{ /^\s*'"$pattern"'/d; }' the_file
Note that the static parts of the sed command strings are single-quoted, as that protects them from interpretation by the shell (which means you needn't quote $ and `, for instance).
You can put everything into a single double-quoted string so as to be able to embed variable references directly, but distinguishing between what the shell will interpret up front and what sed will interpret can get confusing quickly.
That said, using a double-quoted string for the case at hand is simple:
sed "50,$endLine { /^\s*$pattern/d; }" the_file
sed "50,${/^\s*PUTS/d}" the_file
this line won't work, because you used double quotes, and you need escape the dollar: \$ or use single quote: '50,${/.../d}' file
sed "50,99999{/^\s*$pattern/d}" file
this line should work.
EDIT
wait, I just noticed that you set env var via set... this is not correct if you were with Bash. you should use export PAT="PUT" in your script.
check #Jonathan and #tripleee 's comments

capturing groups in sed

I have many lines of the form
ko04062 ko:CXCR3
ko04062 ko:CX3CR1
ko04062 ko:CCL3
ko04062 ko:CCL5
ko04080 ko:GZMA
and would dearly like to get rid of the ko: bit of the right-hand column. I'm trying to use sed, as follows:
echo "ko05414 ko:ITGA4" | sed 's/\(^ko\d{5}\)\tko:\(.*$\)/\1\2/'
which simply outputs the original string I echo'd. I'm very new to command line scripting, sed, pipes etc, so please don't be too angry if/when I'm doing something extremely dumb.
The main thing that is confusing me is that the same thing happens if I reverse the \1\2 bit to read \2\1 or just use one group. This, I guess, implies that I'm missing something about the mechanics of piping the output of echo into sed, or that my regexp is wrong or that I'm using sed wrong or that sed isn't printing the results of the substitution.
Any help would be greatly appreciated!
sed is outputting its input because the substitution isn't matching. Since you're probably using GNU sed, try this:
echo "ko05414 ko:ITGA4" | sed 's/\(^ko[0-9]\{5\}\)\tko:\(.*$\)/\1\2/'
\d -> [0-9] since GNU sed doesn't recognize \d
{} -> \{\} since GNU sed by default uses basic regular expressions.
This should do it. You can also skip the last group and simply use, \1 instead, but since you're learning sed and regex this is good stuff. I wanted to use a non-capturing group in the middle (:? ) but I could not get that to play with sed for whatever reason, perhaps it's not supported.
sed --posix 's/\(^ko[0-9]\{5\}\)\( ko:\)\(.*$\)/\1 \3/g' file > result
And ofcourse you can use
sed --posix 's/ko://'
You don't need sed for this
Here is how you can do it with bash:
var="ko05414 ko:ITGA4"
echo ${var//"ko:"}
${var//"ko:"} replaces all "ko:" with ""
See Manipulating Strings for more info
#OP, if you just want to get rid of "ko:", then
$ cat file
ko04062 ko:CXCR3
ko04062 ko:CX3CR1
ko04062 ko:CCL3
ko04062 ko:CCL5
some text with a legit ko: this ko: will be deleted if you use gsub.
ko04080 ko:GZMA
$ awk '{sub("ko:","",$2)}1' file
ko04062 CXCR3
ko04062 CX3CR1
ko04062 CCL3
ko04062 CCL5
some text with a legit ko: this ko: will be deleted if you use gsub.
ko04080 GZMA
Jsut a note. While you can use pure bash string substitution, its only more efficient when you are changing a single string. If you have a file, especially a big file, using bash's while read loop is still slower than using sed or awk.