sed pattern &

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers sed pattern &
# 1  
Old 04-09-2018
sed pattern &

Hi,

I can't seem to understand what the sed and & do.. Why is the set of numbers appear twice ? how the command really work ?

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


Output: 
123 www 123 abc


Last edited by uniran; 04-09-2018 at 12:46 PM..
# 2  
Old 04-09-2018
man sed yields:
Code:
       s/regexp/replacement/
              Attempt to match regexp against the pattern space.  If  success‐
              ful,   replace  that  portion  matched  with  replacement.   The
              replacement may contain the special character & to refer to that
              portion  of  the  pattern  space  which matched, and the special
              escapes \1 through \9 to refer  to  the  corresponding  matching
              sub-expressions in the regexp.

# 3  
Old 04-09-2018
Quote:
Originally Posted by vgersh99
man sed yields:
Code:
       s/regexp/replacement/
              Attempt to match regexp against the pattern space.  If  success‐
              ful,   replace  that  portion  matched  with  replacement.   The
              replacement may contain the special character & to refer to that
              portion  of  the  pattern  space  which matched, and the special
              escapes \1 through \9 to refer  to  the  corresponding  matching
              sub-expressions in the regexp.

I understood the supposed function of sed with &, but this explanation (I read something similar before posting) doesn't explain to me why the set of numbers appears twice, and how the output came to be this way..
# 4  
Old 04-09-2018
Quote:
Originally Posted by uniran
I understood the supposed function of sed with &, but this explanation (I read something similar before posting) doesn't explain to me why the set of numbers appears twice, and how the output came to be this way..
the numbers appear twice because there're 2 &-s separated by www.
Code:
echo "123 abc" | sed 's/[0-9]*/& www &/'

This User Gave Thanks to vgersh99 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern

I have a file like below. 2018.07.01, Sunday 09:27 some text 123456789 0 21 0.06 0.07 0.00 2018.07.02, Monday 09:31 some text 123456789 1 41 0.26 0.32 0.00 09:39 some text 456789012 1 0.07 0.09 0.09 09:45 some text 932469494 1 55 0.29 0.36 0.00 16:49 some text 123456789 0 48 0.12 0.15 0.00... (9 Replies)
Discussion started by: father_7
9 Replies

2. Shell Programming and Scripting

sed Range Pattern and 2 lines before Start Pattern

Hi all, I have been searching all over Google but I am unable to find a solution for a particular result that I am trying to achieve. Consider the following input: 1 2 3 4 5 B4Srt1--Variable-0000 B4Srt2--Variable-1111 Srt 6 7 8 9 10 End (3 Replies)
Discussion started by: y2jacky
3 Replies

3. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

4. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

5. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

6. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

7. Shell Programming and Scripting

sed & areas respectively sed & pyramiding

Hello everyone, i wonder if someone could give me an advice regarding the following problem using sed. Given ist a structure as shown below: <aaa>text1<b>text2</b>text3<c>text4</c>text5</aaa> Now I want to change the outer tag from "aaa" to "new" and replace all tags inside the outer tags... (4 Replies)
Discussion started by: Donaldinho
4 Replies

8. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

9. UNIX for Dummies Questions & Answers

Pattern matching New to Sed & Awk

Hello, Despite reading the Pattern Matching chapter in the O'Reilly Sed & Awk book several times and looking at numerous examples, I cannot seem to get any kind of conditional script to work in my awk scripts! I am able to do the basic awk and grep script to capture the data but when I do with... (0 Replies)
Discussion started by: pg55
0 Replies

10. Shell Programming and Scripting

SED: match pattern & delete matched lines

Hi all, I have the following data in a file x.csv: > ,this is some text here > ,,,,,,,,,,,,,,,,2006/11/16,0.23 > ,,,,,,,,,,,,,,,,2006/12/16,0.88 < ,,,,,,,,,,,,,,,,this shouldnt be deleted I need to use SED to match anything with a > in the line and delete that line, can someone help... (7 Replies)
Discussion started by: not4google
7 Replies
Login or Register to Ask a Question