extract the total count line (wc -l) number in shell - shell

I am trying to figure out how to extract the last total count number when I use "wc -l" on multiple files under a directory. For example:
currentDir$ wc -l *.fastq
216272 a.fastq
402748 b.fastq
4789028 c.fastq
13507076 d.fastq
5818620 e.fastq
24733744 total
I would only need to extract 24733744 from the above. I tried
wc -l *.fastq | tail -l
to get
24733744 total
but not sure what to do next. If I use "cut", the annoying thing is that there are multiple spaces before the number, and I will need to use this code for other folders too, and the number of spaces may differ.
Any advice is appreciated. Thank you very much!

For this particular problem, it's probably easier to do :
cat *.fastq | wc -l

This should work with any number of spaces:
wc -l *.fastq | tail -l | tr -s ' ' | cut -f 2 -d ' '
Example:
echo " 24733744 total" | tr -s ' ' | cut -f 2 -d ' '
24733744

Related

How to calculate the total size of all files from ls- l by using cut, eval, head, tr, tail, ls and echo

ls -l | tr -s " " | cut -d " " -f5
I tried above code and got following output.
158416
757249
574994
144436
520739
210444
398630
1219080
256965
684782
393445
157957
273642
178980
339245
How to add these numbers. I'm stuck here. Please no use of awk, perl, shell scripting etc.
It's easiest to use du. Somethings like:
du -h -a -c | tail -n1
Will give you the sum total. You can also use the -d argument to specify how deep the traversing should go like:
du -d 1 -h -a -c | tail -n1
You will have to clarify what you mean by "no use of shell scripting" for anyone to come up with a more meaningful answer.
You can try this way but $((...)) is shell scripting
eval echo $(( $(ls -l | tr -s ' ' | cut -d ' ' -f5 | tr '\n' '+') 0 ))
Don't parse the output of ls. It's not meant for parsing. Use du as with Martin Gergov's answer. Or du and find, or just du.
But if just adding numbers is the sole focus, (even if the input is iffy), here's the laziest possible method (first install num-utils):
ls -l | tr -s " " | cut -d " " -f5 | numsum
And there's other ways, see also: How can I quickly sum all numbers in a file?

sort list of files by date in bash

Given a text file containing some list of files, e.g.
$ cat file_list.txt
/var/x/file1.txt
/var/y/file2.txt
<etc>
How can I sort this list of files by some criteria - like their last accessed time, or last changed time?
Thanks in advance.
You can use stat command with sort like this:
while read -r line; do
stat -c '%Y %n' "$line"
done < file_list.txt | sort -n | cut -d ' ' -f2
stat -c '%Y %n' lists time of last modification, seconds since Epoch followed by a space and file name
sort -n sorts timestamps and their filename numerically
cut -d ' ' -f2 prints only file names from sort's output
Try one liner (by modification time):
ls -t $(cat file_list.txt)
OR
ls -t `cat file_list.txt`
You can get the most recently changed file with
cat file_list.txt | xargs stat -c '%Y %n' | sort | tail -1 | cut -c 12-
You can get the most recent timestamp with
cat file_list.txt | xargs stat -c '%Y %n' | sort | tail -1 | cut -c -10

Count lowest number of lines among list of files

I want to print count for file with lowest number of lines among the list of files. Nothing is printed.
Here is the code
MINcount=$(for txtfile in /home/folder/*.txt;
do
LC=$(cat $txtfile | wc -l);
min=0
(($LC < min || min == 0)) && min=$LC
done)
echo $MINcount
Thanks
give this a try:
wc -l /home/folder/*.txt|sort -n|awk '{print $1;exit}'
You can use the following pipe:
wc -l /home/folder/*.txt | sort -n | head -n1 | cut -f1
Explanation
wc -l /home/folder/*.txt | sort -n will produce output like this:
50 file2
94 file1
144 total
wc prints the total in the first line, and then the lines per file, which get sorted in numerical order (sort -n). head -n1 will then select the first line from output, cut -f1 the first column from that line.

using cut command in bash [duplicate]

This question already has answers here:
Get just the integer from wc in bash
(19 answers)
Closed 8 years ago.
I want to get only the number of lines in a file:
so I do:
$wc -l countlines.py
9 countlines.py
I do not want the filename, so I tried
$wc -l countlines.py | cut -d ' ' -f1
but this just echo empty line.
I just want number 9 to be printed
Use stdin and you won't have issue with wc printing filename
wc -l < countlines.py
You can also use awk to count lines. (reference)
awk 'END { print NR }' countlines.py
where countlines.py is the file you want to count
If your file doesn't ends with a \n (new line) the wc -l gives a wrong result. Try it with the next simulated example:
echo "line1" > testfile #correct line with a \n at the end
echo -n "line2" >> testfile #added another line - but without the \n
the
$ wc -l < testfile
1
returns 1. (The wc counts the number of newlines (\n) in a file.)
Therefore, for counting lines (and not the \n characters) in a file, you should to use
grep -c '' testfile
e.g. find empty character in a file (this is true for every line) and count the occurences -c. For the above testfile it returns the correct 2.
Additionally, if you want count the non-empty lines, you can do it with
grep -c '.' file
Don't trust wc :)
Ps: one of the strangest use of wc is
grep 'pattern' file | wc -l
instead of
grep -c 'pattern' file
cut is being confused by the leading whitespace.
I'd use awk to print the 1st field here:
% wc -l countlines.py | awk '{ print $1 }'
As an alternative, wc won't print the file name if it is being piped input from stdin
$ cat countlines.py | wc -l
9
yet another way :
cnt=$(wc -l < countlines.py )
echo "total is $cnt "
Piping the file name into wc removes it from the output, then translate away the whitespace:
wc -l <countlines.py |tr -d ' '
Use awk like this:
wc -l countlines.py | awk {'print $1'}

I want to insert/store wc -l result into a bash array

I have the following comand:
grep RJAVA | grep -v grep | wc -l ' | sort | cut -d':' -f2 | cut -d' ' -f2
After executing this, I get the following result :
10
0
0
10
I would like to put all these numbers into a bash array so that I can loop through
the array. I tried using xargs but couldn't make it work. Any suggestion ?
this should work:
array=($( YOUR_ENTIRE_PIPED_COMMAND ))
BTW, the command above seems broken - you are missing the input to the first grep (either filnames, or pipe into it)
you could try tr:
IN="10 0 0 10"
arr=$(echo $IN | tr " " "\n")
for x in $arr
do
echo "> [$x]"
done
Regards, Edi

Resources