shell - Count lines following a pattern from file -
for example have file test.json
contains series of line containing:
header{...}
body{...}
- other text (as others)
- empty lines
i wanted run script returns following
counted started on : test.json - headers : 4 - body : 5 - <others> counted finished : <time elapsed>
what got far this.
count_file() { echo "counted started on : $1" #todo loop cat $1 | grep header | wc -l cat $1 | grep body | wc -l #others echo "counted finished : " #todo timeelapsed }
edit:
edit question , added code snippet
perl on command line
perl -e '$match=$argv[1];open(input, "<", $argv[0]);while(<input>){ ++$n if /$match/g } $match," ",$n;' your-file your-pattern
for me
perl -e '$match=$argv[1];open(input, "<", $argv[0]);while(<input>){ ++$n if /$match/g } $match," ",$n;' parsing_command_line.pl my
it counts how many number of pattern my are, in script parsing_command_line.pl
output
my 3
for you
perl -e '$match=$argv[1];open(input, "<", $argv[0]);while(<input>){ ++$n if /$match/g } $match," ",$n;' test.json headers
note
write code in one line on command prompt
first argument your file
second pattern
not complete code since have enter pattern one-by-one
Comments
Post a Comment