Find length of fist line of all txt files
I want to loop through all the .TXT files in a directory and get the length of the first line of each one.
find /path -name "*.txt" -exec head -n1 {} \; will print the first line of each file, now how do I print the LENGTH of the first line of each file? like "300" for 300 characters long. |
Use wc
Code:
echo hello | wc -c Code:
-c, --bytes |
ya buts its counting all the output together
Code:
find . -name "*.php" -exec head -n1 {} \; | wc -c |
You can use a while loop to pipe each line found with find
Code:
while read i; Code:
parallel 'head -n1 {} | wc -c' ::: \*.php |
This is good. Now I just need a condition if the length from wc is equal to say 400 to do something. How do I tell this to do a sed command if the length of the first line is 400 characters?
PHP Code:
|
This can easily be done with a if conditional expression.
Feel free to use man bash and search for CONDITIONAL EXPRESSIONS Here's a search to get you started eg this guide |
yes I can do a conditional expression. but how to I capture the number of wc into a variable to check?
|
You can use command substitution for that.
Code:
variable=$(echo hello | wc -c) |
Couting characters, words, lenght of the words and total lenght in a sentence
and https://www.google.com/#q=count+word...in+bash+script Read. Study. Try. Question. |
Code:
find /path -name "*.txt" | |
All times are GMT -5. The time now is 07:47 PM. |