LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-08-2023, 02:02 AM   #1
no-windose
Member
 
Registered: Jun 2018
Posts: 94
Blog Entries: 2

Rep: Reputation: Disabled
sed script and sed command line give different results


I want to replace the 3rd occurrence of a single pipe "|" in some text with a double pipe "||". I can do it with a simple command line but not with a script. May I ask why?
Code:
$ cat sample.txt 
field1 | field2 | field4 | field5 | field6
$ sed 's/ '\|' / \|\| /3' sample.txt
field1 | field2 | field4 || field5 | field6

$ cat sed-script
s/ '\|' / \|\| /3
$ sed -f sed-script sample.txt
field1 | field2 | field4 | field5 | field6

Last edited by no-windose; 01-08-2023 at 02:03 AM.
 
Old 01-08-2023, 02:21 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,334
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
One way is to count to the third pipe:

Code:
sed -r -e 's/^(([^|]+\|){3})\|/\1/'
perl -p -e 's/^(([^|]+\|){3})\|/$1/;'
There is probably a more efficient pattern.
 
Old 01-08-2023, 02:57 AM   #3
no-windose
Member
 
Registered: Jun 2018
Posts: 94

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled
Thanks Turbocapitalist but I would like to know why my own script does not work. Also I would prefer to avoid the -r option because all my other sed scripts are not compatible with it. I haven't tried perl for the same reason: all my scripts are with sed already. Anyway the sed command you suggest does not seem to work:
Code:
$ sed -r -e 's/^(([^|]+\|){3})\|/\1/' sample.txt 
field1 | field2 | field4 | field5 | field6
 
Old 01-08-2023, 03:30 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,976

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
you ought to use set -xv in this case:
Code:
$ set -xv
$ sed 's/ '\|' / \|\| /3' sample.txt
sed 's/ '\|' / \|\| /3' sample.txt
+ sed 's/ | / \|\| /3' sample.txt  # <<< this is the real sed script after command line evaluation
So this is how your command interpreted by the shell. When you use sed -f sed-script bash will not evaluate (or modify) that script, so you need not escape special chars.
From the other hand you don't need to escape anything at all.
Code:
echo 'field1 | field2 | field4 | field5 | field6' | sed 's/ | / || /3'
works too, and actually that sed-script is the same:
Code:
s/ | / || /3
 
1 members found this post helpful.
Old 01-08-2023, 05:20 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,816

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
The embedded sed script is in a 'string'. After a ' the following ' ends it.
Code:
sed 's/ '\|' / \|\| /3' sample.txt
The shell passes it dequoted to sed:
Code:
s/ | / \|\| /3
This is different from your pure sed code
Code:
s/ '\|' / \|\| /3
The | character is not special in a BRE (grep, sed) but it is in an ERE (grep -E, sed -r). And it should never be escaped in the replacement string.
Code:
sed 's/ | / || /3' sample.txt
Code:
sed -r 's/ \| / || /3' sample.txt
Code:
#!/bin/sed -f
s/ | / || /3
Code:
#!/bin/sed -rf
s/ \| / || /3

Last edited by MadeInGermany; 01-08-2023 at 05:27 AM.
 
Old 01-08-2023, 05:31 AM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,145

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
Quote:
Originally Posted by MadeInGermany View Post
The | character is not special in a BRE (grep, sed) but it is in an ERE (grep -E, sed -r).
You can have lots of fun with that character in regex - one of those "corner cases" that people gloss over ...
 
Old 01-08-2023, 02:24 PM   #7
no-windose
Member
 
Registered: Jun 2018
Posts: 94

Original Poster
Blog Entries: 2

Rep: Reputation: Disabled
Thank you all.
 
Old 01-09-2023, 04:19 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,976

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
if you wish to say thanks just click on yes
 
  


Reply

Tags
script, sed



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] df and du give different results on DVD filesystem Wocky Linux - Software 4 02-25-2015 03:25 PM
filter source line based on results line in log using awk and sed samanp Programming 5 04-06-2011 09:42 AM
LXer: Linux Performance: Different Distributions, Very Different Results LXer Syndicated Linux News 0 03-09-2009 09:20 PM
iptables - command line gives different results to gui yogaboy Linux - Newbie 2 12-24-2006 09:28 AM
Different commands, different results jadukor Slackware 8 12-16-2005 03:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration