Home > Blockchain >  How can i grep or read loop different lines form the same input file and put them in the same output
How can i grep or read loop different lines form the same input file and put them in the same output

Time:10-07

the following file contains different rows, from which i need to retrieve some has header followed by respective own rows. Input file testcip.txt
linia1 11
linia2 11
linia3 11
linia4 11
linia5 11
linia6 11
linia7 11
linia8 11
linia9 11
linia10 11
linia11 11
linia12 11
linia13 11
linia14 11
linia15 11
linia1 22
linia2 22
linia3 22
linia4 22
linia5 22
linia6 22
linia7 22
linia8 22
linia9 22
linia10 22
linia11 22
linia12 22
linia13 22
linia14 22
linia15 22
.
.
.

Desired output exitcip.txt
#as header 1
linia3 11
#followed by rows for linia3 11
linia10 11
linia11 11
linia12 11
linia13 11
#as header 2
linia3 22
#followed by rows for linia3 22
linia10 22
linia11 22
linia12 22
linia13 22
... and so on for
linia 3 nn
and the rows
linia10 nn
linia11 nn
linia12 nn
linia13 nn

thanks in advance!

The problem occurs when the following lists are longer then 15 rows and linea3 in fact won't be the 3rd row, for example
linia0 22
linia0.1 22
linia1 22
linia2 22
linia3 22
linia4 22
linia5 22
linia6 22
linia7 22
linia8 22
linia8.1 22
linia9 22
linia9.1 22
linia9.2 22
linia10 22
linia11 22
linia12 22
linia13 22
linia14 22
linia15 22
linea16 22

in fact, i'm searching for the rows that contains "linia3" and then "linia10", "linia11", "linia12", "linia13". thx again

CodePudding user response:

Try this awk. It prints the 3rd and 10-13th line of each 15-line block.

$ awk '{x  } x == 3 || (x <= 13 && x >= 10){print} x == 15{x = 0}' file
3   1
10  1
11  1
12  1
13  1
3   2
10  2
11  2
12  2
13  2
3   3
10  3
11  3
12  3
13  3
  • Related