Given a csv describing firstname and lastname of parent-child relationship
$ cat /var/tmp/hier
F2 L2,F1 L1
F3 L3,F1 L1
F4 L4,F2 L2
F5 L5,F2 L2
F6 L6,F3 L3
I want to print:
F1 L1
F2 L2
F4 L4
F5 L5
F3 L3
F6 L6
I wrote a script like below:
#!/bin/bash
print_node() {
echo "awk -F, '\$2=="\"$@\"" {print \$1}' /var/tmp/hier"
for node in `eval "awk -F, '\$2=="\"$@\"" {print \$1}' /var/tmp/hier"`
do
echo -e "\t"$node
print_node "$node"
done
}
print_node "$1"
run the script:
$ ./print_tree.sh "F1 L1"
awk -F, '$2=="F1 L1" {print $1}' /var/tmp/hier
awk: syntax error near line 1
awk: bailing out near line 1
It seemed that the awk command was malformed. but if I run the command shown in the debug output, it works:
$ awk -F, '$2=="F1 L1" {print $1}' /var/tmp/hier
F2 L2
F3 L3
What might be causing this error?
for node in `eval "awk ...`is sufficiently complex that you should be rethinking from the ground up. I for one simply decline to spend time working out what you're trying to do, and why your code isn't achieving it. You should be able to runawkonce, and it should do all the processing. (Or Perl, or Python, or …)