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

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?

Related

Echo the command result in a file.txt

I have a script such as :
cat list_id.txt | while read line; do for ACC in $line;
do
echo -n "$ACC\t"
curl -s "link=fasta&retmode=xml" |\
grep TSeq_taxid |\
cut -d '>' -f 2 |\
cut -d '<' -f 1 |\
tr -d "\n"
echo
sleep 0.25
done
done
This script allows me from a list of ID in list_id.txt to get the corresponding names in a database in https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=nuccore&id=${ACC}&rettype=fasta&retmode=xml
So from this script I get something like
CAA42669\t9913
V00181\t7154
AH002406\t538120
And what I would like is directly to print or echo this result in fiel call new_ids.txt, I tried echo >> new_ids.txt but the file is empty.
Thanks for your help.
A minimal refactoring of your script might look like
# Avoid useless use of cat
# Use read -r
# Don't use upper case for private variables
while read -r line; do
for acc in $line; do
echo -n "$acc\t"
# No backslash necessary after | character
curl -s "link=fasta&retmode=xml" |
# Probably use a proper XML parser for this
grep TSeq_taxid |
cut -d '>' -f 2 |
cut -d '<' -f 1 |
tr -d "\n"
echo
sleep 0.25
done
done <list_id.txt >new_ids.txt
This could probably still be simplified significantly, but without knowledge of what your input file looks like exactly, or what curl returns, this is somewhat speculative.
tr -s ' \t\n' '\n' <list_id.txt |
while read -r acc; do
curl -s "link=fasta&retmode=xml" |
awk -v acc="$acc" '/TSeq_taxid/ {
split($0, a, /[<>]/); print acc "\t" a[3] }'
sleep 0.25
done <list_id.txt >new_ids.txt

extract the total count line (wc -l) number in 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

pipe result of cut to next argument and concat to string

I have something like:
cut -d ' ' -f2 | xargs cat *VAR_HERE.ss
where I want to use the result of cut as a variable and concat the output of cut between * and . so that cat will use the name to output the appropriate file. for example if the result of cut is:
$ cut -d ' ' -f2
001
I essentially want the second command to do:
$ cat *001.txt
I have seen xargs been used, but I am struggling to find out how to use it to explicitly call the output of cut, rather than assuming the second command only requires the exact output. obviously here I want to concat the output of cut to a string.
thanks
You can do:
cut -d ' ' -f2 | xargs -I {} bash -c 'cat *{}.txt'

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

bash save values in variable

given: file: files.txt with:
sara.gap sara.gao
pe.gap pe.gao
I just want to use f=sara in my bash skript, because I need f later in the skript. so i tryed: get ffirst line,second argument,remove .gao and save in f
f=sed -ne '1p' files.txt |cut -d " " -f2 |sed 's/.gao//g'
But did not work, please help me ;(
You just need backticks:
f=`head -1 files.txt | cut -d " " -f2 | sed 's/.gao//g'`
I'd do
read f junk < files.txt
f=${f%*.gap}
oh, and for second argument:
read junk f junk < files.txt
f=${f%*.gao}
That's completely in bash :-)
Use Command Substitution if you want to use the output of a command to set a variable. The format is v=$(command). You can also use backticks e.g. v=`command`, but this has been superseded by the $(...) form.
Your command would be:
f=$(sed -ne '1p' files.txt |cut -d " " -f2 |sed 's/.gao//g')
echo $f
prints
sara
you mean this?
f="sed -ne '1p' files.txt |cut -d ' ' -f2 |sed 's/.gao//g'"
eval "$f"
with output:
sara
You can use awk as well
f=$(awk 'NR==1{gsub(/\.gao/,"",$2);print $2;exit}' file)

Resources