Hello Everyone,
i need to read specific number of lines ( always serialized ; i.e from 10 to 20 or from 34 to 44 ) in a file , where the first line is found by grep 'ing a keyword.
example
file.txt
------------------------------------------------------------------
--header
this is the message header 3x3
--body
RowId 12
TypeID 0xA
InfoA None
InfoB None
InfoC None
......
.....
----------------------------------------------------------------
so i want to read the first 10 lines begining from the line where 0xA is found.
how can this be achieved?
Appreciate Any help
Alain
Try this...
nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=2 a=4 s="string" file1
...where "b" and "a" are the number of lines to print before and after string "s".
In your case......
nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=0 a=9 s="0xA" file.txt
maybe this?:
awk 'BEGIN{printline=0;cont=1} {if($2=="0xA")printline==1;if(printline==1 && cont<=10){print $0;cont++}}' IN_FILE
Many thanks Gentlmen.
will try and get back to you if needed.
Hi Again,
this is working fine
nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=0 a=9 s="0xA" file.txt
can i make every 10 lines be on one line in the output separated by a delimiter perhaps ?
Alain
You may use 'paste' if you require something like (Delimiter "|")
TypeID 0xA|InfoA None|InfoB None.........................
nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=0 a=9 s="0xA" file.txt | paste -s -d"|" -
hi ,
paste is putting all output in one line.
i need every 10 lines on a single line the other 10 lines shall be on a new line and so on.
Alain