Issue
I am trying to do a very basic operation in Shell. I have the following file:
id DiaMatch(32) = Sector(C26, 9, 1021) * Shift(k2, k2-p1, [], k3, -k3, [], k1, p1-k1, []);
id DiaMatch(33) = Sector(C26, 9, 1021) * Shift(k2, k2-p1, [], k3, -k3, [], k1, p1-k1, []);
id DiaMatch(34) = Sector(C10, 9, 1022) * Shift(k2, -p1+k1, [], k3, -k3, [], k1, p1-k2, []);
id DiaMatch(35) = Sector(C10, 9, 1022) * Shift(k2, -p1+k1, [], k3, -k3, [], k1, p1-k2, []);
and so on.
I have to extract from this file to a new file with the following:
file C26;
replace_(k2, k2-p1, k3, -k3, k1, p1-k1);
end;
file C26;
replace_(k2, k2-p1, k3, -k3, k1, p1-k1);
end;
file C10;
replace_(k2, -p1+k1, k3, -k3, k1, p1-k2);
end;
and so on
I am trying to do this operation line by line as
while read line; do
sed -n '/shift/{s/.*\[\(.*\)\].*/\1/;s/\[//g;s/\]//g;p}'
echo $line
done < qqbz.match.info.inc
but this is not working. Any idea is appreciated.
Solution
Shell is for launching commands, you could but shouldn't use it for anything much more complicated than that. Awk is not shell, you could use for example it. I left some for exercise (add %s
s and $n
s in the printf
):
$ awk -F"[()]" '{
split($4,a,/, /)
split($6,b,/, /)
printf "file %s;\nreplace(%s, %s, ...);\nend;\n\n",a[1],b[1],b[2]
}' file
Output:
file C26;
replace(k2, k2-p1, ...);
end;
file C26;
replace(k2, k2-p1, ...);
end;
file C10;
replace(k2, -p1+k1, ...);
end;
file C10;
replace(k2, -p1+k1, ...);
end;
Answered By - James Brown Answer Checked By - Timothy Miller (WPSolving Admin)