Home > Archive > AWK > July 2004 > File format
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Graeme 2004-07-09, 3:55 am |
| Greeting awkers
I'm a novice with awk but I 'm looking for a script to format the following
111:222
222:222
333:222
111:111
111:333
222:333
333:333
outpu file
222
111
222
333
111
111
333
111
222
333
awk 'BEGIN{FS=":"}{print $2,$0}'
i've got the field seperator etc and have the following which reverses
the fields but this is not
really waht I want ; any pointers apprecaited !
Regards
Graee
| |
| Ian Stirling 2004-07-09, 8:55 am |
| Graeme <gmartin@fhs.usyd.edu.au> wrote:
> Greeting awkers
>
> I'm a novice with awk but I 'm looking for a script to format the following
>
> 111:222
> 222:222
> 333:222
> 111:111
> 111:333
> 222:333
> 333:333
>
> outpu file
> 222
> 111
> 222
> 333
> 111
> 111
> 333
> 111
> 222
> 333
I don't understand the conversion.
Could you amplify?
| |
| Lawson Hanson 2004-07-09, 3:56 pm |
| Graeme wrote:
> Greeting awkers
>
> I'm a novice with awk but I 'm looking for a script to format the following
>
> 111:222
> 222:222
> 333:222
> 111:111 111:333
> 222:333
> 333:333
>
> outpu file
> 222
> 111
> 222
> 333
> 111
> 111
> 333
> 111
> 222
> 333
>
> awk 'BEGIN{FS=":"}{print $2,$0}'
>
> i've got the field seperator etc and have the following which reverses
> the fields but this is not
> really waht I want ; any pointers apprecaited !
> Regards
> Graee
If the line "111:111 111:333" is likely to exist in the input, then you
need to separate that into two lines first; that is easy if you are
working on Unix or Linux which handles pipes, etc.:
awk '{
for (i=1; i <= NF; i++) { print $i }
}' inputFileName | awk -F: '{
if ($2 != same) {
print $2
same = $2
}
print "\t" . $1
}' -
You could also do this in one awk process if you use the "split"
function, for example:
awk 'BEGIN { same = -1 } {
for (i=1; i<=NF; i++) {
split($1,ary,/:/)
if ($ary[2] != same) {
print $ary[2]
same = $ary[2]
}
print $ary[1]
}
}' inputFileName
And remember to change "awk" to "gawk" or "nawk" as appropriate for your
computer platform.
Regards,
Lawson Hanson
| |
| Ted Davis 2004-07-09, 3:56 pm |
| On Fri, 09 Jul 2004 17:10:13 +1000, Graeme <gmartin@fhs.usyd.edu.au>
wrote:
> Greeting awkers
>
>I'm a novice with awk but I 'm looking for a script to format the following
>
>111:222
>222:222
>333:222
>111:111
>111:333
>222:333
>333:333
>
>outpu file
>222
> 111
> 222
> 333
>111
> 111
>333
> 111
> 222
> 333
>
>awk 'BEGIN{FS=":"}{print $2,$0}'
>
>i've got the field seperator etc and have the following which reverses
>the fields but this is not
>really waht I want ; any pointers apprecaited !
I suspect there is more to this than is shown. From the given
material, I deduce that if the second field changes, print it, then
print the first field of each line with the same second field that
immediately follows the change indented by five spaces:
BEGIN{
FS= ":"
Tag = ""
}
{
if( $0 == "" ) next
if( $2 != Tag ) {
Tag = $2
print $2
}
print " " $1
}
I added code to skip blank lines. If the indent is actually supposed
to be a tab, replace " " with "\t".
T.E.D. (tdavis@gearbox.maem.umr.edu)
SPAM filter: Messages to this address *must* contain "T.E.D."
somewhere in the body or they will be automatically rejected.
| |
| Eric Pement 2004-07-09, 3:56 pm |
| Graeme <gmartin@fhs.usyd.edu.au> wrote in message news:<cclgee$pno$1@spacebar.ucc.usyd.edu.au>...
> I'm a novice with awk but I 'm looking for a script to format the following
>
> 111:222
> 222:222
> 333:222
> 111:111
> 111:333
> 222:333
> 333:333
>
> outpu file
> 222
> 111
> 222
> 333
> 111
> 111
> 333
> 111
> 222
> 333
Try this:
awk -F: 'v != $2 {print $2; v=$2}; {print "\t" $1}' yourfile
--
Eric Pement
| |
| Ed Morton 2004-07-09, 3:56 pm |
|
Ted Davis wrote:
> On Fri, 09 Jul 2004 17:10:13 +1000, Graeme <gmartin@fhs.usyd.edu.au>
> wrote:
>
>
>
>
> I suspect there is more to this than is shown. From the given
> material, I deduce that if the second field changes, print it, then
> print the first field of each line with the same second field that
> immediately follows the change indented by five spaces:
>
> BEGIN{
> FS= ":"
> Tag = ""
> }
>
> {
> if( $0 == "" ) next
> if( $2 != Tag ) {
> Tag = $2
> print $2
> }
> print " " $1
> }
>
> I added code to skip blank lines. If the indent is actually supposed
> to be a tab, replace " " with "\t".
I think you figured out the requirements, which is more than I could do
when I first read the OP, but I think it'd be more awk-ish to write the
above as:
BEGIN{FS=":"}
$0 == "" {next}
$2 != Tag {Tag=$2;print Tag}
{print " " $1}
and I suspect there's actually no need for the "$0 ==..." line since
there's no reason I can see to think the input file might have lines in
any format (blank or otherwise) other than that posted.
Regards,
Ed.
>
>
> T.E.D. (tdavis@gearbox.maem.umr.edu)
> SPAM filter: Messages to this address *must* contain "T.E.D."
> somewhere in the body or they will be automatically rejected.
| |
| Ted Davis 2004-07-09, 8:55 pm |
| On Fri, 09 Jul 2004 09:23:21 -0500, Ed Morton <morton@lsupcaemnt.com>
wrote:
>
>
>Ted Davis wrote:
>
>I think you figured out the requirements, which is more than I could do
>when I first read the OP, but I think it'd be more awk-ish to write the
>above as:
>
> BEGIN{FS=":"}
> $0 == "" {next}
> $2 != Tag {Tag=$2;print Tag}
> {print " " $1}
>
>and I suspect there's actually no need for the "$0 ==..." line since
>there's no reason I can see to think the input file might have lines in
>any format (blank or otherwise) other than that posted.
>
Most of the code I write has to be comprehensible to my boss - he can
sort-of make out the C-like fully expressed style I use, but wouldn't
have a chance with the abbreviated style. There are plenty of
examples of several styles in the gawk manual, but I haven't found
many there that even try to reduce the character count to the absolute
minimum - they tend more toward open and explicit formats which are
easier to understand. (My C-like style is also easier fro me to
understand a couple of years later when it seems that though I
generated three times as many lines of comments as of code, the
comments (and the documentation) never seem to address what is
important later.)
T.E.D. (tdavis@gearbox.maem.umr.edu)
SPAM filter: Messages to this address *must* contain "T.E.D."
somewhere in the body or they will be automatically rejected.
| |
| Graeme 2004-07-11, 8:55 pm |
|
Thanx everyone ; I appreciate the prompt responses !! Saved myself
lots of time
Graeme wrote:
> Greeting awkers
>
> I'm a novice with awk but I 'm looking for a script to format the
> following
>
> 111:222
> 222:222
> 333:222
> 111:111 111:333
> 222:333
> 333:333
>
> outpu file
> 222
> 111
> 222
> 333
> 111
> 111
> 333
> 111
> 222
> 333
>
> awk 'BEGIN{FS=":"}{print $2,$0}'
>
> i've got the field seperator etc and have the following which reverses
> the fields but this is not
> really waht I want ; any pointers apprecaited !
> Regards
> Graee
>
>
>
>
>
>
|
|
|
|
|