Yesterday, I was trying to find a way to count lines of a set of R files without comments nor empty lines and found this answer on stackoverflow:
|
|
Exactly what I needed! A few explanations are in order:
- cat reads files sequentially;
- "|" is the the pipe operator;
- sed parses and transforms text;
- wc counts words, lines and more!
Basically, counting lines of a file is done with the option -l
of wc
(option -m
for characters, see man wc
to learn more):
|
|
cat
reads the set of file and with pass it to wc
using the pipe:
|
|
To remove blank lines (here comments and empty lines) we use the stream editor as follows:
|
|
There are two instructions separated by a semi colon:
/^\s*#/d
/^\s*$/d
Characters between the two slashes / /
is the selection and d
at the end of the instruction means “delete the selection”. ^\s*#
means “lines starting by (^
) an arbitrary number (*
) of whitespace characters (\s
) followed by a #
(the character for comments)”, so basically lines of comments. The second line is similar but $
indicates the end of the line, so /^\s*$/d
reads “lines that start and end by an arbitrary number of whitespace characters”, that is blank lines!
|
|
Stream editors are very powerful tools, if you are interested in learning more about one of them, have a careful look at the documentation of GNU sed.