sed, awk help

sed, awk help

Post by yan » Fri, 18 Apr 2003 22:27:33



how to identify the first occurence of a patten by using sed or awk

e.g.:  my file is as following.  I want to identify
the lines which contains the first occurence of "\"

aa aaaaaaaaaa \
bbbbb      cccc \
ddddd ddddddd
eeeeeeee eesss \
ffffff dddddddddd

so the line I want to be printed out are:
aa aaaaaaaaaa \
eeeeeeee eesss \

(note: "bbbbb      cccc \" is not the one i want because the "\" is
the second
occurence, not the first occurence)

great thanks advance!

--
Posted via http://dbforums.com

 
 
 

sed, awk help

Post by j.. » Fri, 18 Apr 2003 23:52:53



> how to identify the first occurence of a patten by using sed or awk

> e.g.:  my file is as following.  I want to identify
> the lines which contains the first occurence of "\"

> aa aaaaaaaaaa \
> bbbbb      cccc \
> ddddd ddddddd
> eeeeeeee eesss \
> ffffff dddddddddd

> so the line I want to be printed out are:
> aa aaaaaaaaaa \
> eeeeeeee eesss \

You just need to keep a flag that tells when to print and when not
to. Here's an awk solution, but it can probably be done in the shell
too.

awk '
  BEGIN {first = 1}
  /\\/ {
  if(first)
  {
    print $0
    first=0
  }
  else
    first = 1

Quote:}

' < data

 
 
 

sed, awk help

Post by Stephane CHAZELA » Sat, 19 Apr 2003 00:39:21



> how to identify the first occurence of a patten by using sed or awk

> e.g.:  my file is as following.  I want to identify
> the lines which contains the first occurence of "\"

> aa aaaaaaaaaa \
> bbbbb      cccc \
> ddddd ddddddd
> eeeeeeee eesss \
> ffffff dddddddddd

> so the line I want to be printed out are:
> aa aaaaaaaaaa \
> eeeeeeee eesss \

[...]

sed -ne '/\\/!d;p;:1' -e '/\\/{n;b1' -e '}'

--
Stphane

 
 
 

sed, awk help

Post by yan » Sat, 19 Apr 2003 03:23:06


sed -ne '/\\/!d;p;:1' -e '/\\/{n;b1' -e '}'

I got an error message. I thought may be typo but I check it very
carefully. thanks!

--
Posted via http://dbforums.com

 
 
 

sed, awk help

Post by yan » Sat, 19 Apr 2003 03:19:09


sorry, the awk solution won't work.
if I have one more line as following:
aa aaaaaaaaaa \
bbbbb cccc \
ddddd ddddddd \
hhhhhhhh
eeeeeeee eesss \
ffffff dddddddddd

then it print out
aa aaaaaaaaaa \
ddddd ddddddd \

but i need
aa aaaaaaaaaa \
eeeeeeee eesss \

--
Posted via http://dbforums.com

 
 
 

sed, awk help

Post by j.. » Sat, 19 Apr 2003 04:17:02



> sorry, the awk solution won't work.
> if I have one more line as following:
> aa aaaaaaaaaa \
> bbbbb cccc \
> ddddd ddddddd \
> hhhhhhhh
> eeeeeeee eesss \
> ffffff dddddddddd

> then it print out
> aa aaaaaaaaaa \
> ddddd ddddddd \

> but i need
> aa aaaaaaaaaa \
> eeeeeeee eesss \

Ok, I thought you wanted every other line containing '\'. If what you
really want is to print out lines containing '\' only after a line not
containing '\' adjust the counter accordingly.

awk '
  BEGIN {first = 1}
  {
    if($0 ~ /\\/)
    {
      if(first)
      {
        print $0
        first = 0
      }
    }
    else
      first = 1
  }
' < data

 
 
 

sed, awk help

Post by William Par » Sat, 19 Apr 2003 04:30:58



> how to identify the first occurence of a patten by using sed or awk

> e.g.:  my file is as following.  I want to identify
> the lines which contains the first occurence of "\"

> aa aaaaaaaaaa \
> bbbbb      cccc \
> ddddd ddddddd
> eeeeeeee eesss \
> ffffff dddddddddd

> so the line I want to be printed out are:
> aa aaaaaaaaaa \
> eeeeeeee eesss \

> (note: "bbbbb      cccc \" is not the one i want because the "\" is
> the second
> occurence, not the first occurence)

> great thanks advance!

grep -A 1 | grep '\$'

man grep

--

Linux solution for data management and processing.

 
 
 

sed, awk help

Post by rakesh shar » Sat, 19 Apr 2003 09:58:51



> how to identify the first occurence of a patten by using sed or awk

> e.g.:  my file is as following.  I want to identify
> the lines which contains the first occurence of "\"

> aa aaaaaaaaaa \
> bbbbb      cccc \
> ddddd ddddddd
> eeeeeeee eesss \
> ffffff dddddddddd

> so the line I want to be printed out are:
> aa aaaaaaaaaa \
> eeeeeeee eesss \

> (note: "bbbbb      cccc \" is not the one i want because the "\" is
> the second
> occurence, not the first occurence)

> great thanks advance!

put these lines in a runfile, then invoke sed on them:  sed -f runfile Input

:begin
/\\$/{
        $b
        :loop
        N
        /\\$/bloop
        P
        s/^.*\n//
        bbegin

Quote:}

d
 
 
 

sed, awk help

Post by rakesh shar » Sat, 19 Apr 2003 10:53:59



> how to identify the first occurence of a patten by using sed or awk

> e.g.:  my file is as following.  I want to identify
> the lines which contains the first occurence of "\"

> aa aaaaaaaaaa \
> bbbbb      cccc \
> ddddd ddddddd
> eeeeeeee eesss \
> ffffff dddddddddd

> so the line I want to be printed out are:
> aa aaaaaaaaaa \
> eeeeeeee eesss \

> (note: "bbbbb      cccc \" is not the one i want because the "\" is
> the second
> occurence, not the first occurence)

> great thanks advance!

there were some gotchas(eg,last 2 lines w/ \) in my previous version;

/\\$/!d
:loop
$!N
/\\$/{
        $!bloop

Quote:}

P
d
 
 
 

sed, awk help

Post by John Savag » Sun, 20 Apr 2003 08:33:36



>sed -ne '/\\/!d;p;:1' -e '/\\/{n;b1' -e '}'

>I got an error message. I thought may be typo but I check it very
>carefully. thanks!

And that error message was exactly what?

Some seds require labels to stand alone, so try rearranging as:

sed -ne '/\\/!d;p' -e ':1' -e '/\\/{n;b1;}'
--
John Savage      (newsgroup email invalid; keep news replies in newsgroup)

 
 
 

1. sed/awk help

I would like to search for a number of strings in a number of files.
Then I would like it to replace or delete the string based on what it is
(like in a case statement). The strings can be either hardcoded or
(preferably) read in from a file.

How can I do this using sed or awk?
Much appreciated.

tw-
Thet Win
UCLA

2. Needs someone's .Xdefaults

3. sed/awk - generating Makefiles (Re: awk processing of Makefile macros)

4. cannot ftp to my computer!

5. Help with awk/sed word extracting

6. Problem with S3 Trio3D under X

7. sed and awk help needed

8. Multiprocessor Linux ???

9. Help with Sed or Awk task

10. Awk, Sed Help

11. help sed or awk task

12. beginner: sed or awk help

13. sed or awk? Multiline editing help needed