0

I am reading a file with a matching pattern then getting the first five lines after the matching pattern. The patterns are not in proper numeric order, so I am using a numeric variable in the pattern to loop through each set of records in numeric order. It prints all nine record to the terminal in numeric order, but if I redirect to a file it prints nothing to the terminal and only the first record to the file. If I insert a numeric value for just one matching pattern instead of incrementing variable it print to the terminal and to the file for just the one matching pattern 1 - 9. Here is the line causing the trouble.

awk -v data="$devnum" '$0~data{print $0
 for (i=1; i<=5; i++) {getline; print}}' $data > $devicedata

If I rm the > $devicedata it prints to terminal if I replace "$devnum" with a matching pattern /device 3/ it prints the 3rd record with the next 4 lines to the terminal and outputs to a file. I apologize for any improper format if any. Thank you for any help! For an update I believe I should be looking for the problem as an "ambiguous redirect". I did try piping to the tee command and appending with >>, but I went back to redirecting with 2>&1 which did away with the "ambiguous redirect" which I had forgotten about. $data=devices.txt and $devicedata=devicedata.txt.

4
  • You can try to use the tee command. If you pipe to tee -a it will print in both places, terminal and file. $data | tee -a $devicesdata
    – Cyberninja
    Commented Oct 6, 2023 at 3:01
  • In the part of the command that is $data > $devicedata, do $data and $devicedata both contain the same filename?
    – Sotto Voce
    Commented Oct 6, 2023 at 5:02
  • Is your command part of a loop? You know that each time you redirect with > into a file, the file is truncated (emptied)? If you want to append to a file, you'd use >>. This is not a real answer as the context of the command in the question is unclear.
    – Kusalananda
    Commented Oct 6, 2023 at 5:18
  • Thank you – Kusalananda for getting me back on track and a possible answer looking into the error "ambiguous redirect". Learning never stops. I do feel one step closer and there are several replies about this error on this site that I will read & check into next. I did update the original question with additional info.
    – Joey Simon
    Commented Oct 6, 2023 at 10:58

1 Answer 1

1

Whatever problem you're having with redirection it has nothing to do with -v. Run your script through http://shellcheck.net and fix the issues it tells you about.

Your current symptoms are almost certainly because the value stored in devicedata contains a space and so unquoted > $devicedata expands to multiple redirection targets and so quoting > "$devicedata" will resolve the ambiguous redirect but that may not be your only problem.

Here's an example of how you get an ambiguous redirect error from bash:

$ devicedata='foo'
$ echo 'hi' > $devicedata

$ devicedata='foo bar'
$ echo 'hi' > $devicedata
-bash: $devicedata: ambiguous redirect

and here's how you get rid of that error message:

$ echo 'hi' > "$devicedata"
$

Now the remaining thing you need to figure out is why your devicedata variable contains white space and if that's what you want/expect or not.

Quotes in shell aren't something you add when you need to, they're something you use by default and remove when you need to. You should ALWAYS double quote variables in shell unless you have a very specific reason not to and fully understand what it means to remove those quotes, see https://mywiki.wooledge.org/Quotes.

If you DO still have a problem after running shellcheck, etc. then make sure to post a minimal, complete shell script that we can help you debug as the problem has nothing to do with your awk script. Having said that - what you're trying to do isn't a situation you should use getline for, see https://stackoverflow.com/questions/17908555/printing-with-sed-or-awk-a-line-following-a-matching-pattern/17914105#17914105, and if it was that isn't how you should call it, see awk.freeshell.org/AllAboutGetline.

FWIW I'd write your script as (untested):

awk -v devnum="$devnum" '$0~devnum{c=5} c&&c--' "$data" > "$devicedata"

It's confusing to use data as the name of an awk variable for one purpose and then also use data as the name of a shell variable for a totally different purpose so I renamed one of them.

2
  • Thank you @Ed for such an elegant awk statement and the information about the double quotes, etc. Although, the output > "$devicedata" doesn't work yet and I don't quite understand about the white space in the "$devicedata" variable. It is just a path to ~/devicedata.txt. But, the variable "$devnum" does have white spaces which is the combination of two fields joined together for a search: ($7 = "Dev#=") && ($8 = " $c") to equate to '/Dev#= c/' . I know this isn't very elegant as that will change with more knowledge of awk.
    – Joey Simon
    Commented Oct 9, 2023 at 11:40
  • @JoeySimon your problem of ambiguous redirect has nothing to do with devnum or anything else - it's devicedata expanding to multiple values. I've no idea how you're populating it as you don't show that in your question so if you need more help you'd have to provide a minimal, complete shell script for us to be able to help you with that.
    – Ed Morton
    Commented Oct 9, 2023 at 18:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .