How do I get sed to read from standard input?

LinuxBashShell

Linux Problem Overview


I am trying

grep searchterm myfile.csv | sed 's/replaceme/withthis/g'

and getting

unknown option to `s'

What am I doing wrong?

Edit:

As per the comments the code is actually correct. My full code resembled something like the following

grep searchterm myfile.csv | sed 's/replaceme/withthis/g'
# my comment

And it appears that for some reason my comment was being fed as input into sed. Very strange.

Linux Solutions


Solution 1 - Linux

use the --expression option

grep searchterm myfile.csv | sed --expression='s/replaceme/withthis/g'

Solution 2 - Linux

use "-e" to specify the sed-expression

cat input.txt | sed -e 's/foo/bar/g'

Solution 3 - Linux

To make sed catch from stdin , instead of from a file, you should use -e.

Like this:

curl -k -u admin:admin https://$HOSTNAME:9070/api/tm/3.8/status/$HOSTNAME/statistics/traffic_ips/trafc_ip/ | sed -e 's/["{}]//g' |sed -e 's/[]]//g' |sed -e 's/[\[]//g' |awk  'BEGIN{FS=":"} {print $4}'

Solution 4 - Linux

If you are trying to do an in-place update of text within a file, this is much easier to reason about in my mind.

grep -Rl text_to_find directory_to_search 2>/dev/null | while read line; do  sed -i 's/text_to_find/replacement_text/g' $line; done

Solution 5 - Linux

  1. Open the file using vi myfile.csv
  2. Press Escape
  3. Type :%s/replaceme/withthis/
  4. Type :wq and press Enter

Now you will have the new pattern in your file.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondeltanovemberView Question on Stackoverflow
Solution 1 - LinuxBen DavisView Answer on Stackoverflow
Solution 2 - LinuxumläuteView Answer on Stackoverflow
Solution 3 - LinuxLeoChuView Answer on Stackoverflow
Solution 4 - LinuxWesView Answer on Stackoverflow
Solution 5 - LinuxTejaView Answer on Stackoverflow