Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help with an input file

2 views
Skip to first unread message

eric...@gmail.com

unread,
Apr 20, 2009, 11:56:01 PM4/20/09
to
Hello everyone!

I'll be honest, I was a UNIX guy long ago, but that has fallen into a
void due to years of non-use. I don't know what to do and need a
little help from you guys.

I need an input file to be formatted with awk (or another utility) as
per these examples:

http://chocolatine.org/eric/input.txt

http://chocolatine.org/eric/output.txt

So yeah. Got any scripts to take input.txt, and come out with
output.txt?

Thanks in advance!

Eric

pk

unread,
Apr 21, 2009, 5:05:08 AM4/21/09
to

awk '
/^[^[:space:]]/{
if(last""){
print last"; "r;s=""
}
last=$1"; "
sub(/^[^[:space:]]+[[:space:]]+/,"")
r=$0;next
}
{sub(/^[[:space:]]+[^[:space:]]+[[:space:]]+/,"");last=last s $0;s=", "}
END{if(last""){print last"; "r}}' file.txt

assuming the first field in the line never contains spaces.

Steffen Schuler

unread,
Apr 21, 2009, 3:31:57 PM4/21/09
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 2009-04-21, eric...@gmail.com <eric...@gmail.com> wrote:
> [...]


> I need an input file to be formatted with awk (or another utility) as
> per these examples:
>
> http://chocolatine.org/eric/input.txt
>
> http://chocolatine.org/eric/output.txt
>
> So yeah. Got any scripts to take input.txt, and come out with
> output.txt?

> [...]

Hi,

a tested script for this task. It should work with any new awk.

- --------------------------8<--------------------------------
substr($0,1,1) ~ /[ \t]/ {
printf "%s %s", (prev_hdr ? ";" : ","), flds_from(2)
prev_hdr = 0
next
}
NR > 1 {
printf "; %s\n\n", hdr2
}
{
printf "%s", $1
hdr2 = flds_from(2)
prev_hdr = 1
}
END {
printf "; %s\n", hdr2
}
function flds_from(start, s, i)
{
for (i = start; i <= NF; ++i)
s = s (i == start ? "" : " ") $i
return s
}
- --------------------------8<--------------------------------

call it:

awk -f scriptfile input.txt >output.txt

Kind regards,

Steffen

- --
Steffen Schuler (goedel) <schuler...@gmail.com>
Key ID: 0x42C5D853 / Key-server: pgp.mit.edu
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAknuHyAACgkQ+/TWb0LF2FMlBQCdFQ2HaNUyvxd01+eruLy7T4jN
NskAn3zv/TORyRIrwBFdevxxFOa4e+RR
=+iXI
-----END PGP SIGNATURE-----

0 new messages