Hi there
does anybody know how i can get the line number from an entry or entries in a file ?? for example if i had a file
test1
test2
test3
test1
and i needed to get the line numbers for all instances of test1 in that file
with the answer being (1,4)
Would anybody be able to point me in the right direction with this ..?
Any help would be greatly appreciated
$ cat samplefile
test1
test2
test3
test1
$ grep -n test1 samplefile
1:test1
4:test1
An approach with awk:
awk '/test1/{s=s?s","NR:NR}END{print s}' file
Regards
ripat
4
Why not simply:
awk '/test1/ {print NR}' file
Or did I miss something in the OP?
Edit: Oh, I see. It's the desired output format (n,n)! Disregard my remark.
i have a file amit.txt
#cat amit.txt
amit
ars
amit
arg
i want to the patten amit to be serched.
The code is
=============
grep -n amit amit.txt >ars
sed 's/\(.\).*/\1/' ars
awk '{
if (_[$0]=="")
_[$0]=NR
else
_[$0]=sprintf("%s,%s",_[$0],NR)
}
END{
for (i in _)
print i" ("_,")"
}' a
This will meet your requirements but has too many pipes:D
$ grep -n test1 samplefile | awk -F":" '{print $1}' | tr "\n" ",\n" | sed 's/,$/\n/'
1,4