If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Finding Line sequence

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-15-07, 11:05
bufsn bufsn is offline
Registered User
 
Join Date: Jun 2005
Posts: 17
Finding Line sequence

Hi,
I have a file like the following with altername lines starting with 'H' and 'S'

H0102.........
SLSC10299......
H0103.........
SLSC10299......
H0104.........
SLSC10299......

I want to delete all the 'H' and 'S' line which are not occuring in the above sequence
for eg, I want to delete the third line in the following
H0102.........
SLSC10299......
H0103.........
H0104.........
SLSC10299......

I want to delete the fifth line in the following
H0102.........
SLSC10299......
H0103.........
SLSC10299......
SLSC10299......
H0104.........
SLSC10299......
Can anyone please help?
Thanks.
Reply With Quote
  #2 (permalink)  
Old 06-15-07, 11:51
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
You can do something like that (if you are sue that lines start only with H or S) :
Code:
$ cat lseq.txt
H0102.........
SLSC10299......
H0103.........
H0104.........
SLSC10299......
SLSC10299......
H0104.........
SLSC10299......
$ awk -v seq=x '$0 !~ "^" seq { print; seq=substr($0,1,1) }' lseq.txt
H0102.........
SLSC10299......
H0103.........
SLSC10299......
H0104.........
SLSC10299......
$
Another way :
Code:
$ awk -v seq=S '
   seq=="H" && /^S/ { print; seq="S"; next }
   seq=="S" && /^H/ { print; seq="H"; next }
   ' lseq.txt
H0102.........
SLSC10299......
H0103.........
SLSC10299......
H0104.........
SLSC10299......
$
__________________
Jean-Pierre.
Reply With Quote
  #3 (permalink)  
Old 06-18-07, 09:32
bufsn bufsn is offline
Registered User
 
Join Date: Jun 2005
Posts: 17
Thanks a lot.
Reply With Quote
  #4 (permalink)  
Old 06-20-07, 11:53
bufsn bufsn is offline
Registered User
 
Join Date: Jun 2005
Posts: 17
H0102.........
SLSC10299......
H0103.........
H0104.........
SLSC10299......
H0105.........
SLSC10299......

In this case,
I want the following output

H0102.........
SLSC10299......
H0104.........
SLSC10299......
H0105.........
SLSC10299......

and not
H0102.........
SLSC10299......
H0103.........
SLSC10299......
H0105.........
SLSC10299......
Reply With Quote
  #5 (permalink)  
Old 06-20-07, 13:49
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Code:
$ awk '
   BEGIN { seq = "xx" }
   /^S/  { lineS = $0 }
   /^H/  { lineH = $0 }
   { seq = substr(seq, 2, 1) substr($0, 1, 1) }
   seq=="HS" { print lineH; print lineS }
   ' lseq.txt
H0102.........
SLSC10299......
H0104.........
SLSC10299......
H0104.........
SLSC10299......
$
__________________
Jean-Pierre.
Reply With Quote
  #6 (permalink)  
Old 06-20-07, 15:41
bufsn bufsn is offline
Registered User
 
Join Date: Jun 2005
Posts: 17
Perfect. Thanks.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On