Get number of lines after certain string

Hello people,
I have a file like that:

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:

start,1,xxx,yyy
start,3,ccc,vvv
start,1,rrr,ttt
start,3,iii,ooo

Could you please help. Thanks!

for i in $(egrep(^1|3) file| sed 's/ /,/'); do echo start,$i;done

Try

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
1 Like

Trying this I get:

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 ----------

It is working! Thanks a lot!

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

One additional question, is it possible to be done in a similar way if the line starts with string? E.g.

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!

Try

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
1 Like

Using RudiC's approach

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
              }
/start/,/stop/{
               if ((NUM[tolower($1)]-2)^2 == 1)
               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
1 Like

Thanks a lot RudiC!

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

I'm getting syntax error, will try to figure it out. Thanks!

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
              }
/start/,/stop/{
               if ((NUM[tolower($1)]-2)^2 == 1)
               printf "start,%d,%s,%s\n", NUM[tolower($1)], $2, $3
              }
   ' file

If you are trying in Solaris/Sun OS use nawk

1 Like