How to pass AWK output to a variable?

I have a small bash script this paragraph greps / awk using the keyword.

But after adding additional codes: set var = "(......)" it prints only an empty line, not a paragraph.

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

My codes are:

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

Thanks!

+6
linux awk paragraph
source share
5 answers

Use command substitution to display process output.

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

Some general recommendations regarding shell scripts:

  • (almost) always refers to every variable reference.
  • never put spaces around the equal sign in variable assignments.
+5
source share
  • You need to use command substitution. Place the command inside or back, `COMMAND` or, in a pair of parentheses preceded by a dollar sign, $(COMMAND) .
  • To set a variable, you are not using set , and you cannot have spaces before and after = .

Try the following:

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

You gave me the idea of ​​this for killing process :). Just chrome in any process you want to kill.

Try the following:

 VAR=$(ps -ef | grep -i chromium | awk '{print $2}'); kill -9 $VAR 2>/dev/null; unset VAR; 
+2
source share

Anytime you see grep connected to awk, you can remove grep. for above

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

awk can easily replace any text command such as cut, tail, wc, tr, etc. and especially a few grep related to each other. iee

 grep some_co.mand | a | grep b ... to | awk '/a|b|and so on/ {some action}. 
+1
source share

Try creating a variable coming from the / Hashicorp repository when using the variables of the packer template, for example:

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

You can do the same with grep ^user

0
source share

All Articles