Solved sed operator or |

bagas

Well-Known Member

Reaction score: 8
Messages: 284

Hello.
My system FreeBSD 13.
The operator does not work in sed or |
echo "ggg nnn aaa nnn tttt ggg" | sed 's/ggg\|nnn//g'
ggg nnn aaa nnn tttt ggg
 

Jose

Daemon

Reaction score: 989
Messages: 1,195

Hello.
My system FreeBSD 13.
The operator does not work in sed or |
You have two problems.
  1. The shell's pipe operator | does not need to be escaped if you use single quotes. The sed(1) command you're giving only matches a literal "ggg|nnn". Edit: try this to convince yourself
    Code:
    echo 'ggg|nnn aaa nnn tttt ggg' | sed 's/ggg\|nnn//g'
  2. The or operator in sed(1), | is only available if you turn on extended regular expressions with the -E command line switch.
Consult the man page for more details.
 
Last edited:
OP
bagas

bagas

Well-Known Member

Reaction score: 8
Messages: 284

You have two problems.
  1. The pipe operator | does not need to be escaped if you use single quotes. The sed(1) command you're giving only matches a literal "ggg|nnn". Edit: try this to convince yourself
    Code:
    echo 'ggg|nnn aaa nnn tttt ggg' | sed 's/ggg\|nnn//g'
  2. The or operator in sed(1), | is only available if you turn on extended regular expressions with the -E command line switch.
Consult the man page for more details.
Understood nothing.
It works fine in Linux.
$ echo "ggg nnn aaa nnn tttt ggg" | sed 's/ggg\|nnn//g'
aaa tttt
 
OP
bagas

bagas

Well-Known Member

Reaction score: 8
Messages: 284

it worked,thanks.
echo "ggg nnn aaa nnn tttt ggg" | sed -E 's/ggg|nnn//g'
aaa tttt
 

ralphbsz

Son of Beastie

Reaction score: 2,346
Messages: 3,238

Understood nothing.
That's bad. To do anything interesting in shell, you need to understand quoting: How text strings behave when put between pairs of ' or ", or preceded by \.

It works fine in Linux.
So? This is not Linux. Programs like sed or ls or awk or make act differently from their Linux counterparts.
 

Erichans

Member

Reaction score: 37
Messages: 50

Understood nothing. [...]

That's bad. To do anything interesting in shell, you need to understand quoting: How text strings behave when put between pairs of ' or ", or preceded by \.
[...]

With FreeBSD, as installed, you'll work with tcsh(1); sometimes with the csh oddities within tcsh. The tcsh is a fine interactive shell. Stay away from tcsh shell scripting (also: stay away from csh scripting).

You as a user have the option of using various shells to your liking (bash, zsh etc.). They all have more or less complicated quoting rules: it pays off to—at least—read about those rules and how the shell interprets and executes a command line. The man pages for your shell of choice, being the easy available reference, are terse in the nitty gritty details.

For me, the easy intro, well explained, with numerous examples is Using the csh and tcsh by Paul H. Dubois (albeit from some time ago). You can start reading about the topic in question: Chapter 11: Quoting and Special Characters. You might also find helpfull A Guide to Unix Shell Quoting; and especially: 3.2.2 csh, tcsh
 
Top