Get number of lines after certain string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get number of lines after certain string
# 1  
Old 11-27-2013
Get number of lines after certain string

Hello people,
I have a file like that:
Code:
start something
1 xxx yyy
2 zzz xxx
3 ccc vvv
4 mmm nnn
stop something
1 aaa bbb
2 ccc ddd
3 eee fff
4 qqq www
start something
1 rrr ttt
2 yyy uuu
3 iii ooo
4 ppp sss

And I need only line starting with 1 and 3 and only after start something.
Output like that:
Code:
start,1,xxx,yyy
start,3,ccc,vvv
start,1,rrr,ttt
start,3,iii,ooo

Could you please help. Thanks!
# 2  
Old 11-27-2013
for i in $(egrep(^1|3) file| sed 's/ /,/'); do echo start,$i;done
# 3  
Old 11-27-2013
Try
Code:
awk '/start/,/stop/ {if (($1-2)^2 == 1) printf "start,%s,%s,%s\n", $1, $2, $3}' file
start,1,xxx,yyy
start,3,ccc,vvv
start,1,rrr,ttt
start,3,iii,ooo

This User Gave Thanks to RudiC For This Post:
# 4  
Old 11-27-2013
Quote:
Originally Posted by Skrynesaver
for i in $(egrep(^1|3) file| sed 's/ /,/'); do echo start,$i;done
Trying this I get:
Code:
bash: command substitution: line 1: syntax error near unexpected token `^1'
bash: command substitution: line 1: `egrep(^1|3) file.txt | sed 's/ /,/''

---------- Post updated at 05:33 PM ---------- Previous update was at 05:28 PM ----------

Quote:
Originally Posted by RudiC
Try
Code:
awk '/start/,/stop/ {if (($1-2)^2 == 1) printf "start,%s,%s,%s\n", $1, $2, $3}' file
start,1,xxx,yyy
start,3,ccc,vvv
start,1,rrr,ttt
start,3,iii,ooo

It is working! Thanks a lot!

---------- Post updated at 06:33 PM ---------- Previous update was at 05:33 PM ----------

Quote:
Originally Posted by RudiC
Try
Code:
awk '/start/,/stop/ {if (($1-2)^2 == 1) printf "start,%s,%s,%s\n", $1, $2, $3}' file
start,1,xxx,yyy
start,3,ccc,vvv
start,1,rrr,ttt
start,3,iii,ooo

One additional question, is it possible to be done in a similar way if the line starts with string? E.g.
Code:
start something
one xxx yyy
two zzz xxx
three ccc vvv
four mmm nnn
stop something
one aaa bbb
two ccc ddd
three eee fff
four qqq www
start something
one rrr ttt
two yyy uuu
three iii ooo
four ppp sss

Thanks!
# 5  
Old 11-27-2013
Try
Code:
awk '/start/,/stop/ {if ($1~/^one|^three/) printf "start,%s,%s,%s\n", $1, $2, $3}' file
start,one,xxx,yyy
start,three,ccc,vvv
start,one,rrr,ttt
start,three,iii,ooo

This User Gave Thanks to RudiC For This Post:
# 6  
Old 11-27-2013
Using RudiC's approach

Code:
awk '
         BEGIN{
               num = "one two three four five six seven eight nine ten"
                 n = split(num,A)
               for(i=1;i<=n;i++)NUM[A[i]]=i
              }
/start/,/stop/{
               if ((NUM[tolower($1)]-2)^2 == 1)
               printf "start,%s,%s,%s\n", $1, $2, $3
              }
   ' file

Code:
start,one,xxx,yyy
start,three,ccc,vvv
start,one,rrr,ttt
start,three,iii,ooo

This User Gave Thanks to Akshay Hegde For This Post:
# 7  
Old 11-27-2013
Quote:
Originally Posted by RudiC
Try
Code:
awk '/start/,/stop/ {if ($1~/^one|^three/) printf "start,%s,%s,%s\n", $1, $2, $3}' file
start,one,xxx,yyy
start,three,ccc,vvv
start,one,rrr,ttt
start,three,iii,ooo

Thanks a lot RudiC!

---------- Post updated at 10:41 PM ---------- Previous update was at 10:39 PM ----------

Quote:
Originally Posted by Akshay Hegde
Using RudiC's approach

Code:
awk '
         BEGIN{
               num = "one two three four five six seven eight nine ten"
                 n = split(num,A)
               for(i=1;i<=n;i++)NUM[A[i]]=i
              }
/start/,/stop/{
               if ((NUM[tolower($1)]-2)^2 == 1)
               printf "start,%s,%s,%s\n", $1, $2, $3
              }
   ' file

Code:
start,one,xxx,yyy
start,three,ccc,vvv
start,one,rrr,ttt
start,three,iii,ooo

I'm getting syntax error, will try to figure it out. Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print number of lines for files in directory, also print number of unique lines

I have a directory of files, I can show the number of lines in each file and order them from lowest to highest with: wc -l *|sort 15263 Image.txt 16401 reference.txt 40459 richtexteditor.txt How can I also print the number of unique lines in each file? 15263 1401 Image.txt 16401... (15 Replies)
Discussion started by: spacegoose
15 Replies

2. Shell Programming and Scripting

Add string number to lines

So I have a file in the form > akdfvcnciejcndmdjfk > kdjkkkifjeeeeelfjfuf > fjfhchdejhfhfhfhfhfhf > skdkdhfhvnvncnccm and I would like it to come out in the form >1 akdfvcnciejcndmdjfk >2 kdjkkkifjeeeeelfjfuf >3 fjfhchdejhfhfhfhfhfhf (3 Replies)
Discussion started by: viored
3 Replies

3. Shell Programming and Scripting

Help in printing n number of lines if a search string matches in a file

Hi I have below script which is used to grep specific errors and if error string matches send an email alert. Script is working fine , however , i wish to print next 10 lines of the string match to get the details of error in the email alert Current code:- #!/bin/bash tail -Fn0 --retry... (2 Replies)
Discussion started by: neha0785
2 Replies

4. Shell Programming and Scripting

Concatenate lines with unique string AND number

In Bash using AWK or sed I need to convert the following file: ... numitem_tab0 =<p>1 KEYWORD</p><p>2 KEYWORD</p><p>3 KEYWORD</p><p>4 KEYWORD</p><p>5 KEYWORD</p>...<p>25 KEYWORD</p> subitem_tab0 =<p></p><p></p> ... numitem_tab6 =<p>1 KEYWORD</p><p>2 KEYWORD</p><p>3 KEYWORD</p><p>4 KEYWORD</p>... (2 Replies)
Discussion started by: pioavi
2 Replies

5. Shell Programming and Scripting

Merge two non-consecutive lines based on line number or string

This is a variation of an earlier post found here: unixcom/shell-programming-scripting/159821-merge-two-non-consecutive-lines.html User Bartus11 was kind enough to solve that example. Previously, I needed help combining two lines that are non-consecutive in a file. Now I need to do the... (7 Replies)
Discussion started by: munkee
7 Replies

6. Shell Programming and Scripting

Reading n number of lines after finding string

I am trying to search a file for a value: "Top 30 reject reasons" and want the next 30 lines after that and output in a text file. If I knew the line number, I can use a combination of head and tail commands to get my results, but this doesn't seem to work when I don't have a line number. I... (2 Replies)
Discussion started by: oriqin
2 Replies

7. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 Replies

8. UNIX for Dummies Questions & Answers

Number of lines containing a certain string

Hey I want to know if there is an option to know the number of lines containing a certain string (bit for example) in a file? Say I want to know number of lines containing only the string BIT in file xyz. I know how to get number of lines in a file by using wc -l but how do you get number of lines... (1 Reply)
Discussion started by: #moveon
1 Replies

9. Shell Programming and Scripting

How to print the number of lines from a file, the starting string should be passed`

Hi , I have file, which has the below content: line 100 a b c d line300 a s d f s line200 a s d a (3 Replies)
Discussion started by: little_wonder
3 Replies

10. Shell Programming and Scripting

searching and storing unknown number of lines based on the string with a condition

Dear friends, Please help me to resolve the problem below, I have a file with following content: date of file creation : 12 feb 2007 ==================== = name : suresh = city :mumbai #this is a blank line = date : 1st Nov 2005 ==================== few lines of some text this... (7 Replies)
Discussion started by: swamymns
7 Replies
Login or Register to Ask a Question