sed question


 
Thread Tools Search this Thread
# 1  
sed question

Hi, can anyone explain step by step what this line of sed code is trying to do. Thanks.

Sed -e "s/big \(.*\) dog/small \1 cat/" filename

Last edited by NycUnxer; 10-04-2007 at 02:04 PM..
# 2  
Does this help?
Code:
echo "a big black dog" | sed -e 's/big \(.*\) dog/small \1 cat/'
a small black eared cat

The \1 means that the first pattern that matches (.*) (any word) is put in. Similarly, another (second) matching pattern can also be put in. As an example:
Code:
echo "a big black eared dog ran away" | sed -e 's/big \(.*\) dog \(.*\)/small \1 cat \2/'
a small black eared cat ran away

# 3  
Quote:
Originally Posted by blowtorch
Does this help?
Code:
echo "a big black dog" | sed -e 's/big \(.*\) dog/small \1 cat/'
a small black eared cat

The \1 means that the first pattern that matches (.*) (any word) is put in. Similarly, another (second) matching pattern can also be put in. As an example:
Code:
echo "a big black eared dog ran away" | sed -e 's/big \(.*\) dog \(.*\)/small \1 cat \2/'
a small black eared cat ran away

Torch,

this is the not the way it works
Code:
echo "a big black dog" | sed -e 's/big \(.*\) dog/small \1 cat/'
a small black cat

Shouldn't the echo statement be as,
Code:
echo "a big black eared dog"

# 4  
Ok guys, thanks for the confusion lol. Hope someone can clarify for sure.
# 5  
Sed -e "s/big \(.*\) dog/small \1 cat/" filename

Explanations:

1> -e - is used when using multiple replacements usinf regular expressions or not
2> you are searching for "big" and "anything after that" and then "dog"
3> you are replacing "big" with "small" and "dog" with "cat" without deterioriating the "anything after that" text

when sed searches, it remembers the \(.*\) and it can be accessed using \1.

hope this helps.


Cheers,
Devaraj Takhellambam
# 6  
Thanks Devtakh, that helped alot actually.
# 7  
Sorry for the confusion. I copied the wrong output there.
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #497
Difficulty: Medium
A computer compiler or interpreter is just as smart as a human.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Question on sed

How does sed substitutes a string of characters in a file and replace it with a string passed as an argument to a variable in a script and saves the file with the changes. Say for example read name sed 's/string1/$name/g filename BTW I am using /bin/sh (4 Replies)
Discussion started by: Tirmazi
4 Replies

2. UNIX for Advanced & Expert Users

SED Question

awk "/^$YYEAR/$YMNTH/$YCDAY I wont to turn the above into: awk "/^$YYEAR\/$YMNTH\/$YCDAY How can I do that? I tried the below but it didn't work: echo awk "/^$YYEAR/$YMNTH/$YCDAY | sed 's~/$~\/$~g' (4 Replies)
Discussion started by: SkySmart
4 Replies

3. Shell Programming and Scripting

Sed question

I have following input: almantas 31949 1 0 00:00:08 ? /usr/bin/pulseaudio -D --log-target=syslog almantas 31979 12082 0 00:00:06 pts/2 bash almantas 31991 31949 0 00:00:00 ? /usr/lib/pulseaudio/pulse/gconf-helper almantas 32009 1 0 00:00:06 ? ... (3 Replies)
Discussion started by: eXPlosion
3 Replies

4. Shell Programming and Scripting

Sed Question 1. (Don't quite know how to use sed! Thanks)

Write a sed script to extract the year, rank, and stock for the most recent 10 years available in the file top10_mktval.csv, and output in the following format: ------------------------------ YEAR |RANK| STOCK ------------------------------ 2007 | 1 | Exxon... (1 Reply)
Discussion started by: beibeiatNY
1 Replies

5. Shell Programming and Scripting

Another sed question

Hello, I am very new to shell scripting and have a directory path such as: /usr/dev/blah/Arch/release/812-1235-P05/files/list and I want to output: 812-1235-P05 I think using sed with a regex like --? would be the way to go, but I am having much trouble getting it to work. Any suggestions?... (4 Replies)
Discussion started by: phreezr
4 Replies

6. Filesystems, Disks and Memory

Question about 'sed'

Hi all: I understand that the following action: sed '1s/a/b/' f will search the first line of f and find the first ocurrence of a then replaced by b. But, instead of specify a character or string, is there any way I can specify the column(s) we want to replace then replace it by using 'sed'?... (0 Replies)
Discussion started by: heartsunny2000
0 Replies

7. UNIX for Dummies Questions & Answers

sed question

Hello, I have a file where I need to search for a word in a line and then break the line into two lines. I have the following file and I want the below result File1: PROJ=Workflow System CPPDOC=QUOTE PROJ=center Workflow System CPPDOC=CONTRACT PROJ=Workflow System CPPDOC=CPP CENTER ... (4 Replies)
Discussion started by: orahi001
4 Replies

8. Shell Programming and Scripting

Sed question

echo "SOME" | sed -e :a -e "s/^.\{1,$COUNT\}$/ &/;ta" Could someone please, tell me how does this script work? Also, what is ":a" and what for what it's needed? (1 Reply)
Discussion started by: mirusnet
1 Replies

9. UNIX for Dummies Questions & Answers

sed question

I have this line "%is xxx = 123" in a program. I want to use sed command to replace it with something else but part of this line is not the same all the time. For example, it could be "%is xxx = 456" (and these three number change all the time) I have: sed 's/%is xxx = 123/my... (3 Replies)
Discussion started by: bobo
3 Replies

10. Shell Programming and Scripting

sed question (again)

hello there, I have a sed question. I have a file (temp.srv), in it it has v1_host1 v2_host2 And I have another file (temp2.srv), in it is has v1_host3_date v1_host1 v2_host2 v2_host4_date v3_host5_date I had used a script to remove the name from temp2.srv base on the name inside... (3 Replies)
Discussion started by: ahtat99
3 Replies

Featured Tech Videos