|
|
Help with an input file
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
|
|
0
|
|
|
|
Reply
|
erics584
|
4/21/2009 3:56:01 AM |
|
On Tuesday 21 April 2009 05:56, erics584@gmail.com wrote:
> 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?
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.
|
|
0
|
|
|
|
Reply
|
pk
|
4/21/2009 9:05:08 AM
|
|
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 2009-04-21, erics584@gmail.com <erics584@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.steffen@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
|
|
|
|
Reply
|
Steffen
|
4/21/2009 7:31:57 PM
|
|
|
2 Replies
150 Views
(page loaded in 3.264 seconds)
|
|
|
|
|
|
|
|
|