What does this sed line do? - sed

I am not familiar with sed. Now I came accross the following line in a shell script:
sed 's|'`pwd`'||'
what does the line do? Does it replace || with something else?

it removes any string that equals to current-working-directory in the input. because pwd may itself contain / instead of /, | used in sed command as pattern-delimiter.

Related

How can I replace "^M" with ",1" at the end using sed on Mac OS?

How can I add ",1" at the end of each of the lines using sed?
The file I am using is like this;
A
B
C
I tried this;
sed 's/$/,1/' alphabets.txt > alphabets1.txt
and got this;
A
,1
B
,1
C
,1
but what I want is this;
A,1
B,1
C,1
When I tried the best answer from "https://stackoverflow.com/questions/15978504/add-text-at-the-end-of-each-line", I got a blank page.
sed -n 's/$/,1/' alphabets.txt > alphabets2.txt
I found out that there's ^M at the end using cat -vt file. How can I delete it and change it to ",1"?
Mac sed is BSD sed. You may try this command on a file optionally ending with \r to remove \r and append 1 in the end:
sed -E $'s/\\\r?$/,1/'
A,1
B,1
C,1
Make sure you shell is bash while running this.
You may also use:
sed -E 's/'$'\r''?$/,1/' file
$'\r' is a BASH string to match \r and ? after this makes it optional.
Please note that this sed command will also run fine on gnu sed.

Replace string and next one using sed

I know how to replace sting via sed:
sed -i "s|.*#app-${BRANCH}-log.*| Path ${LOG_PATH} #app-${BRANCH}-log|" /etc/td-agent-bit/td-agent-bit.conf
and this is work for file like
[INPUT]
Name tail
Path /var/lib/docker/containers/f774c1a3689dfffb2528833ac2ded629c3b1873fd3af96fe0cf1f041f22f88d8/f774c1a3689dfffb2528833ac2ded629c3b1873fd3af96fe0cf1f041f22f88d8-json.log #app-develop-log
Tag app.develop
Interval_Sec 1
but how to replace next line? for example:
#This line just for triggering sed
And this one must be replaced
Any idea? Sorry for my horrible English.
With GNU sed:
sed '/^#This line just for triggering sed$/{n;s/.*/foo/}' file
Output:
#This line just for triggering sed
foo
See: man sed

replace particular line with user provided variable with sed

My code is like below; and it is not working.
line_number=$1;
variable1=$2;
sed '\$line_number c\
\$variable1' file > tmp.out
If I write like below then its working. Can you please suggest how can I make above code running?
sed '1 c\
<replace>abc</replace>' file > tmp.out
Take the variables out of single quotes, and you don't need any backslash before the variables:
sed "$line_number"' c\
'"$variable1" file > tmp.out
You can try this,
#!/bin/bash
line_number=$1;
variable1=$2;
sed "$line_number c\
$variable1" file > tmp.out
If you want to use your variables values inside sed, then you have use " (double quotes).
Then, It will be interpreted by shell.

How to insert strings containing slashes with sed? [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 1 year ago.
I have a Visual Studio project, which is developed locally. Code files have to be deployed to a remote server. The only problem is the URLs they contain, which are hard-coded.
The project contains URLs such as ?page=one. For the link to be valid on the server, it must be /page/one .
I've decided to replace all URLs in my code files with sed before deployment, but I'm stuck on slashes.
I know this is not a pretty solution, but it's simple and would save me a lot of time. The total number of strings I have to replace is fewer than 10. A total number of files which have to be checked is ~30.
An example describing my situation is below:
The command I'm using:
sed -f replace.txt < a.txt > b.txt
replace.txt which contains all the strings:
s/?page=one&/pageone/g
s/?page=two&/pagetwo/g
s/?page=three&/pagethree/g
a.txt:
?page=one&
?page=two&
?page=three&
Content of b.txt after I run my sed command:
pageone
pagetwo
pagethree
What I want b.txt to contain:
/page/one
/page/two
/page/three
The easiest way would be to use a different delimiter in your search/replace lines, e.g.:
s:?page=one&:pageone:g
You can use any character as a delimiter that's not part of either string. Or, you could escape it with a backslash:
s/\//foo/
Which would replace / with foo. You'd want to use the escaped backslash in cases where you don't know what characters might occur in the replacement strings (if they are shell variables, for example).
The s command can use any character as a delimiter; whatever character comes after the s is used. I was brought up to use a #. Like so:
s#?page=one&#/page/one#g
A very useful but lesser-known fact about sed is that the familiar s/foo/bar/ command can use any punctuation, not only slashes. A common alternative is s#foo#bar#, from which it becomes obvious how to solve your problem.
add \ before special characters:
s/\?page=one&/page\/one\//g
etc.
In a system I am developing, the string to be replaced by sed is input text from a user which is stored in a variable and passed to sed.
As noted earlier on this post, if the string contained within the sed command block contains the actual delimiter used by sed - then sed terminates on syntax error. Consider the following example:
This works:
$ VALUE=12345
$ echo "MyVar=%DEF_VALUE%" | sed -e s/%DEF_VALUE%/${VALUE}/g
MyVar=12345
This breaks:
$ VALUE=12345/6
$ echo "MyVar=%DEF_VALUE%" | sed -e s/%DEF_VALUE%/${VALUE}/g
sed: -e expression #1, char 21: unknown option to `s'
Replacing the default delimiter is not a robust solution in my case as I did not want to limit the user from entering specific characters used by sed as the delimiter (e.g. "/").
However, escaping any occurrences of the delimiter in the input string would solve the problem.
Consider the below solution of systematically escaping the delimiter character in the input string before having it parsed by sed.
Such escaping can be implemented as a replacement using sed itself, this replacement is safe even if the input string contains the delimiter - this is since the input string is not part of the sed command block:
$ VALUE=$(echo ${VALUE} | sed -e "s#/#\\\/#g")
$ echo "MyVar=%DEF_VALUE%" | sed -e s/%DEF_VALUE%/${VALUE}/g
MyVar=12345/6
I have converted this to a function to be used by various scripts:
escapeForwardSlashes() {
# Validate parameters
if [ -z "$1" ]
then
echo -e "Error - no parameter specified!"
return 1
fi
# Perform replacement
echo ${1} | sed -e "s#/#\\\/#g"
return 0
}
this line should work for your 3 examples:
sed -r 's#\?(page)=([^&]*)&#/\1/\2#g' a.txt
I used -r to save some escaping .
the line should be generic for your one, two three case. you don't have to do the sub 3 times
test with your example (a.txt):
kent$ echo "?page=one&
?page=two&
?page=three&"|sed -r 's#\?(page)=([^&]*)&#/\1/\2#g'
/page/one
/page/two
/page/three
replace.txt should be
s/?page=/\/page\//g
s/&//g
please see this article
http://netjunky.net/sed-replace-path-with-slash-separators/
Just using | instead of /
Great answer from Anonymous. \ solved my problem when I tried to escape quotes in HTML strings.
So if you use sed to return some HTML templates (on a server), use double backslash instead of single:
var htmlTemplate = "<div style=\\"color:green;\\"></div>";
A simplier alternative is using AWK as on this answer:
awk '$0="prefix"$0' file > new_file
You may use an alternative regex delimiter as a search pattern by backs lashing it:
sed '\,{some_path},d'
For the s command:
sed 's,{some_path},{other_path},'

modify a line using sed

I need to used sed for following requirment using sed
I have one string as $str and I need to replace blow line in a file
abh{1..$abh} cdf_$ghu,xyz * abh{}.$xy
New modified line should be as below
abh{1..$abh} cdf_$ghu,$str * abh{}.$xy
Note "xyz" can be any arbitrary value. Could you please tell me how to do using sed in one liner.
sed 's/\(^\s*abh{1..$abh}\s*\)\(.*xyz\)/\1/' file.txt
but still does not work. Any help would be appreciated.
Try this:
$ sed 's|\(\S\+\s\+[^,]\+,\)\S\+\(\s\+.*\)|\1$str\2|' file.txt
abh{1..$abh} cdf_$ghu,$str * abh{}.$xy
Or even more simple:
$ sed 's|,\S\+|,$str|' example.txt
echo 'abh{1..$abh} cdf_$ghu,xyz * abh{}.$xy' | sed 's/\(.*\$ghu,\)\(.*\)\( .*\)/\1\$str\3/g'