Is there a command to output only part of a command result in Linux? - linux

My question is, when we type a command with grep in terminal we get output along with the title:
For example:
lscpu | grep MHz
Will output:
CPU MHz: 1216.851
But what if I only want:
1216.851
As the output? Is there any other command to perform this task?

While there are other ways, the most straightforward would probably be awk:
$ lscpu | grep MHz | awk '{print $3}'
2494.038
Or:
$ lscpu | grep MHz | awk '{print $NF}'
2494.038
$3 represents the third field in the output (separated by any amount of whitespace). $NF represents the LAST field in the output, no matter how many fields there are.
You can also skip grep entirely and just do it all with awk:
$ lscpu | awk '/MHz/ { print $NF; exit }'
2494.038
As #glenn jackman pointed out, GNU grep can also do this:
lscpu | grep --color=never -oP 'MHz:\s+\K.*'
But the other examples above are POSIX-friendly (although systems that have lscpu probably also have GNU grep).

Related

How to extract a single word from the output of a Linux command?

$ lsusb --verbose | grep "THRSL_C_C_V"
results in:
iManufacturer 1 THRSL_C_C_V3.07
I want to extract the word THRSL_C_C_V3.07 only.
I tried lsusb --verbose | grep -w "THRSL_C_C_V". Didn't show anything.
Try:
lsusb --verbose | grep -o "THRSL_C_C_V3\.07"
The -o options results in only matching text, the backlash escapes the dot allowing an exact match of the requested text.
You may use awk instead of grep:
lsusb --verbose | awk '/THRSL_C_C_V/{print $3}'
This awk command searches a line that has text THRSL_C_C_V in it and by using print $3 we make sure to print 3rd column of matched line.
Alternatively you can use grep -o like this:
echo 'iManufacturer 1 THRSL_C_C_V3.07' |
grep -oE 'THRSL_C_C_V[^[:blank:]]*'
THRSL_C_C_V3.07
Use the following:-
$ lsusb --verbose | grep "THRSL_C_C_V" | awk '{print $NF}'
or
$ lsusb --verbose | awk '/THRSL_C_C_V/{print $NF}'
Here, $NF will be used for the last element of output

Count lines of CLI output in linux

Hi have the following command:
lsscsi | grep HITACHI | awk '{print $6}'
I want that the output will be the number of lines of the original output.
For example, if the original output is:
/dev/sda
/dev/sdb
/dev/sdc
The final output will be 3.
Basically the command wc -l can be used to count the lines in a file or pipe. However, since you want to count the number of lines after a filter has been applied I would recommend to use grep for that:
lsscsi | grep -c 'HITACHI'
-c just prints the number of matching lines.
Another thing. In your example you are using grep .. | awk. That's a useless use of grep. It should be
lsscsi | awk '/HITACHI/{print $6}'

cut or awk command to print first field of first row

I am trying print the first field of the first row of an output. Here is the case. I just need to print only SUSE from this output.
# cat /etc/*release
SUSE Linux Enterprise Server 11 (x86_64)
VERSION = 11
PATCHLEVEL = 2
Tried with cat /etc/*release | awk {'print $1}' but that print the first string of every row
SUSE
VERSION
PATCHLEVEL
Specify NR if you want to capture output from selected rows:
awk 'NR==1{print $1}' /etc/*release
An alternative (ugly) way of achieving the same would be:
awk '{print $1; exit}'
An efficient way of getting the first string from a specific line, say line 42, in the output would be:
awk 'NR==42{print $1; exit}'
Specify the Line Number using NR built-in variable.
awk 'NR==1{print $1}' /etc/*release
try this:
head -1 /etc/*release | awk '{print $1}'
df -h | head -4 | tail -1 | awk '{ print $2 }'
Change the numbers to tweak it to your liking.
Or use a while loop but thats probably a bad way to do it.
You could use the head instead of cat:
head -n1 /etc/*release | awk '{print $1}'
sed -n 1p /etc/*release |cut -d " " -f1
if tab delimited:
sed -n 1p /etc/*release |cut -f1
Try
sed 'NUMq;d' /etc/*release | awk {'print $1}'
where NUM is line number
ex. sed '1q;d' /etc/*release | awk {'print $1}'
awk, sed, pipe, that's heavy
set `cat /etc/*release`; echo $1
the most code-golfy way i could think of to print first line only in awk :
awk '_{exit}--_' # skip the quotations and make it just
# awk _{exit}--_
#
# if u're feeling adventurous
first pass through exit block, "_" is undefined,
so it fails and skips over for row 1.
then the decrementing of the same counter will make
it "TRUE" in awk's eyes (anything not empty string
or numeric zero is considered "true" in their agile boolean sense). that same counter also triggers default action of print for row 1.
—- incrementing… decrementing… it's same thing,
merely direction and sign inverted.
then finally, at start of row 2, it hits criteria to
enter the action block, which instructs it to instantly
exit, thus performing essentially the same functionality as
awk '{ print; exit }'
… in a slightly less verbose manner. For a single line print, it's not even worth it to set FS to skip the field splitting part.
using that concept to print just 1st row 1st field :
awk '_{exit} NF=++_'
awk '_++{exit} NF=_'
awk 'NR==1&&NF=1' file
grep -om1 '^[^ ]\+' file
# multiple files
awk 'FNR==1&&NF=1' file1 file2
You can kill the process which is running the container.
With this command you can list the processes related with the docker container:
ps -aux | grep $(docker ps -a | grep container-name | awk '{print $1}')
Now you have the process ids to kill with kill or kill -9.

AWK - Remove last item out of list

I'm trying to get the PID's of a certain service. I'm trying to do that with the following command:
ps aux | grep 'DynamoDBLocal' | awk '{print $2}'
Gives output:
1021
1022
1161
This returns me 3 PID's, 2 of the service that I want, and 1 for the grep it just did. I would like to remove the last PID (the one from the grep) out of the list.
How can I achieve this?
Just use pgrep, it is the correct tool in this case:
pgrep 'DynamoDBLocal'
Using grep -v:
ps aux | grep 'DynamoDBLocal' | grep -v grep | awk '{print $2}'
If you have pgrep in your system
pgrep DynamoDBLocal
You can say:
ps aux | grep '[D]ynamoDBLocal' | awk '{print $2}'
With a single call to awk
ps aux | awk '!/awk/ && /DynamoDBLocal/{print $2}'
Try pidof, it should give you the pid directly.
pidof DynamoDBLocal
Answering the original question: How to remove lines from output:
ps aux | grep 'DynamoDBLocal' | awk '{print $2}' | head --lines=-1
head allows you to view the X (default 10) first lines of whatever comes in. Given a value X with minus prepended it shows all but the last X lines. The 'inverse' is tail, btw (when you are interested in the last X lines).
However, given your specific problem of looking for PIDs, I recommend pgrep (perreals answer).
I'm not sure that ps aux | grep '...' is the right way.
But assuming that it is right way, you could do
ps aux | grep '...' | awk '{ if (prev) print prev; prev=$2 }'

Linux command for physical memory, getting the value only

In Linux cat /proc/meminfo | grep MemTotal gets back MemTotal: 12298824 kB
I want only the numbers here
so i wrote cat /proc/meminfo | grep MemTotal | cut -d':' -f2 which gave me 12298824 kB
I only want the numbers here, can anyone help me?
Note: cat /proc/meminfo | grep MemTotal | cut -d':' -f2 | cut -d'k' -f1 gives me the solution 12298824, but is there a better way? one liner?
Use awk:
cat /proc/meminfo | grep MemTotal | awk '{print $2}'
From #Lars Wirzenius's comment(No cat and No grep):
awk '/MemTotal/ { print $2 }' /proc/meminfo
I have used:
dmidecode -t 17 | grep Size | awk '{s+=$2} END {print s}'
to great effect in my CentOS kickstart %pre section. It returns the total installed amount of memory in MB. Even if you have empty memory banks, awk will ignore them and only add the integer results.

Resources