user3113427
user3113427

Reputation: 465

Why my sed command doesn't work sometimes with the r flag

I am really confused.

Sometimes this flag works fine

sed -e 's/dd//gp' file

sometimes i need to use

sed -re 's/dd//gp' file

sometimes this

sed -ren 's/dd//gp' file

and sometimes

sed -n 's/dd//gp' file

Just now i was trying to suppress the outout but i was keeping coming with -ren flag

which worked for other regex last week

Now i had to use -n only to suppress the ouput

what is the difference between combinations

I have this commnad

sed -n 's/\/\*\*//gp' file which works witn -n but not with -en

Upvotes: 1

Views: 2055

Answers (1)

Conner
Conner

Reputation: 31060

You can use man sed to determine what the various flags of sed are used for.

The -e flag is only necessary if specifying multiple quote blocks to sed.

echo "foo" | sed 's/foo/bar/'
=> bar
echo "foo" | sed -e 's/foo/bar/' -e 's/bar/baz/'
=> baz

The -r flag expands sed's regular expressions to use extended regular expressions.

echo "foo" | sed 's/f??/bar/'
=> foo
echo "foo" | sed -r 's/f??/bar/'
=> baroo

The -n flag does not suppress output, instead it suppresses the automatic output of whatever is in the pattern space. If you use p or the s///p flag then printing will still happen explicitly and produce output.

For example:

echo "foo" | sed ''
=> foo
echo "foo" | sed -n ''
=> 
echo "foo" | sed -n 'p'
=> foo
echo "foo" | sed -n 's/foo/bar/p'
=> bar

If you'd like to suppress sed's output you can redirect STDOUT and STDERR to /dev/null like this:

sed '<commands>' &>/dev/null

See this video series for an introduction to sed.

Upvotes: 3

Related Questions