I am happy that grep
does support Perl Compatible Regular Expressions with the -P
option.
Is there a reason why the tool sed
does not have this feature?
The grep
command gives out an exit status:
$echo "foo.bar" | grep -o foo
foo
$echo $?
0
$echo "foo.bar" | grep -o pop
$echo $?
1
But I need to use sed
and I realized that it has no exit status:
$echo "foo.bar" | sed 's/bar.*$//'
foo.
$echo $?
0
$echo "foo.bar" | sed 's/pop.*$//'
foo.bar
$echo $?
0
I know that I should play around with the -q
option, but I have not succeeded.
I have been told it was possible, but without a single working example, that sed
can read from a string variable without need for an input file. I have yet to get it to work.
For general safety, I’m writing the $PATH
variable to another variable, as I mess with this, because I don’t need other problems to arise until I know exactly how to do this.
Consider the following:
~$x=$PATH
~$sed -i 's/:/ /g' $x
this fails with: No such file or directory.
Here are some others I have tried:
~$ sed -i 's/:/ /g' | (read x)
sed: no input files
~$ sed -i 's/:/ /g' | (read $x)
bash: read: `/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games': not a valid identifier
sed: no input files
~$ sed -i 's/:/ /g' | $(read x)
sed: no input files
~$ sed -i 's/:/ /g' | $(read $x)
bash: read: `/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games': not a valid identifier
sed: no input files
~$ sed -i 's/:/ /g' < $(read $x)
bash: read: `/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games': not a valid identifier
bash: $(read $x): ambiguous redirect
~$ sed -i 's/:/ /g' < read $x
bash: read: No such file or directory
~$ sed -i 's/:/ /g' < (read x)
bash: syntax error near unexpected token `('
~$ sed -i 's/:/ /g' < $((read x))
bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games: syntax error: operand expected (error token is "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games")
~$ sed -i 's/:/ /g' < $((read $x))
bash: read /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games: division by 0 (error token is "usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games")
~$ sed -i 's/:/ /g' << $((read $x))
> ;^C
~$ sed -i 's/:/ /g' << $(read $x)
> ;^C
~$ sed -i 's/:/ /g' $x
sed: can't read /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games: No such file or directory
~$ sed -i 's/:/ /g' < $x
bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games: No such file or directory
~$ sed -i 's/:/ /g' < echo $x
bash: echo: No such file or directory
~$ sed -i 's/:/ /g' | echo $x
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
sed: no input files
Can this even work? I would prefer not to need to write files I don't need just so I can use sed. For this particular example, if ~$x=$PATH ~$sed -i 's/:/ /g' $x
actually worked the way I would have hoped, I would get:
/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin /usr/games /usr/local/games
which I could then assign to a variable, and use in future commands, like ls $x
I have a line that says...
Fred Flintstone, Bedrock USA
and I want it to look like...
Fred Flintstone, Bedrock USA ***
How do I append a few * to the end of the line using sed
command?
Does anyone know how to use Sed to delete all blank spaces in a text file? I haven been trying to use the "d" delete command to do so, but can't seem to figure it out