I tried this one, but it is displaying the count on number of lines instead.
declare -i x=0 while IFS="" read -r p || [ -n "$p" ] do x=x+1 done <test.txt echo "$x
I would be thankful if someone could explain this since i am a beginner
I tried this one, but it is displaying the count on number of lines instead.
declare -i x=0 while IFS="" read -r p || [ -n "$p" ] do x=x+1 done <test.txt echo "$x
I would be thankful if someone could explain this since i am a beginner
Assuming your words are separated by tabs, spaces ad newlines, the following snippet:
echo $'word1 word2! word3
\tword4\t\t\t\t\t\tword5\tword6
word7 word8
word9 word10' | \
while IFS=$'\t ' read -ra linewords; do
for i in "${linewords[@]}"; do
echo word is "'$i'"
done
done
will output:
word is 'word1'
word is 'word2!'
word is 'word3'
word is 'word4'
word is 'word5'
word is 'word6'
word is 'word7'
word is 'word8'
word is 'word9'
word is 'word10'
It uses multiple IFS values combined with read
reading into an array, see this answer on how to split a string on a delimeter.
I'd use awk for that:
$ echo "Lorem ipsum dolor sit amet,
consectetur adipisci elit,
..." |
awk '{
for(i=1;i<=NF;i++)
print "iterating " $i
}'
Output:
iterating Lorem
iterating ipsum
iterating dolor
iterating sit
iterating amet,
iterating consectetur
iterating adipisci
iterating elit,
iterating ...
grep -oE '\w+' YOUR_FILE.txt
writes the words in YOUR_FILE.txt to standard output. Pipe this into your loop, and you have an iteration over the words.
This assumes that a "word" in your case is one or more characters described by \w
, i.e. either an underscore or what your current locale defines to be an alphanumeric character. If your idea of a "word" is different, you can of course tailor the regular expression according to your needs.