
Hi Marcus, first I had an error (spot the url=1 instead of url==1 condition?) so it did not work that well for multiple appearences. However, this one should do: #!/usr/bin/awk -f { 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 "";} } } } I made it a bit more "C-like" in appearance but the logic is the same. Regards Peter Quoting "Marcus Furlong" <furlongm@gmail.com>:
On Wed, Nov 16, 2011 at 15:52, Peter Ross <Peter.Ross@bogen.in-berlin.de> wrote:
Hi Marcus,
I copied your example into a file (because I do not have a RPM based Linux at hand)
Repo-id : extras Repo-name : CentOS-5 - Extras Repo-updated : Tue Oct 25 03:18:58 2011 Repo-pkgs : 237 Repo-size : 124 M Repo-baseurl : http://ftp.swin.edu.au/centos/5/extras/x86_64/, : http://ftp.monash.edu.au/pub/linux/CentOS/5/extras/x86_64/, : http://mirror.aarnet.edu.au/pub/centos/5/extras/x86_64/ Repo-expire : 3,600 second(s) (last: Wed Nov 16 12:39:48 2011)
and piped into the following quick&dirty awk script:
#!/usr/bin/awk -f
$1=="Repo-name" {printf "'"; for (i=3; i<NF; i++) printf $i" "; printf $NF"' "} $1=="Repo-baseurl" {url=1; comma=match($NF,","); if (comma) out=substr($NF,1,comma-1); else out=$NF printf "'"out"' "} url=1 {if ($1==":") { comma=match($NF,","); if (comma) out=substr($NF,1,comma-1); else out=$NF printf "'"out"' "} else url=0}
The result is:
$ cat /tmp/mom | /tmp/mom.awk 'CentOS-5 - Extras' 'http://ftp.swin.edu.au/centos/5/extras/x86_64/' 'http://ftp.monash.edu.au/pub/linux/CentOS/5/extras/x86_64/' 'http://mirror.aarnet.edu.au/pub/centos/5/extras/x86_64/'
(One line actually)
Happy with it?
Hi Peter,
Thanks, yes! It almost does the trick. If there were multiple repos (e.g. the above text repeated again a few times), how would I go about adding a newline after each one? I've tried to modify your example but my awk-fu is not good! E.g. the following is on two lines:
'CentOS-5 - Extras' 'http://ftp.swin.edu.au/centos/5/extras/x86_64/' 'http://ftp.monash.edu.au/pub/linux/CentOS/5/extras/x86_64/' 'http://mirror.aarnet.edu.au/pub/centos/5/extras/x86_64/' 'CentOS-5 - Updates' 'http://ftp.swin.edu.au/centos/5/updates/x86_64/' 'http://ftp.monash.edu.au/pub/linux/CentOS/5/updates/x86_64/' 'http://mirror.aarnet.edu.au/pub/centos/5/updates/x86_64/'
Sorry for not debugging your sed scripts, I have a problem to understand my own;-) (That's why I prefer awk as long as it seems doable by awk, it is easier to understand and read than sed)
Haha, yeah I often cringe when I have to go back months later and try to understand what I wrote in sed.
Thanks, Marcus.
-- Marcus Furlong