i have script that filter several text file using grep and awk in loop, my issue is when create output to each file after filtered,
this my script:
grep_multi.sh
path=$(find /home/folder/file/source -iname "Tracert*" )
for i in "$path"
do
grep -E '^%%.*.%%$'\|'IPv4 type' $i | awk '/%%TRACERT:/ {sfx = $0; next} {print $1","$2","$3","$4","$5","$6","$7" "$8","sfx}' > filter.result.$i
done
when running the script i got error like this:
./grep_multi.sh: line 5: filter.result.$i: ambiguous redirect
this for variabel $path
$ find /home/folder/file/source -iname "Tracert*"
/home/folder/file/source/Tracert_1.txt
/home/folder/file/source/Tracert_2.txt
/home/folder/file/source/Tracert_3.txt
/home/folder/file/source/Tracert_4.txt
/home/folder/file/source/Tracert_5.txt
/home/folder/file/source/Tracert_6.txt
/home/folder/file/source/Tracert_7.txt
/home/folder/file/source/Tracert_8.txt
tracert_1.txt
O&M #108
%%TRACERT: IPTYPE=IPv4, LOCALIP4="10.10.10.10", PEERIP4="10.10.10.10", MAXHOP=15;%%
RETCODE = 0 Operation succeeded
The result is as follows
------------------------
Record index Response number First response time(ms) Second response time(ms) Third response time(ms) IP type Peer IP address
1 3 1 1 1 IPv4 type 10.10.10.10
2 3 1 1 1 IPv4 type 10.10.10.10
3 0 NULL NULL NULL IPv4 type Timeout
4 0 NULL NULL NULL IPv4 type Timeout
5 3 1 1 1 IPv4 type 10.10.10.10
6 3 1 1 1 IPv4 type 10.10.10.10
7 3 1 1 1 IPv4 type 10.10.10.10
"$path"
you are causing the loop to run once, with$i
expanding to the whole list of paths. See this somewhat related question Why is looping over find's output bad practice? – steeldriver Oct 16 '19 at 14:14%%.*.%%
=>%%.*%%
, unsure why we close a quoted block, escape a pipe then open a new quote). Could you give us a sample of what you're trying to extract? – SYN Oct 16 '19 at 14:30grep -E '%%.*%%$|IPv4 type' $i ...
– SYN Oct 16 '19 at 15:07