-1

I am running a bash script that I input a file, it reads the file and runs through the file in a loop. my problem is my awk command. I am not sure why I am getting the error I get

I input a file running the script as such

./almost.sh < text.txt

The script is

#!/bin/bash

while read STRING
do
    echo $STRING
    "awk -v RS='' -v ORS='\n\n'  $STRING file.txt" >> out.txt 

     #grep $STRING dbutils.list.Volume.txt >> out.txt
done

The output I get is

00061
./almost.sh: line 6: awk -v RS='' -v ORS='\n\n'  00061 list.txt: command not found
00062
./almost.sh: line 6: awk -v RS='' -v ORS='\n\n'  00062 list.txt: command not found
00091
./almost.sh: line 6: awk -v RS='' -v ORS='\n\n'  00091 list.txt: command not found
000C1
./almost.sh: line 6: awk -v RS='' -v ORS='\n\n'  000C1 list.txt: command not found
000C2
./almost.sh: line 6: awk -v RS='' -v ORS='\n\n'  000C2 list.txt: command not found

Could someone help out with the awk command? the command works fine if it is not in a loop but just ran once.

3

2 Answers 2

4

The error is given because there is no command

"awk -v RS='' -v ORS='\n\n'  $STRING file.txt"

awk and its parameters (which make no sense) are between double quotes, for the shell the combination is the command, the shell is looking for a file called "awk -v RS='' -v ORS='\n\n' $STRING file.txt".

Perhaps you mean

awk -v RS='' -v ORS='\n\n'  "/$STRING/" file.txt >> out.txt
1

As there hasn’t been a further response, I’m going to suggest the entire script can be simply replaced with

grep -F -f text.txt file.txt >out.txt

This is presuming the input from text.txt is simply strings to be checked for rather than regex, suggested by the error messages replacing $STRING with 000.. due to this, -f reads each string from a file, while -F just treats them as plain strings. Any that then match against file.txt will be sent to out.txt.

In response to dave_thompson_085's comment below, this may still be useful if there are paragraphs with a known structure, i.e. the string looked for is on a particular row in each paragraph, then the -B and -A options can be used to also print a number of rows before and after the match as well.

1
  • 1
    Except grep does a line at a time, not a 'paragraph' (chunks separated by empty line) as awk does for empty RS. If you do want literal-string match by paragraph use something like awk -vRS='' -vORS='\n\n' 'index($0,needle)!=0' and if you want multiple of them you can loop either within awk or around it. Commented Jan 20, 2018 at 8:46

You must log in to answer this question.

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