How to store a line number in variable (shell script,unix)? -
file.txt below :
gui : 789 gui : 789 gui : 789 gui : 789 abc : 120 the followibng gives o/p
$ grep -n "gui : 789" file.txt | cut -f1 -d: 1 2 3 4 if there n number of such gui : 789 , how store line numbers of same ?
you can use awk 1 liner:
awk '/gui : 789/{print nr}' file to process inside loop:
while read l echo $l done < <(awk '/gui : 789/{print nr}' file) edit: these command work number of matches in file. store output of above line numbers in array:
arr=( $(awk '/gui : 789/{print nr}' x) ) later on process these array elements as:
echo ${arr[0]} echo ${arr[1]} ... echo ${arr[5]}
Comments
Post a Comment