
On 22/11/11 14:06, Marcus Furlong wrote:
On Thu, Nov 17, 2011 at 15:56, Peter Ross <Peter.Ross@bogen.in-berlin.de> wrote:
Hi Markus,
in principle it works, you can put all stuff in one line and run it between single quotes as a awk command line.
There is a little script doing this for you:
cat $my_awk_file | awk '{if (NR>1) for (i=1; i<=NF; i++) printf $i" "}'
In general it works, however, you have to escape the single quotes _inside_ the script.
"'" has to be written as "'"'"'"
Well, that syntax hurts;-) especially if you want to write a awk line that processes an awk script to get an awk line..
And than you get:
cat $my_awk_file | \ awk -F"'" '{for (i=1; i<NF; i++) printf $i"'"'"'""\"""'"'"'""\"""'"'"'"; print $NF}' | \ awk '{if (NR>1) for (i=1; i<=NF; i++) printf $i" "}'
The appearing output as a command line to do what the awk script does:
cat $my_file | awk '{ if ($1=="Repo-name") {printf "'"'"'"; for (i=3; i<NF; i++) printf $i" "; printf $NF"'"'"' "} if ($1=="Repo-baseurl") { url=1; comma=match($NF,","); if (comma) out=substr($NF,1,comma-1); else out=$NF; printf "'"'"'"out"'"'"' "; } else { if (url==1) { if ($1==":") { comma=match($NF,","); if (comma) out=substr($NF,1,comma-1); else out=$NF; printf "'"'"'"out"'"'"' "; } else {url=0; print "";} } } }'
Well, that's all in one line now - but who can read that? Agreed, it's a bit unwieldy!
Feel free to use whatever you like;-) Will continue to investigate options, thanks again for your help, at least I have something that works now :)
Another option is to use perl. sed, awk and bash are all useful tools for simple tasks, but as the complexity of what you are doing rises, they get unwieldy fast. Perl tends to scale better. You're at least bordering on where perl would be cleaner if treated as a text processing exercise. Alternatively, it looks like there's some CPAN modules for binding to the RPM API directly, so you would have a data structure to navigate rather than a text processing exercise. If what you have is working, then I don't expect it's worth re-doing it, but if you're building further on this, it might be worth thinking about. Andrew