For Programmers: Free Programming Magazines  


Home > Archive > AWK > December 2006 > awk and fixed-width data









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]

 

Author awk and fixed-width data
ams100272@gmail.com

2006-12-11, 7:00 pm

Hi awk masters --

Simple task. Suppose I want to change the 22nd character in each line
of a text file to the letter X. I assume this is very easy in awk but I
don't know how to do it.

To give a little more detail, I have a fixed-width text file defined as
follows:

(note: the leftmost column is column 1)
cols 1-6, the string "ATOM "
cols 7-11, an integer representing the atom's serial number
col 12 unused
....
col 22 chainID
....

and so on. In column 22 is a single character which I want to change.

In order to mirror this structure I wrote out

BEGIN { FIELDWIDTHS = "6 5 1 ... 1 ... and so on ... "
print $8 }

which I put together based on a gawk reference on the web. (The field
$8 should be the item I want to modify as long as there are no mistakes
in my FIELDWIDTHS.) It doesn't work.

Is this a gawk-versus-whatever-awk-I-have problem? How do I find out
which awk is my awk anyway? (-help, --help, -version and --version
don't do it.)

Thanks & regards, Adam, Chicago, IL

Janis Papanagnou

2006-12-11, 7:00 pm

ams100272@gmail.com wrote:
> Hi awk masters --
>
> Simple task. Suppose I want to change the 22nd character in each line
> of a text file to the letter X. I assume this is very easy in awk but I
> don't know how to do it.
>
> To give a little more detail, I have a fixed-width text file defined as
> follows:
>
> (note: the leftmost column is column 1)
> cols 1-6, the string "ATOM "
> cols 7-11, an integer representing the atom's serial number
> col 12 unused
> ...
> col 22 chainID
> ...
>
> and so on. In column 22 is a single character which I want to change.
>
> In order to mirror this structure I wrote out
>
> BEGIN { FIELDWIDTHS = "6 5 1 ... 1 ... and so on ... "
> print $8 }
>
> which I put together based on a gawk reference on the web. (The field
> $8 should be the item I want to modify as long as there are no mistakes
> in my FIELDWIDTHS.) It doesn't work.


If you want to just replace a character using FIELDWIDTHS see the follwing
example...

echo "ABCDEF12345X0abc" | awk '

BEGIN { OFS=""; FIELDWIDTHS = "6 5 1 1 3" }
$3 = "Z"

'

Which will produce...

ABCDEF12345Z0abc


You can also leave that GNU awk feature aside and use substr() instead...

{ print substr($0,1,n-1) "Z" newchar substr($0,n+1) }

....where n is the character column of the character to be replace by "Z".

Janis

>
> Is this a gawk-versus-whatever-awk-I-have problem? How do I find out
> which awk is my awk anyway? (-help, --help, -version and --version
> don't do it.)
>
> Thanks & regards, Adam, Chicago, IL
>

news.t-online.de

2006-12-11, 7:00 pm

ams100272@gmail.com wrote:
> Hi awk masters --
>
> Simple task. Suppose I want to change the 22nd character in each line
> of a text file to the letter X. I assume this is very easy in awk but I
> don't know how to do it.
>
> To give a little more detail, I have a fixed-width text file defined as
> follows:
>
> (note: the leftmost column is column 1)
> cols 1-6, the string "ATOM "
> cols 7-11, an integer representing the atom's serial number
> col 12 unused
> ...
> col 22 chainID
> ...
>
> and so on. In column 22 is a single character which I want to change.
>
> In order to mirror this structure I wrote out
>
> BEGIN { FIELDWIDTHS = "6 5 1 ... 1 ... and so on ... "
> print $8 }
>
> which I put together based on a gawk reference on the web. (The field
> $8 should be the item I want to modify as long as there are no mistakes
> in my FIELDWIDTHS.) It doesn't work.
>
> Is this a gawk-versus-whatever-awk-I-have problem? How do I find out
> which awk is my awk anyway? (-help, --help, -version and --version
> don't do it.)
>
> Thanks & regards, Adam, Chicago, IL
>

there is a bug in certain versions of gawk.
The last field is swallowed.
Have a look at the bug list or get the
Newest version.
Hermann Peifer

2006-12-11, 7:00 pm

ams100272@gmail.com wrote:
> Hi awk masters --
>
> Simple task. Suppose I want to change the 22nd character in each line
> of a text file to the letter X. I assume this is very easy in awk but I
> don't know how to do it.
> ...


Hi,

I am an awk beginner rather than an awk master, however this should also
work:

BEGIN{FS=OFS=""}$22="X"

Hermann
ams100272@gmail.com

2006-12-11, 7:00 pm

Thanks for your help all! - Adam

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com