Small Help on SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Small Help on SED
# 1  
Old 09-19-2006
Small Help on SED

Hi All,
I have come across a command

echo "123 abc" | sed 's/[0-9]*/& &/'

output is

123 123 abc

then i tried in different ways to get 123 abc abc as output.

Can u please explain me the importance of & in the above command.

Thank you
- Chanakya
# 2  
Old 09-19-2006
This is from the sed man page:
Code:
                  An ampersand (&) appearing in the  replacement
                  will  be  replaced  by the string matching the
                  RE.

This means that the sed will replace & with 123 (the string that matches the regular expression in this example). Since the & occurs twice, it is placed there twice.
# 3  
Old 09-19-2006
Thank you blowtorch ..
Expecting the same i tried

echo 123 abc | sed 's/*[a-z]/& &/' to get 123 abc abc as output but i could not ..can u guide me how to get the desired output
# 4  
Old 09-19-2006
try this:

Code:
echo 123 abc | sed 's/\([a-z].*\)/& &/'

# 5  
Old 09-19-2006
Try this :
Code:
echo "123 abc" | sed 's/[a-z]\{1,\}/& &/'

Another example of sed :
Code:
echo "++++ 123 abc ----" | sed 's/\([0-9]\{1,\}\)[[:space:]]*\([a-z]\{1,\}\)/Number=\1 Text=\2/'

Output:
++++ Number=123 Text=abc ----



Jean-Pierre.
# 6  
Old 09-19-2006
hi aigles,
thank you.. I got the required output. Can u explain me what is wrong in

echo 123 abc | sed 's/\([a-z].*\)/& &/' and if possible explain ur command also echo "123 abc" | sed 's/[a-z]\{1,\}/& &/'

-Chanakya
# 7  
Old 09-19-2006
The regex [a-z].* matches one alphabetic character and all following characters.
For example, the command :
Code:
echo 123 abc 456 | sed 's/\([a-z].*\)/& &/'

gives the ouput:
Code:
123 abc 456 abc 456

Code:
echo "123 abc" | sed 's/[a-z]\{1,\}/& &/'

The regex [a-z]\{1,\} matches one or more alphabtic character.
Some commands accept the following syntax [a-z]+

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

need a small help

Hi, sorry for inconvenience..wrong query posted. Thanks for your help. Thanks (1 Reply)
Discussion started by: kirankumar
1 Replies

2. What is on Your Mind?

A small irritation

I thought I post about a small irritation that I have been having for years, in fact ever since CDs came out, which is a good 25 years now. For some reason, CD player manufacturers do not design their tray to come out completely and leaving about 1cm or 2cms of the tray in the system, so that it is... (8 Replies)
Discussion started by: figaro
8 Replies

3. Shell Programming and Scripting

Need small help

Hi all, I have two files in my folder 1.index.jsp 2.maintenance.jsp Once hit the URL in IE,It will call the index.jsp file than application working fine. I want to some maintenance in my application, during the application maintenance than it will... (1 Reply)
Discussion started by: lkeswar
1 Replies

4. Shell Programming and Scripting

SED help, small problem

Hi, I have this sed command to grep a date from a filename for a script we have. I am awful with sed so I need help. Sometimes it works fine but other times it does not, see below. works bash-3.00# echo... (1 Reply)
Discussion started by: GermanJulian
1 Replies

5. Shell Programming and Scripting

sed parse small xml file

I have a tmp.xml file like: <?xml version="1.0" encoding="UTF-8"?> <Response> <Ip>193.143.121.198</Ip> <Status>OK</Status> <CountryCode>PL</CountryCode> <CountryName>Poland</CountryName> <RegionCode>82</RegionCode> <RegionName>Pomorskie</RegionName> <City>Gdansk</City> ... (9 Replies)
Discussion started by: unclecameron
9 Replies

6. Shell Programming and Scripting

Need small help

Hi, i have a client requirement to post the files into generic folder named as "source".file identification is to retrieve Publication code (Instance name) from the file name.So thereafter we move the files to different instances of specific folders for the same unix server.(means if the file... (2 Replies)
Discussion started by: kirankumar
2 Replies

7. Shell Programming and Scripting

small sed script on command line.

Can anyone help me get this small sed script to work in shell on the command line? I need it in a one liner really as i want to edit many scripts in a for loop and dont want to have to invoke a separate script each time. #!/bin/sh sed '/mailx\ -s.*$ { i\ #Comment above mailx line ... (5 Replies)
Discussion started by: lavascript
5 Replies

8. UNIX for Advanced & Expert Users

Two small queries

Query 1 : How to check if a directory already exists? If doesn't exist then create a new one. Query 2 : I want to put following text using a single echo statement into a log file and also want to retain the formatting of the text. How it can be... (3 Replies)
Discussion started by: skyineyes
3 Replies

9. UNIX for Dummies Questions & Answers

Sed working on lines of small length and not large length

Hi , I have a peculiar case, where my sed command is working on a file which contains lines of small length. sed "s/XYZ:1/XYZ:3/g" abc.txt > xyz.txt when abc.txt contains lines of small length(currently around 80 chars) , this sed command is working fine. when abc.txt contains lines of... (3 Replies)
Discussion started by: thanuman
3 Replies

10. UNIX for Dummies Questions & Answers

small unix!

i'm looking to put a very minimal unix on a 386 i just acquired. it needs to be downloadable and i need to install it using 3.5 floppies! thanks for suggestions, -nydel (4 Replies)
Discussion started by: nydel
4 Replies
Login or Register to Ask a Question