Extracting the last occurence


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting the last occurence
# 8  
Old 05-03-2007
Quote:
Originally Posted by anbu23
Code:
sed -n -e ":a" -e "N;/\naaa bbb/s/.*\n//;$ p;b a" file



Hi anbu
please can you explain me the command you have written
# 9  
Old 05-03-2007
Quote:
Originally Posted by napster_san
Hi anbu
please can you explain me the command you have written
Code:
********
aaa bbb 2007
********
123
234
134
********
xxx yyy 2007
********
aaa
bbb
cccc
dd
********
aaa bbb 2007
********
mmm
kkkkk
pppppp
ccccccc

Code:
sed -n -e ":a" -e "N;/\naaa bbb/s/.*\n//;$ p;b a" file

"********" is in pattern space.
N causes to read next line and append it to pattern space and now pattern space has "********\naaa bbb 2007" with embedded newline character.
Now substitute command removes "********\n" in pattern space.
b a causes to branch control to label ":a". N causes to read next line and append it to pattern space.
This is continued till it reaches line "aaa bbb 2007". Once this line is reached substitue command removes all the lines before this line.
$ p when the last line is reached it will print the contents in pattern space
# 10  
Old 05-03-2007
Quote:
Originally Posted by Raynon
Hi All,
Looks very complicated, but it works.
Any simplier method like using the command "cut" ?
Raynon,
Here is a program written in shell.
If you know shell and programming logic, it is easy to understand:
Code:
typeset -i StarLines=0
SearchStr='aaa bbb 2007'
Stars='********'
ToPrint='N'
while read ILine
do
 if [ "${ILine}" = "${Stars}" ]; then
   StarLines=$StarLines+1
   if [ ${StarLines} -eq 3 ]; then
     StarLines=1
   fi
   if [ ${StarLines} -eq 1 ]; then
     ToPrint='N'
     continue
   fi
 fi
 if [ ${StarLines} -eq 1 ]; then
   if [ "${ILine}" = "${SearchStr}" ]; then
     ToPrint='Y'
     echo "======== Starting a new string ==========="
   fi
 fi
 if [ ${ToPrint} = 'Y' ]; then
   echo "$ILine"
 fi
done < input_file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Get the first occurence between two patterns

I have an output file which gives me the timely status of a server. Sample file: March 11 2014 21:10, 1, 2, 3, 4, 5, 6, 7, 8, 9, x, y, z... 21:05, 1, 2, 3, 4, 5, 6, 7, 8, 9, x, y, z... 21:00, 1, 2, 3, 4,... (3 Replies)
Discussion started by: rpm120
3 Replies

2. Shell Programming and Scripting

Print between patterns - first occurence, second occurence etc

I have a file # cat asasas AAAAAA 11 22 33 44 BBBBB NILNILNIL AAAAAA 22 33 44 55 66 77 88 BBBBB NILNILNIL (2 Replies)
Discussion started by: anil510
2 Replies

3. Shell Programming and Scripting

Percentage of occurence

Dear all, I have data like below and i need to add coloumn before the COUNT field to see the Percentage out of all COUNT field value for respective raw. ============================================= COUNT CODE sConnType tConnType... (6 Replies)
Discussion started by: Iroshan
6 Replies

4. UNIX for Dummies Questions & Answers

How to print first occurence

Hi there, how can i print the first pattern occurrence in a .log file? I want to print the filename of the first 17262? I tried but all I can do is print all the lines with the number 17262? I tried using awk and sed but nothing!:wall: I just want filename! Here´s an example: 17259... (3 Replies)
Discussion started by: BMatter
3 Replies

5. Shell Programming and Scripting

Change first occurence

I'm not getting the syntax correct to change a line only on the first occurrence: I've tried to change only the first match and I've tried to change the from the second match forward sed 's/<B>PT#/<tr><td class=\"pt1\" width=\"40%\"><B>pt#/1' $file > tmpfile.html sed ... (0 Replies)
Discussion started by: dba_frog
0 Replies

6. UNIX for Dummies Questions & Answers

Only one particular occurence needs to be substituted !!!

In a file, field separetor in line is irregular number of spaces, so I canot use field in sub function to get my charecter replaced with empty space. I would like to substitute only one perticular charecter with space at perticular posiotn, so I canot use perticular character as that may occur... (6 Replies)
Discussion started by: vaka
6 Replies

7. UNIX for Dummies Questions & Answers

First occurence from grep

Hi , supoose i have a file in which a word is repeated so many times. I just want the firts occurence of that word through grep and it should not go to the next one means get the first occurence and stop there. Suggest me some solutions. Thanks Namish (10 Replies)
Discussion started by: namishtiwari
10 Replies

8. UNIX for Advanced & Expert Users

First Occurence

Hi, This is the format of the file that i have StartDate:10/01/06 EndDate :10/02/06 Cno Ccode 1 10 2 11 StartDate:10/03/06 EndDate :10/04/06 Cno Ccode 2 13 4 12 StartDate:10/01/06 EndDate :10/02/06 (5 Replies)
Discussion started by: kkm_job
5 Replies

9. UNIX for Dummies Questions & Answers

grep the last occurence

is there a grep or awk one-line command to print the line of the last occurence of a string in a file? (3 Replies)
Discussion started by: apalex
3 Replies

10. UNIX for Dummies Questions & Answers

grep for certain occurence

I am using grep to pull out info from a file. The line I am searching for begins: START TIME - Tue Sep 11 16:40:00. There are mutiple lines of START TIME. I need the FIRST occurence ONLY. My grep is as follows: start="$( grep 'START TIME' filename | cut -c15-33)" If I echo or cat... (2 Replies)
Discussion started by: app4dxh
2 Replies
Login or Register to Ask a Question