Reputation: 157
Is it possible to grep strings or compare fields with awk in an assigned $variable.
For example
grep "word" "$foo"
only lists the complete content of $foo.
The awk command does not recognize variables but searches for a file in my folder:
awk 'FNR==NR{a[$1]++;next}a[$1]' "$foo" "$fee"
It says awk: fatal: cannot open file `$foo' for reading (No such file or directory)
@BMW suggested to provide more details. Here they are: This is the complete command:
foo=$(cat my_text.txt | grep -B5 'application' | paste -s --delimiters=" " |sed 's/--/\n/g'| awk '{print $1 " " $2 " " $3 " " $4 " " $5}')
This is the output and the content of $foo.
Upvotes: 0
Views: 177
Reputation: 11796
You can do this by using a herestring in place of a filename:
grep "word" <<< "$foo"
This will work if your command only requires a single input file/variable. If you require more than one, like your example awk
command, you need to use process substitution:
awk 'FNR==NR{a[$1]++;next}a[$1]' <(echo "$foo") <(echo "$fee")
The <(...)
construct runs the inner commands, then the output is treated as if it is a file.
Examples:
$ echo "$foo"
first line
second line
last one
$ echo "$fee"
example
text
$ grep "line" <<< "$foo"
first line
second line
$ grep "last" <(echo "$foo")
last one
$ awk '{print NR": "$0}' <(echo "$foo") <(echo "$fee")
1: first line
2: second line
3: last one
4: example
5: text
Upvotes: 3
Reputation: 786329
If you're doing word search using awk then use:
awk -v w="word" '$0 ~ w' "$foo"
Assuming $foo
is a file.
You can even use:
awk '/word/' "$foo"
Upvotes: 0