0

I am trying to print a variable using awk. Below is the code that I have wriiten, but $svr seems to be not getting the value of the declared variable.

input.txt:

string abcd
char   &%

Code:

svr="abc.net,def.net"

for n in $svr; do
awk '/string/ {if ($1 == "string") print $1, "$svr OK";}' input.txt
done

output should be like this:

abc.net OK
def.net OK
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
user3834663
  • 513
  • 2
  • 7
  • 17
  • 3
    Can you try and explain more clearly what you're trying to do? At the moment it's not clear to me at all. – Tom Fenech Dec 29 '14 at 17:02
  • Here I would need to print the value declared as variables using awk. to be more concise, I would need print the $svr values in awk statement..should be abc.net OK def.net OK – user3834663 Dec 29 '14 at 17:05
  • In your opinion, what is the `/string/` before `if ($1 == "string")` doing for you? To put it another way - if $1 DOES equal "string", in what scenario do you think the `/string/` condition would be false? – Ed Morton Dec 29 '14 at 17:06
  • I have the input.txt (see above)./string/ will search for the line of word "string" and prints the $1 – user3834663 Dec 29 '14 at 17:08
  • No, it won't do that. Think about it for a minute. `/string/` is doing nothing useful. – Ed Morton Dec 29 '14 at 17:10
  • This is just a example code I have written to make understand the scenario. my requirement is if the input.txt does not have a word eg "string" then I should print some thing like "$svr does not have string in the txt file" – user3834663 Dec 29 '14 at 17:12
  • It does not help us to understand your requirements so we can help you when you post example code that doesn't make sense and sample output that doesn't reflect the output you really want. Please edit your question to describe what you're really trying to do with some truly representative sample input and expected output. – Ed Morton Dec 29 '14 at 17:14
  • Here I get the output like "$svr does not have the string in the txt file" where $svr does not replaced with the declared values.ie abc.net or def.net..hope you understand the issue now – user3834663 Dec 29 '14 at 17:14
  • 2
    No, it is completely unclear. Please edit your question to clarify and provide truly representative sample input and expected output. Nothing in your question so far, for example, shows you needing to produce any output that says `"$svr does not have the string in the txt file"`. – Ed Morton Dec 29 '14 at 17:15
  • I did update my post. Is this on the right track? – Jotne Dec 29 '14 at 18:03

1 Answers1

0

You are reading variable wrong with awk, see here: How to use shell variables in an awk script

#!/bin/bash
svr=(abc.net def.net)

for n in ${svr[*]}; do
   awk -vSVR="$n" '$1=="string" {f=1} END {if (f) print $1, SVR,"OK";}' input.txt
done

Using array is a better way to store server name.

If you like to test the file and only get on output is ok, its better to print result in an END block.

PS this still does not work correctly, since its not clear what you like to test where.


This is an attempt to see if I am on correct track.

You have a set of files. You like to test to see if the files have a specific settings.

cat abc.net
string home
More data

cat def.net
string work
more data

cat test
#!/bin/bash
svr=(abc.net def.net)
for n in ${svr[*]}; do
   awk -vI=$(cat input) '/string/ {if ($2~I) f=1} END {if (f) print FILENAME,"OK"}' "$n"
done

With input file set to test for work

cat input
work

./test
def.net OK

Then setting input to home

cat input
home

./test
abc.net OK

If there is only one hit in every file, you could shorten it some and remove the loop

cat test
#!/bin/bash
svr=(abc.net def.net)
awk -vI=$(cat input) '/string/ {if ($2~I) print FILENAME,"OK"}' ${svr[*]}
Community
  • 1
  • 1
Jotne
  • 40,548
  • 12
  • 51
  • 55
  • this cannot be the desired solution as it wouldn't make sense to do this instead of just passing the svr list to awk and parsing input.txt once – Ed Morton Dec 29 '14 at 17:42
  • @EdMorton I know, just showing how to get the variable from `svr` in to `awk`. OP needs to be some more clear on what he like to test. – Jotne Dec 29 '14 at 17:44
  • I suspect the OP may not have understood that's all you're trying to do here though and think that this is actually the solution you're proposing to his overall problem, whatever that is. – Ed Morton Dec 29 '14 at 17:48