I'm trying to get the size of a specified file only. Normally I have zero issues doing this, but no matter what I try here the filename does not go away.
[root#dockertest Shipper]# du -s c_parser.py | cut -d ' ' -f 2
8 c_parser.py
Nothing changes based on what I put after the pipe. Changing '2' to '1' doesn't do anything. Using:
[root#dockertest Shipper]# du -s c_parser.py | awk -F="c_parser.py" '{ print $1 }'
Does nothing at all either. Any thoughts?
The output of du is tab-separated, you need to use tab delimiter. Though tab is the default delimiter in cut, you could also use it explicitly
du -s file | cut -d $'\t' -f2
or just
du -s file | cut -f2
In such cases, do a hexdump of the output will help you understand easily
du -s file | hexdump -c
0000000 8 \t f i l e \n
0000007
Using awk too on tab-delimiter
du -s file | awk 'BEGIN{FS=OFS="\t"}{print $2}'
Related
I want to extract the services from the file /etc/services. The problem is that when extracting them, I get the following output when entering head file.txt:
acr-nema
afbackup
afbackup
afmbackup
afmbackup
afpovertcp
afpovertcp
afs3-bos 7007
But the desired output should be as follows:
acr-nema 104/udp dicom
afbackup 2988/tcp #
afbackup 2988/udp
afmbackup 2989/tcp #
afmbackup 2989/udp
afpovertcp 548/tcp #
afpovertcp 548/udp
afs3-bos 7007/tcp #
The command that I am entering is the following:
cat /etc/services | sed '/^#/ d' | cut -d ' ' -f 1 | sort | awk '!a[$0]++' > file.txt
give this a try:
awk '$0&&/^[^#]/&&!a[$0]++' /etc/services |sort
btw, don't do cat aFile|awk '...' instead, do awk '...' file
cut -f1 /etc/services | grep '^[^#].*s$' | sort --unique
can be used to get unique services but if you want to store in a different file you can add > file.txt
i have a shell script
#/bin/bash
var1=`cat log.json | grep "accountnumber" | awk -F ' ' '{print $1}'`
echo $var
output of shell script is :-
23466
283483
324932
87374
I want match the above number which is already store in another file (below is the file format ) and print its value .
23466=account-1
283483=account-2
324932=account-3
87374=account-4
127632=account-5
1324237=account-6
73642=account-7
324993284=account-8
.
.
4543454=account-200
exapected output
account-1
account-2
account-3
account-4
a Compact one line solution can be:
join -t "=" <(sort bf) <(sort fa) | cut -d '=' -f 2
here fa is a file containing out-put of your bash script and bf is the file that has 23466=account-1 format
the output is:
account-1
account-2
account-3
account-4
#!/bin/bash
for var1 in $(awk -F ' ' '/accountnumber/{print $1}' log.json)
do
awk -F= '$1=="'"$var1"'"{print $2}' anotherfile
done
For a moment there was another answer that almost worked that I think is much slicker than what I wrote. Probably faster / more efficient on large files too. Here it is fixed.
awk -F ' ' '/accountnumber/{print $1}' log.json \
| sort -n \
| join -t= - accountfile \
| cut -d= -f2
I am trying to find the size of a folder, and add 1M to it. The 1M is just spacing that I need for other reasons.
Here is what I have tried:
echo $($(du -sh myFolder) + 1) # command not found: 170M
echo $(`du -sh myFolder` + 1) # same as above
I want to be able to save this to a variable so I can use it in a dd call.
echo $(($(du -sb myFolder | cut -f1)+1048576))
du -sb gives a single sumarized result in bytes.
Alternatives if you can't use -b as Joao Morais suggested:
expr `du -hs myFolder | awk '{print $1}' | tr -d M` + 1
echo $((`du -hs myFolder | awk '{print $1}' | tr -d M` + 1))
Yet another way:
BLOCKSIZE=1048576 du -s myFolder | awk '{print $1+1}'
or to put in a variable:
mbs=$(BLOCKSIZE=1048576 du -s myFolder | awk '{print $1+1}')
Works on Linux, BSD, and OS X (possibly others).
I have a file which has contents like the below
SPEC.2.ATTRID=REVISION&
SPEC.2.VALUE=5&
SPEC.3.ATTRID=NUM&
SPEC.3.VALUE=VS&
I am using the below command to extract only the numbers from the first line. Is this way efficient or you guys think of an alternate way ?
cat ticketspecdata | tr -d " " | tr -s "[:alpha:]" "~" | tr -d "[=.=]" | cut -d "~" -f2
Using grep :
$ grep -om1 '[0-9]\+' file
2
Or
head -n1 file | tr -cd '[:digit:]'
You may also want to read about UUOC:
http://porkmail.org/era/unix/award.html
I have a jar file, i need to execute the files in it in Linux.
So I need to get the result of the unzip -l command line by line.
I have managed to extract the files names with this command :
unzip -l package.jar | awk '{print $NF}' | grep com/tests/[A-Za-Z] | cut -d "/" -f3 ;
But i can't figure out how to obtain the file names one after another to execute them.
How can i do it please ?
Thanks a lot.
If all you need the first row in a column, add a pipe and get the first line using head -1
So your one liner will look like :
unzip -l package.jar | awk '{print $NF}' | grep com/tests/[A-Za-Z] | cut -d "/" -f3 |head -1;
That will give you first line
now, club head and tail to get second line.
unzip -l package.jar | awk '{print $NF}' | grep com/tests/[A-Za-Z] | cut -d "/" -f3 |head -2 | tail -1;
to get second line.
But from scripting piont of view this is not a good approach. What you need is a loop as below:
for class in `unzip -l el-api.jar | awk '{print $NF}' | grep javax/el/[A-Za-Z] | cut -d "/" -f3`; do echo $class; done;
you can replace echo $class with whatever command you wish - and use $class to get the current class name.
HTH
Here is my attempt, which also take into account Daddou's request to remove the .class extension:
unzip -l package.jar | \
awk -F'/' '/com\/tests\/[A-Za-z]/ {sub(/\.class/, "", $NF); print $NF}' | \
while read baseName
do
echo " $baseName"
done
Notes:
The awk command also handles the tasks of grep and cut
The awk command also handles the removal of the .class extension
The result of the awk command is piped into the while read... command
baseName represents the name of the class file, with the .class extension removed
Now, you can do something with that $baseName