Extract text in sed using back reference


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extract text in sed using back reference
# 1  
Old 12-21-2015
Extract text in sed using back reference

i have a text

20 21 22 23 24 25 26


i want to get 22 using sed back reference.
I have used
HTML Code:
sed 's/[^.]{6}\(..\).*/\1/'
but, it does not work.

I am missing something somewhere.

Please help.
# 2  
Old 12-21-2015
You're close:
Code:
sed 's/^.\{6\}\(..\).*$/\1/' file
22

# 3  
Old 12-21-2015
Quote:
Originally Posted by RudiC
Code:
sed 's/^.\{6\}\(..\).*$/\1/' file
22

This is, of course, most probably what the thread-o/p intended. Still, i wonder:

Code:
sed 's/[^.]{6}\(..\).*/\1/'

why this didn't work: the expression searches for 6 consecutive non-"."-characters and takes the next two into the variable. This should have produced "22" too, no?

Or was the part of the text not at the beginning of a line? In this case RudiCs solution will only work after some modification either, i suppose. @Thread-o/p: could you please show the complete input and output of your original statement?

I hope this helps.

bakunin
# 4  
Old 12-21-2015
By default sed uses BRE (Basic Regular Expressions) which requires the {m,n} quantifiers to be escaped, as \{m,n\}
# 5  
Old 12-22-2015
many thanks RudiC and all for the quick response.
thanks Aia for answering bakunin.

Any pointer / reference to sed back reference site / book plz.
# 6  
Old 12-22-2015
Try the O'Reilly book on sed and awk, or check out the POSIX specification:
BREs Matching Multiple Characters (arrived at, through Regular Expressions in sed on the POSIX sed page).

Last edited by Scrutinizer; 12-22-2015 at 06:38 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - use back reference in 2nd command

I have data that looks like this: <Country code="US"><tag>adsf</tag><tag>bdfs</tag></Country><Country code="CA"><tag>asdf</tag><tag>bsdf</tag></Country> I want to grab the country code save it, then drop each new "<..." onto a new line with the country code added to the beginning of each So,... (9 Replies)
Discussion started by: JenniferAmon
9 Replies

2. Shell Programming and Scripting

sed back reference error

I am trying to change a single line of a special file whose comment character is ! to show a path to the file in the comment. such as: !!HFSS and mcm path: \Signal_Integrity\Package_SI\Section_Models\C4toTrace\28nm\D6HS\SLC_5-2-5\GZ41_ICZ\NSSS\ to a different path and replace the !!HFSS... (1 Reply)
Discussion started by: mobrien601
1 Replies

3. UNIX for Dummies Questions & Answers

Invalid back reference

The thread can be closed now :D. (3 Replies)
Discussion started by: vaz0r
3 Replies

4. Shell Programming and Scripting

Shell Scripting Problem - Invalid Back Reference

Here is the question... Create a new script, sub2, taking three parameters... 1.) the string to be replaced 2.) the string with which to replace it 3.) the name of the file in which to make the substitution ...that treats the string to be replaced as plain text instead of as a regular... (1 Reply)
Discussion started by: johnhisenburg
1 Replies

5. Shell Programming and Scripting

sed - extract text from xml file

hi, please help, i have an xml file, e.g: ... <tag> test text asdas="${abc}" xvxvbs:asdas${222}sdad asasa="${aa_bb_22}" </tag> ... i want to extract all "${...}", e.g: ${abc} ${222} ${aa_bb_22} thank you. (2 Replies)
Discussion started by: gioni
2 Replies

6. Shell Programming and Scripting

Extract word from text (sed,awk, etc...)

Hello, I need some help extracting the number after the RBA e.g 15911688 from the below block of text (e.g: grep RBA |sed .......). The code should be valid for blocks if text generated at different times as well and not for the below text only. ... (2 Replies)
Discussion started by: drbiloukos
2 Replies

7. Shell Programming and Scripting

SED to extract HTML text data, not quite right!

I am attempting to extract weather data from the following website, but for the Victoria area only: Text Forecasts - Environment Canada I use this: sed -n "/Greater Victoria./,/Fraser Valley./p" But that phrasing does not sometimes get it all and think perhaps the website has more... (2 Replies)
Discussion started by: lagagnon
2 Replies

8. UNIX for Dummies Questions & Answers

Using awk/sed to extract text between Strings

Dear Unix Gurus, I've got a data file with a few hundred lines (see truncated sample)... BEGIN_SCAN1 TASK_NAME=LA48 PDD Profiles PROGRAM=ArrayScan 1.00 21.220E+00 2.00 21.280E+00 END_DATA END_SCAN1 BEGIN_SCAN2 TASK_NAME=LA48 PDD Profiles 194.00 2.1870E+00 ... (5 Replies)
Discussion started by: tintin72
5 Replies

9. Shell Programming and Scripting

Perl: Getting back reference from s modifier

My input text has the following pattens: func_a(3, 4, 5); I want to replace it with this: func_b(3, 4, 5, 6); I'm trying the following expression, but it does not work: perl -p -e "s/func_a\((.*)?\);/func_b(\1,\n6)/s" <... (8 Replies)
Discussion started by: cooldude
8 Replies

10. Shell Programming and Scripting

back reference error

Hi, i am getting this error........ find ./ | sed '/\(*\) \(*\)/\2\1/' Unrecognized command: /\(*\) \(*\)/\2\1/ Any idea??? regards Apoorva Kumar (4 Replies)
Discussion started by: apoorvasharma80
4 Replies
Login or Register to Ask a Question