How to Get a Part of the Output of a Command in Linux Bash

How do I get a part of the output of a command in Linux Bash?

You could try the below AWK command,

$ php -v | awk 'NR==1{print $1,$2,$3}'
PHP 5.3.28 (cli)

It prints the first three columns from the first line of input.

  • NR==1 (condition)ie, execute the statements within {} only if the value of NR variable is 1.
  • {print $1,$2,$3} Print col1,col2,col3. , in the print statement means OFS (output field separator).

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

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).

How to get specific part of output with bash script

You can simply use awk to quickly parse your userswithjson file and output the userid portion of the file path. Here's some code that will achieve that for you:

awk -F/ '{print $3}' userswithjson 

The -F/ sets the IFS (input field separator) for awk. Awk breaks each input line into fields based on the IFS. The fields are given numeric names (like $3).

How can i get this certain part of the command output using shell?


find . -name "Safari" -ls | cut -d/ -f 2

Use cut to extract the desired part from the output.

  • d/: set '/' as the delimeter
  • -f 2: print only the second field

in bash shell: extract part of the output of a command and output to file

For a line that matches "mounted by", print column 3:

mount | awk '/mounted by / { print $3 }'

Checking if output of a command contains a certain string in a shell script

Test the return value of grep:

./somecommand | grep 'string' &> /dev/null
if [ $? == 0 ]; then
echo "matched"
fi

which is done idiomatically like so:

if ./somecommand | grep -q 'string'; then
echo "matched"
fi

and also:

./somecommand | grep -q 'string' && echo 'matched'

How do I set a variable to the output of a command in Bash?

In addition to backticks `command`, command substitution can be done with $(command) or "$(command)", which I find easier to read, and allows for nesting.

OUTPUT=$(ls -1)
echo "${OUTPUT}"

MULTILINE=$(ls \
-1)
echo "${MULTILINE}"

Quoting (") does matter to preserve multi-line variable values; it is optional on the right-hand side of an assignment, as word splitting is not performed, so OUTPUT=$(ls -1) would work fine.

Bash: Grab part of string from a command line output

Using awk:

awk -F"|" '{print $NF}'

this will work:

echo " Web App | domain.com | 2012-02-23 | 2012-08-31 | | anotherdomain.com | 
e-8sgkf3eqbj | Web-App-Name | Status | Linux | Ready | N/A |
20120831 - daily" | awk -F"|" '{print $NF}'

and yield:

20120831 - daily

To assign to a variable (data.txt contains your string just for simplicity, it also works the echo above):

$ myvar=$(awk -F"|" '{print $NF}' data.txt)
$ echo $myvar
20120831 - daily

Explanation

the -F sets the input field separator, in this case to |. NF is a built-in awk variable that denotes the number of input fields, and the $ in front of the variable accesses that element, i.e., in this case the last field in the line ($NF).

Alternatively: You could grab each of the last three fields separated by white space (the awk default) with this:

awk '{print $(NF-2), $(NF-1), $NF}'

save an output of a command in a variable in Bash scripting

You could pipe the output of ads2 version to grep and then to cut to extract the field corresponding to the version number:

$ version="$(ads2 version | grep 'ADS2 version' | cut -d' ' -f 3)"

or better using awk instead:

$ version="$(ads2 version | awk '/ADS2 version/{print $3}')"

The version will be then stored in the version shell variable:

$ echo $version
4.8.10

As for the follow-up question of your edit, you could do something like:

if [ "$(ads2 version | grep DDA | grep '(unknown)')" ]; then
echo "Not activated"
else
echo "Activated"
fi

That is, you limit yourself to the line containing the text DDA and then check whether you have the text (unknown) on that line. If so, it's not activated; otherwise, it is.



Related Topics



Leave a reply



Submit