How do I get sed to read from standard input?
LinuxBashShellLinux 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
- Open the file using
vi myfile.csv
- Press Escape
- Type
:%s/replaceme/withthis/
- Type
:wq
and press Enter
Now you will have the new pattern in your file.