SED command explanation


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers SED command explanation
# 1  
Old 06-29-2010
SED command explanation

can someone please explain the below sed command..

Code:
 
sed 's/\([^|][^|]*|\)\(.*\)/\2\1/'

# 2  
Old 06-29-2010
Code:
[house@leonov] echo "a|b|c|d|e" | sed 's/\([^|][^|]*|\)\(.*\)/\2|\1/'
b|c|d|e|a|

# 3  
Old 06-29-2010
i know what the sed command does, just do not know how it does that...Smilie... so was hoping if someone could explain it......Smilie
# 4  
Old 06-29-2010
It looks for at least one character that is not a '|' ( [^|] ) at the start of the line plus the first '|' and stores it in variable 1 it stores the rest of the line in variable 2. Then it prints variable 2 followed by variable 1 ( \2\1 ). IMO in practice it is equivalent to:
Code:
sed 's/\([^|]*|\)\(.*\)/\2\1/'


Last edited by Scrutinizer; 06-29-2010 at 06:37 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 06-29-2010
In short, what this will do is the following: Given an arbitrary string containing one or more characters that are not | followed by a |, take everything after that | character and move it to the front of that whole pattern. So, for example, it would turn 'foo|bar' into 'barfoo|', but '||foo|bar' would become '||barfoo|'. The first | is bypassed because it has nothing on its left; the second is skipped because it has only another | on its left. 'bar' is moved after the leading || characters because they are not part of the pattern matched.

(Whether this is what was actually intended is another question.)

As a side note, 's/\([^|][^|]*|\)\(.*\)/\2\1/' is equivalent to the simpler pattern 's/\([^|]+|\)\(.*\)/\2\1/'. [^|] is the set of all characters which are not |, so [^|]* matches any zero or more characters that are not |, while [^|]+ matches any one or more such characters, which makes [^|][^|]* functionally identical to [^|]+ within the regexp.

O'Reilly has a good book on regular expressions. It covers things like this and much more.
# 6  
Old 06-29-2010
Not all sed-s support 'pattern+' paradigm - Solaris' sed doesn't.
# 7  
Old 06-29-2010
Corrrect, AFAIK Only GNU sed supports pattern+ as pattern\+ .
+ is not part of POSIX BRE (Basic Regular Expressions)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Explanation of Nawk command

Hi Folks, I am struggling to understand nawk command which was used by another developer. Can you please explain what each character or string is doing here below: if ; then (3 Replies)
Discussion started by: kirans.229
3 Replies

2. Shell Programming and Scripting

sed command explanation

Will someone give me an explanation on how the sed command below works. sed 's/.*//' Thanks! (3 Replies)
Discussion started by: scj2012
3 Replies

3. UNIX for Dummies Questions & Answers

Explanation of the sort command

Hi everyone, I am wondering if someone could please break down and explain the following sort command for me: ls ${DEST_LOCATION}/${FILES} | sort -rt -k 4,4n | head -1 I have tried working it out using 'man sort', but on AIX there is not a great explanation of this function. I know that... (9 Replies)
Discussion started by: jimbojames
9 Replies

4. UNIX for Advanced & Expert Users

command explanation

can anyone please tell me what does this expression means , i am under probation and need some explanation :) $AUDIT_DIR -type f -mtime +$AUDIT_EXPIRE \ -exec rm {} > /dev/null 2>&1 \; AUDIT_DIR="/var/log/" AUDIT_EXPIRE='30' Please use code tags! (4 Replies)
Discussion started by: semaan
4 Replies

5. Shell Programming and Scripting

sed sorting command explanation

sed '$!N; /^\(.*\)\n\1$/!P; D' i found this file which removes duplicates irrespective for sorted or unsorted file. keep first occurance and remove the further occurances. can any1 explain how this is working.. i need to remove duplicates following file. duplicate criteria is not the... (3 Replies)
Discussion started by: mukeshguliao
3 Replies

6. UNIX for Dummies Questions & Answers

Explanation of the command sed -n -e 's_.*>\(.*\)<.*_\1_p' filename.xml

Hi Friends, I am trying to modify a script .The script contains this line: sed -n -e 's_.*>\(.*\)<.*_\1_p' filename.xml I am not great in sed command.I know, it is regular expression to match a pattern string that starts with s_ and ends with 1_.I doesnot give the desired result. Can... (4 Replies)
Discussion started by: rajsharma
4 Replies

7. Shell Programming and Scripting

Explanation for interesting sed behaviour?

This is my first post so hi to you all. I have browsed these forums in the past and what a great community and resource this is! Thanks to all the contributors ... I look forward to being able to give something back. In the meantime, I have a little conundrum concerning sed. My very simple... (6 Replies)
Discussion started by: Gavster
6 Replies

8. Shell Programming and Scripting

command line explanation

Hello everyone, I found this command line in a website: perl -pi.bak -we's/\z/Your new line\n/ if $. == 2;' your_text_file.txt With this command line you can insert a new line anywhere you want in a text without overwriting what's in it. -p causes perl to assume a loop around your... (4 Replies)
Discussion started by: goude
4 Replies

9. Shell Programming and Scripting

A sed doubt - need explanation

Hi, The following command works fine for me, but I could not grasp the logic working behind of sed command, it's obscure to me :( :confused: echo "./20080916/core/audioex.amr" | sed "s%\(\)/%\1_%g" o/p: ./20080916_core_audioex.amr Could anyone please explain to me in detail, that how... (6 Replies)
Discussion started by: royalibrahim
6 Replies

10. Shell Programming and Scripting

sed command explanation needed

Hi, Could you please explain me the below statement -- phrase wise. sed -e :a -e '$q;N;'$cnt',$D;ba' abc.txt > xyz.txt if suppose $cnt contains value: 10 it copies last 9 lines of abc.txt to xyz.txt why it is copying last 9 rather than 10. and also what is ba and $D over there in... (4 Replies)
Discussion started by: subbukns
4 Replies
Login or Register to Ask a Question