8

I have a small bash script that greps/awk paragraph by using a keyword.

But after adding in the extra codes : set var = "(......)" it only prints a blank line and not the paragraph.

So I would like to ask if anyone knows how to properly pass the awk output into a variable for outputting?

My codes:

#!/bin/sh

set var = "(awk 'BEGIN{RS=ORS="\n\n";FS=OFS="\n"}/FileHeader/' /root/Desktop
/logs/Default.log)"
echo $var;

Thanks!

JavaNoob
  • 3,494
  • 19
  • 49
  • 61
  • 3
    Make it a habit to always put double quotes around variables: `echo "$var"`. This typically bites users with filenames containing spaces. – glenn jackman Sep 24 '10 at 17:10

5 Answers5

9

Use command substitution to capture the output of a process.

#!/bin/sh

VAR="$(awk 'BEGIN{RS=ORS="\n\n";FS=OFS="\n"}/FileHeader/' /root/Desktop/logs/Default.log)"
echo "$VAR"

some general advice with regards to shell scripting:

  • (almost) always quote every variable reference.
  • never put spaces around the equals sign in variable assignment.
Lesmana
  • 25,663
  • 9
  • 82
  • 87
  • 1
    @JavaNoob, you have to be carefull with backslashes inside the backticks. see here http://mywiki.wooledge.org/BashFAQ/082 – Lesmana Sep 24 '10 at 18:24
3
  • You need to use "command substitution". Place the command inside either backticks, `COMMAND` or, in a pair of parentheses preceded by a dollar sign, $(COMMAND).
  • To set a variable you don't use set and you can't have spaces before and after the =.

Try this:

var=$(awk 'BEGIN{RS=ORS="\n\n";FS=OFS="\n"}/FileHeader/' /root/Desktop/logs/Default.log)
echo $var
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
dogbane
  • 266,786
  • 75
  • 396
  • 414
2

You gave me the idea of this for killing a process :). Just chromium to whatever process you wanna kill.

Try this:

VAR=$(ps -ef | grep -i chromium | awk '{print $2}'); kill -9 $VAR 2>/dev/null; unset VAR;
cokedude
  • 379
  • 1
  • 11
  • 21
  • You generally want to use the `ps -C` argument, as in `ps -C chromium`. Using grep with ps output like this will eventually come back to bite you when there's a filename open in vim or something which matches the command you were grepping for. And you can specify that just the pid be in the output, saving awk by using `ps -C chromium -o pid=`. – dannysauer Feb 11 '16 at 02:52
  • Or just use `killall chromium`, I guess ;) – dannysauer Feb 11 '16 at 02:53
1

anytime you see grep piped to awk, you can drop the grep. for the above,

awk '/^password/ {print $2}'

awk can easily replace any text command like cut, tail, wc, tr etc. and especally multiple greps piped next to each other. i.e

grep some_co.mand | a | grep b ... to | awk '/a|b|and so on/ {some action}.
wogsland
  • 9,106
  • 19
  • 57
  • 93
jim
  • 11
  • 1
0

Try to create a variable coming from vault/Hashicorp, when using packer template variables, like so:

BUILD_PASSWORD=$(vault read secret/buildAccount| grep ^password | awk '{print $2}')
echo $BUILD_PASSWORD

You can to the same with grep ^user

Stephan Kristyn
  • 15,015
  • 14
  • 88
  • 147
Kenneth King
  • 126
  • 1
  • 2