2

Can you please help me solve this puzzle? I am trying to print the location of a string (i.e., line #) in a file, first to the std output, and then capture that value in a variable to be used later. The string is “my string”, the file name is “myFile” which is defined as follows:

this is first line
this is second line
this is my string on the third line
this is fourth line
the end

Now, when I use this command directly at the command prompt:

% awk ‘s=index($0, “my string”) { print “line=” NR, “position= ” s}’ myFile

I get exactly the result I want:

% line= 3, position= 9

My question is: if I define a variable VAR=”my string”, why can’t I get the same result when I do this:

% awk ‘s=index($0, $VAR) { print “line=” NR, “position= ” s}’ myFile

It just won’t work!! I even tried putting the $VAR in quotation marks, to no avail? I tried using VAR (without the $ sign), no luck. I tried everything I could possibly think of ... Am I missing something?

0

1 Answer 1

3

awk variables are not the same as shell variables. You need to define them with the -v flag

For example:

$ awk -v var="..." '$0~var{print NR}' file

will print the line number(s) of pattern matches. Or for your case with the index

$ awk -v var="$Var" 'p=index($0,var){print NR,p}' file

using all uppercase may not be good convention since you may accidentally overwrite other variables.

to capture the output into a shell variable

$ info=$(awk ...)

for multi line output assignment to shell array, you can do

$ values=( $(awk ...) ); echo ${values[0]}

however, if the output contains more than one field, it will be assigned it's own array index. You can change it with setting the IFS variable, such as

$ IFS=$(echo -en "\n\b"); values=( $(awk ...) )

which will capture the complete lines as the array values.

Sign up to request clarification or add additional context in comments.

4 Comments

Brilliant! It worked ... Thanks so much, karakfa! :-) How can I 'capture' the result in a variable?
worked, thanks! now, last question, i promise. suppose i have i have multiple strings i'm dealing with, in my example, let's say i have the awk command in a loop to find all occurrences of the the string "this is". the values returned for the positions, how can i store them in an array or a list? i.e., i want to keep them? -thx.
you don't need a loop for all instances of the same string, awk will return all matches. see the update for array assignment with a caveat.
@lookiluke If you think it answers your question and will be useful for others please accept it as the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.