linux - Finding the Number of strings in a File -
i'm trying write small program check number of sub strings in large text file. count first 2000 lines of text file, find "ttt" sub-strings, count them, , set variable total. i'm bit new shell, amazingly appreciated!
#!/bin/bash $counter=(head -2000 [file name] | grep ttt | grep -o ttt | wc -l) echo $counter
for it's worth might awk
better suited task:
awk -f"ttt" '{j=(nf-1)+j}end{print j}' filename
this split each record in file delimiter "ttt". counts number of fields, subtracts one, , adds total.
a file like:
ttt tttttt 1 5 ttt tt 1 more ttt record
would split (visualizing pipe delim) like:
| || 1 5 | tt 1 more | record
counting number of fields per record:
4 2 1 2
subtracting 1 that:
3 1 0 1
which totals 5, how many "ttt" substrings present.
to incorporate script (and fixing other issue):
#!/bin/bash counter=$(awk -f"ttt" '{j=(nf-1)+j}end{print j}' filename) echo $counter
the change here when set variable in bash don't include $ sign @ front. in referencing variable include $
.
Comments
Post a Comment