Text formatting 
Author Message
 Text formatting

Hi people!

I'm having a (stupid) problem.

I've a file containing a lines like this:

19990517150304
19990518160206

meaning year, month, day, hour, minute and second

What I want it to be able to retreive the lines in this format:

1999.05.17.15.03.04
1999.05.18.16.02.06

Do that I could retrive information easly with awk.

Can someone help me?

Thanks a lot.

--
Miguel Manso



Mon, 05 Nov 2001 03:00:00 GMT  
 Text formatting
        In Article 10517 of comp.lang.awk, "Miguel Manso"

Quote:

> I'm having a (stupid) problem.

> I've a file containing a lines like this:

> 19990517150304
> 19990518160206

> meaning year, month, day, hour, minute and second

> What I want it to be able to retreive the lines in this format:

> 1999.05.17.15.03.04
> 1999.05.18.16.02.06

> Do that I could retrive information easly with awk.

> Can someone help me?

        Since you don't have any character that can easily be
used to separate the data fields, your best approach is to use
substr to parse each line of input.  The following 'one-liner'
(tested) does what you are looking for:

xawk '
{
    printf("%s.%s.%s.%s.%s.%s\n", substr($0,1,4), substr($0,5,2),
           substr($0,7,2), substr($0,9,2), substr($0,11,2),
           substr($0,13,2))

Quote:
}'

    Note that this treats the parsed information as strings when
printing them out.

Save this script to an executable file and run using the command

        scriptfile < datafile

                                        DaveB



Mon, 05 Nov 2001 03:00:00 GMT  
 Text formatting

Quote:

> I've a file containing a lines like this:

> 19990517150304
> 19990518160206

> meaning year, month, day, hour, minute and second

> What I want it to be able to retreive the lines in this format:

> 1999.05.17.15.03.04
> 1999.05.18.16.02.06

{ print substr($1, 1,4) "." \
        substr($1, 5,2) "." \
        substr($1, 7,2) "." \
        substr($1, 9,2) "." \
        substr($1,11,2) "." \
        substr($1,13,2)
 }

+---------------------------------------------------------------------+
| Juergen Kahrs,       STN Atlas Elektronik GmbH,   D-28305 Bremen    |
| Simulation Division  Sebaldsbruecker Heerstr. 235 +49/421/457-2819  |
+----------- http://home.t-online.de/home/Juergen.Kahrs/ -------------+



Mon, 05 Nov 2001 03:00:00 GMT  
 Text formatting

Quote:

> Hi people!

> I'm having a (stupid) problem.

> I've a file containing a lines like this:

> 19990517150304
> 19990518160206

> meaning year, month, day, hour, minute and second

> What I want it to be able to retreive the lines in this format:

> 1999.05.17.15.03.04
> 1999.05.18.16.02.06

> Do that I could retrive information easly with awk.

> Can someone help me?

> Thanks a lot.

> --
> Miguel Manso


in gawk you can use the FIELDWIDTHS variable.

BEGIN { FIELDWIDTHS="4 2 2 2 2 2" }
{ printf "%s.%s.%s.%s.%s.%s\n",$1,$2,$3,$4,$5,$6 }

or even shorter  :

BEGIN { FIELDWIDTHS="4 2 2 2 2 2" ; OFS="\."}
{ $1=$1; print $0}

the $1=$1 is needed to remake the $0 with the OFS included.

goodluck, Eiso
__________________________________________________________________

           o                     Eiso AB

                 o               Dept. of Biochemistry
                                 University of Groningen                
                                 The Netherlands                      
                  o  
            . .    
         o   ^                  
         |   -   _              
          \__|__/              
             |                  
             |
            / \
           /   \
           |   |
________ ._|   |_. ________________________________________________



Tue, 06 Nov 2001 03:00:00 GMT  
 Text formatting

Quote:

> Thanks a lot for the help.

> the FIELDWIDTHS is only available in gawk, right? I'm working on a Solaris machine that have only awk.

> --
> Miguel Manso


1. Please reply to the newgroup, then other people can benefit from
  the answers.
2. As far as I know FIELDWIDTH is only in gawk. But I would guess you
 can get a version of gawk for solaris machines. So you might ask your
 systemmanager to install it or do it yourself. see the FAQ for where
 to get it.

Eiso

Quote:
> in gawk you can use the FIELDWIDTHS variable.

> > BEGIN { FIELDWIDTHS="4 2 2 2 2 2" }
> > { printf "%s.%s.%s.%s.%s.%s\n",$1,$2,$3,$4,$5,$6 }

> > or even shorter  :

> > BEGIN { FIELDWIDTHS="4 2 2 2 2 2" ; OFS="\."}
> > { $1=$1; print $0}

> > the $1=$1 is needed to remake the $0 with the OFS included.

> > goodluck, Eiso
> > __________________________________________________________________

> >            o                     Eiso AB

> >                  o               Dept. of Biochemistry
> >                                  University of Groningen
> >                                  The Netherlands
> >                   o
> >             . .
> >          o   ^
> >          |   -   _
> >           \__|__/
> >              |
> >              |
> >             / \
> >            /   \
> >            |   |
> > ________ ._|   |_. ________________________________________________

--
__________________________________________________________________

           o                     Eiso AB

                 o               Dept. of Biochemistry
                                 University of Groningen                
                                 The Netherlands                      
                  o  
            . .    


          \__|__/                http://www.nmr.chem.rug.nl
             |                  
             |
            / \
           /   \
           |   |
________ ._|   |_. ________________________________________________



Tue, 06 Nov 2001 03:00:00 GMT  
 Text formatting
Miguel, try this:

nawk '{gsub("..","& "); print $1 $2,$3,$4,$5,$6,$7;}' OFS="."
19990517150304
1999.05.17.15.03.04
19990518160206
1999.05.18.16.02.06

: Hi people!

: I'm having a (stupid) problem.

: I've a file containing a lines like this:

: 19990517150304
: 19990518160206

: meaning year, month, day, hour, minute and second

: What I want it to be able to retreive the lines in this format:

: 1999.05.17.15.03.04
: 1999.05.18.16.02.06

: Do that I could retrive information easly with awk.

: Can someone help me?

: Thanks a lot.

: --
: Miguel Manso



Mon, 12 Nov 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. RTF-text format in text fields ?

2. Text format of text widget

3. Importing other text formats into the tk text widget

4. How to see Hierarchy Diagrams in text format?

5. text formatting

6. text formatting script

7. Text Formatting with Awk

8. Text Formatting Anomalie

9. Clarion text formatting source-FREE

10. Push Button Text Formatting

11. RTF (Rich Text Format) driver or library

12. Reports in RTF (Rich Text Format)

 

 
Powered by phpBB® Forum Software