Issue
I have a text file that sometimes-not always- will have an array with a unique name like this
unique_array=(1,2,3,4,5,6)
I would like to find the size of the array-6 in the above example- when it exists and skip it or return -1 if it doesnt exist.
grepping the file will tell me if the array exists but not how to find its size.
The array can fill multiple lines like
unique_array=(1,2,3,
4,5,6,
7,8,9,10)
Some of the elements in the array can be negative as in
unique_array=(1,2,-3,
4,5,6,
7,8,-9,10)
Solution
awk -v RS=\) -F, '/unique_array=\(/ {print /[0-9]/?NF:0}' file.txt
-v RS=\)
- delimit records by)
instead of newlines-F,
- delimit fields by,
instead of whitespace/unique_array=(/
- look for a record containing the unique identifier/[0-9]?NF:0
- if record contains digit, number of fields (ie. commas+1), otherwise 0
Answered By - jhnc Answer Checked By - Cary Denson (WPSolving Admin)