What's The Meaning of This Sed Command? Sed 's%^.*/%%'

What's the meaning of this sed command? sed 's%^.*/%%'

The % is an alternative delimiter so that you don't need to escape the forward slash contained in the matching portion.

So if you were to write the same expression with / as a delimiter, it would look like:

sed 's/^.*\///'

which is also kind of difficult to read.

Either way, the expression will look for a forward slash in a line; if there is a forward slash, remove everything up to and including that slash.

Why can s command of sed can be followed by a comma?

From Using different delimiters in sed:

sed takes whatever follows the "s" as the separator

It is a good way to avoid escaping too much. Code is more readable if you use a delimiter that is not present in the string you want to handle.

For example let's say we want to replace lo/bye from a string. With / as delimiter it would be a little messy:

$ echo "hello/bye" | sed 's/lo\/bye/aa/g'
helaa

So if we define another separator it is more clear:

$ echo "hello/bye" | sed 's|lo/bye|aa|g'
helaa
$ echo "hello/bye" | sed 's,lo/bye,aa,g'
helaa

Using different delimiters in sed commands and range addresses

You can use an alternative regex delimiter as a search pattern by backslashing it:

sed '\,some/path,d'

And just use it as is for the s command:

sed 's,some/path,other/path,'

You probably want to protect other metacharacters, though; this is a good place to use Perl and quotemeta, or equivalents in other scripting languages.

From man sed:

/regexp/
Match lines matching the regular expression regexp.

\cregexpc
Match lines matching the regular expression regexp. The c may be any character other than backslash or newline.

s/regular expression/replacement/flags
Substitute the replacement string for the first instance of the regular expression in the pattern space. Any character other than backslash or newline can be used instead of a slash to delimit the RE and the replacement. Within the RE and the replacement, the RE delimiter itself can be used as a literal character if it is preceded by a backslash.

What does command `sed -i s/^ \+//g; s/ \+/\t/g` mean?

Here is what it means:

  • -i - apply modifications to the input file directly
  • "s/^ \+//g; - remove all spaces at the start of each line
  • s/ \+/\t/g" - after the first replacement, replace all space strings with tabs

How does this sed command: sed -e :a -e '$d;N;2,10ba' -e 'P;D' work?

:a is a label. $ in the address means the last line, d means delete. N stands for append the next line into the pattern space. 2,10 means lines 2 to 10, b means branch (i.e. goto), P prints the first line from the pattern space, D is like d but operates on the pattern space if possible.

In other words, you create a sliding window of the size 10. Each line is stored into it, and once it has 10 lines, lines start to get printed from the top of it. Every time a line is printed, the current line is stored in the sliding window at the bottom. When the last line gets printed, the sliding window is deleted, which removes the last 10 lines.

You can modify the commands to see what's getting deleted (()), stored (<>), and printed by the P ([]):

$ printf '%s\n'  {1..20} | \
sed -e ':a ${s/^/(/;s/$/)/;p;d};s/^/</;s/$/>/;N;2,10ba;s/^/[/;s/$/]/;P;D'
[<<<<<<<<<<1>
[<2>
[<3>
[<4>
[<5>
[<6>
[<7>
[<8>
[<9>
[<10>
(11]>
12]>
13]>
14]>
15]>
16]>
17]>
18]>
19]>
20])

what does sed 's#/text##' mean?

This replace "/text" into null "", try below it gives blank

echo /text| sed 's#/text##'

Initial # is sed delimited for replace and
middle # is seperator between first string to be replace, and
Last # is last delimiter for sed

its same as regex/replace sed command

What does the comma in sed commands stand for?

If you specify two addresses, then you specify range of lines over which the command is executed. In your sed expression, it deletes all lines beginning with the line matched by the first pattern and up to and including the line matched by the second pattern.



Related Topics



Leave a reply



Submit