sed very confused


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed very confused
# 8  
Old 04-14-2008
The same thing that Franklin52 :
Code:
awk -F'[<>]' '$2=="Pattern" && ++occ==3 {print "string="$3; exit}' file

Jean-Pierre.
# 9  
Old 04-14-2008
Quote:
Originally Posted by Franklin52
Code:
awk 'BEGIN{FS="<|>"}/<Pattern>/{c++}c==3{print "string="$3}' file

Regards
mm..I'm not very familiar with awk, but it's not giving what I want.
it outputs something like:

string=\Pattern>
string=
string=
# 10  
Old 04-14-2008
Quote:
Originally Posted by aigles
The same thing that Franklin52 :
Code:
awk -F'[<>]' '$2=="Pattern" && ++occ==3 {print "string="$3; exit}' file

Jean-Pierre.
mm..I get an empty output.
# 11  
Old 04-14-2008
No problem on my AIX box.
Code:
$ cat pattern.txt
<Pattern>file<\Pattern>
<Pattern>file2<\Pattern>
..
<Pattern>file3<\Pattern>
..
<Pattern>file4<\Pattern>

$ awk -F'[<>]' '$2=="Pattern" && ++occ==3 {print "string="$3; exit}' pattern.txt
string=file3
$

Try with nawk instead of awk.

@Franklin52
Your solution prints lines from the 3rd occurence of pattern to the end of the file (I haven't detect the problem when i first read your post).

Jean-Pierre.
# 12  
Old 04-14-2008
Quote:
Originally Posted by aigles
No problem on my AIX box.
Code:
$ cat pattern.txt
<Pattern>file<\Pattern>
<Pattern>file2<\Pattern>
..
<Pattern>file3<\Pattern>
..
<Pattern>file4<\Pattern>

$ awk -F'[<>]' '$2=="Pattern" && ++occ==3 {print "string="$3; exit}' pattern.txt
string=file3
$

Try with nawk instead of awk.

@Franklin52
Your solution prints lines from the 3rd occurence of pattern to the end of the file (I haven't detect the problem when i first read your post).

Jean-Pierre.
THANK YOU~~~~~~~~~~~~~~~~~~~~~~~~~~
Smilie
# 13  
Old 04-14-2008
Quote:
Originally Posted by aigles
No problem on my AIX box.
Code:
$ cat pattern.txt
<Pattern>file<\Pattern>
<Pattern>file2<\Pattern>
..
<Pattern>file3<\Pattern>
..
<Pattern>file4<\Pattern>

$ awk -F'[<>]' '$2=="Pattern" && ++occ==3 {print "string="$3; exit}' pattern.txt
string=file3
$

Try with nawk instead of awk.

@Franklin52
Your solution prints lines from the 3rd occurence of pattern to the end of the file (I haven't detect the problem when i first read your post).

Jean-Pierre.
It works fine for me, did you try it? It prints only if c==3 but with an exit statement it's faster for a big file.

Regards
# 14  
Old 04-14-2008
Quote:
Originally Posted by Franklin52
It works fine for me, did you try it? It prints only if c==3 but with an exit statement it's faster for a big file.

Regards
Your command prints line for the 3rd occurrence of Pattern and for all following lines until next occurence of Pattern.
Adding the exit statement resolves the problem.

Code:
$ cat pattern.txt
<Pattern>file<\Pattern>
<Pattern>file2<\Pattern>
..
<Pattern>file3<\Pattern>
.. 
...
....<Pattern>file4<\Pattern>

$ awk 'BEGIN{FS="<|>"}/<Pattern>/{c++}c==3{print "string="$3}' pattern.txt
string=file3
string=
string=
string=
$ awk 'BEGIN{FS="<|>"}/<Pattern>/{c++}c==3{print "string="$3;exit}' pattern.txt
string=file3
$

Jean-Pierre.
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