sed very confused


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed very confused
# 1  
Old 04-11-2008
sed very confused

Hello experts,
I have this complicated code that output value in between pattern and add "a string" to the front of the output.
The problems I have many pattern that are the same.
so I want to know how to get the value between 1st pattern, 2nd pattern etc.
Any suggestions?

sed -n '/<pattern>/s/.*>\(.*\)<.*/a string\1/gp' file


file:
<Pattern>file<\Pattern>
<Pattern>file2<\Pattern>
..
<Pattern>file3<\Pattern>
..
<Pattern>file4<\Pattern>

sed -n '/<pattern>/s/.*>\(.*\)<.*/string=\1/gp' file
returns:

string=file
string=file2
string=file3
string=file4

but I want a sed command that gets only the value between the 3rd occurrence of pattern

string=file3

Last edited by minifish; 04-11-2008 at 06:04 PM..
# 2  
Old 04-11-2008
Please post examples.

Jean-Pierre.
# 3  
Old 04-11-2008
Quote:
Originally Posted by aigles
Please post examples.

Jean-Pierre.
ok

so
file:
<Pattern>file<\Pattern>
<Pattern>file2<\Pattern>
..
<Pattern>file3<\Pattern>
..
<Pattern>file4<\Pattern>

sed -n '/<pattern>/s/.*>\(.*\)<.*/string=\1/gp' file
returns:

string=file
string=file2
string=file3
string=file4

but I want a sed command that gets only the value between the 3rd occurrence of pattern

string=file3

Last edited by minifish; 04-11-2008 at 05:34 PM..
# 4  
Old 04-11-2008
original sed command:
sed -n '/<pattern>/s/.*>\(.*\)<.*/string=\1/gp' file
# 5  
Old 04-11-2008
Quote:
Originally Posted by minifish
but I want a sed command that gets only the value between the 3rd occurrence of pattern
string=file3
Code:
sed -n '3,/<pattern>/s/.*>\(.*\)<.*/string=\1/gp' file

# 6  
Old 04-14-2008
Quote:
Originally Posted by danmero
Code:
sed -n '3,/<pattern>/s/.*>\(.*\)<.*/string=\1/gp' file

hm.. I tried this one before, it output all values up to the 1st pattern.

so
file:
<before>blah<\before>
<before>blah1<\before>
<before>blah2<\before>
<Pattern>file<\Pattern>
..
<Pattern>file2<\Pattern>

outputs:

string=blah
string=blah1
string=blah2
string=file
# 7  
Old 04-14-2008
Code:
awk 'BEGIN{FS="<|>"}/<Pattern>/{c++}c==3{print "string="$3}' file

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Confused by the sed command

Hi Gurus, I am a little bit confused by the sed command. my file is below cat sample aaa bbb ccc ddd eee fff ggg hhh iii below is command and output. (6 Replies)
Discussion started by: ken6503
6 Replies

2. UNIX for Dummies Questions & Answers

Confused with sed...

Yes I know this is a bad use of cat, but just ignore it please :P My problem is with sed. File: "newFile" contains: day Command I run: cat newFile | sed 's/day/night' This returns: sed: -e expression #1, char 11: unterminated `s' command Why??? (1 Reply)
Discussion started by: maximus73
1 Replies

3. Shell Programming and Scripting

SED - confused by the s command.

echo "abc 123" | sed 's/*/& &/g' output: a b c 123 123 Why there are spaces between the "abc" letters? echo "abc 123" | sed 's/*/&&/' output: abc 123 Why the regex in the above script does not match anything? I thought * should match 123 in any case. ---------- Post updated at 08:25... (3 Replies)
Discussion started by: kevintse
3 Replies

4. Ubuntu

New and Confused

Hello, I am having a problem with Dual Booting Windows XP Pro and Linux Mint. I have Three Hard Drives, One Hard Drive has Linux Mint Loaded on it. When it is hooked up to the computer by itself it works great. This is an IDE Drive. The Second Hard Drive has Window XP Pro loaded on it.... (3 Replies)
Discussion started by: Forextrading
3 Replies

5. Solaris

Confused c#t#d#s#

I am confused c#t#d#s# once I learn the following : slice 0 ...... 0 to 2520 slice 1....... 2521 to 2840 slice 6........2841 to 8891 slice 2........0 to 8891 really really confused. Please explain. (8 Replies)
Discussion started by: deltakutty
8 Replies

6. Shell Programming and Scripting

confused with cp

may i know what cp $1 $2 $0 $2 does? (12 Replies)
Discussion started by: C|[anti-trust]
12 Replies

7. UNIX for Dummies Questions & Answers

confused

hi! how when i'm chattin inside com there was this chatter andi don't know what he did but he saw all my files inside my shell. what did he do? (4 Replies)
Discussion started by: hapiworm
4 Replies

8. UNIX for Dummies Questions & Answers

confused,,,,

Hi,,, is there any possibility to install Linux in my P.C which is use Win98 without loose anything from my hard disk???? ---------------------------------------------------------------------------------- Is it better for a newbie in this kind of OS to install Linux instead of... (5 Replies)
Discussion started by: spyros
5 Replies
Login or Register to Ask a Question