Title says it all. I've managed to get just the lines with this:
lines=$(wc file.txt | awk {'print $1'});
But I could use an assist appending this to the filename. Bonus points for showing me how to loop this over all the .txt files in the current directory.
where
bash
command is executed for each (\;
) matched file;{}
is replaced by the currently processed filename and passed as the first argument ($0
) to the script;${0%.txt}
deletes shortest match of.txt
from back of the string (see the official Bash-scripting guide);wc -l < "$0"
prints only the number of lines in the file (see answers to this question, for example)Sample output:
You could use
grep -c '^'
to get the number of lines, instead ofwc
andawk
:You could use the
rename
command, which is actually a Perl script, as follows:Sample Output
If you like what it says, remove the
--dry-run
and run again.The script counts the lines in the file without using any external processes and then renames them as you ask, also without using any external processes, so it quite efficient.
Or, if you are happy to invoke an external process to count the lines, and avoid the Perl method above: