shell - Searching symbol patterns -


i trying use awk script:

awk -v count=0 '/`),`/ { count++ } end { print count }' sample.sql 

but following error:

error: awk: line 1: runaway regular expression /\),/ {.. 

why getting error , can fix it?

the target count line backticks , closing parenthese like:

insert test (`id`, `address`) values (1,'delhi'),(2,'mumbai') 

thanx @tensibai. added \ line ),. , works. .sql contain insert test (id,nm) values (1,'delhi'),(2,'mumbai'),(3,'chennai')...; so, did search ), starting count=1. have idea search better count.?

if correctly understood try achieve (counting lines wich contain ), ) should do:

awk '/\(`.*`\)`/ { count++ } end { print count }' sample.sql 

no need initialize variable, match line containing

(` text here `) 

and increment counter printed @ end.


as i've small doubt on original question here how count number of value in each line:

awk '/\(.*\),/ { n=split($0,a,"\),"); print n; ncount += n; count++;} end { print "lines matched:",count,"total values:",ncount }' 

here each line more 1 value ( match (text inside), ) split on ), , number of values variable n, print n, sum number of values , increment number of lines matched.

at end print lines matched , total values found.

warning not count lines 1 value given. (no , after last ))


to avoid warning above , focus on insert statement only:

 awk '/insert.*\(.*\),*/ { n=split($0,a,"\)(,|\n)"); print n; count++;} end { print count }' /srv/db-backup-20-10-2014.sql 

adding insert starting word , option on split split on closing parenthese followed comma or newline.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -