I need to scan a file with many different special charecters and values. Given a set of special charecters - I need to provide the value next to it:
547 %$
236 \"
4523 &*
8876 (*
8756 "/
...
I am using an awk command with gsub in order to find the sequences as they are.
awk -v st="$match_string" 'BEGIN {gsub(/(\[|\]|\-|\$|\*|\:|\+|\"|\(|\))/,"\\\\&", st)} match($0,st) {print;exit}' file.txt
The command works great e.g.
> (*
>> 8876 (*
However I am having trouble using the command to locate the \" sequence I am trying to add to the gsub different strings to represnt the sequence:
|\\|
|\\\\|
|\\\\"|
...
But the result is always:
> \"
>> 8756 "/
while the result I am looking for woould be:
> \"
>> 236 \"
It seems that the gsub does not work, and the \" is interpeted just as " Any ideas?
follwoing is a short script to run - - it should find the symbol attached to the value in first_num - Next it should print the first value in the file attched to the symbol found
first_num=$1
echo "looking for : $first_num"
sym_to_check=$(awk -v s="$first_num" '$0~s {if ($0~s)print $2}' temp.txt)
echo "symbol - $sym_to_check"
first_val=$(awk -v s="$sym_to_check" 'BEGIN {gsub(/(\[|\]|\-|\$|\^|\*|\:|\+|\"|\(|\))/,"\\\\&",s)} $0~s {if ($0~s)print; if ($0~s)exit}' temp.txt)
echo "first val- $first_val"
suppose the txt file is:
547 %$
111 [*
222 ()
5655 (*
454 )"
35 #!
743 \"
657 #!
236 \"
4523 &*
8876 (*
456 \"
8756 "/
first run is good:
> bash temp1.sh 8876
looking for : 8876
symbol - (*
first val- 5655 (*
the script finds the first value attached to (* but the next run is bad:
> bash temp1.sh 236
looking for : 236
symbol - \"
first val- 454 )"
the symbol is correct - looking for \" but when searching for the first value attached to it, it looks for the first symbol with " This gives the value 454 )" instead of the desired 743 \"