For Programmers: Free Programming Magazines  


Home > Archive > AWK > February 2008 > manipulating texts









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 manipulating texts
Minalba Jordao

2008-02-12, 7:59 am

Hello!

I am a newbie.

I have a few problems.

I have a file that looks like:
"
! comments
! comments
! comments

@ text
X
Y
Z
@

column1 column2 column3 column4
column1 column2 column3 column4
.... ... ...
.... ... ...
column1 column2 column3 column4
column1 column2 column3 column4
"


My questions:
I would to get the
- "! coments"
- what is between the @ (at sign).
and
column1 column2 -column3
for each line..

could anyone help me?

Mina
Minalba Jordao

2008-02-12, 7:59 am

Including the @ (at sign) and what is in between.

tks in advance
On 12 fev, 10:57, Minalba Jordao <minalbajor...@gmail.com> wrote:
> Hello!
>
> I am a newbie.
>
> I have a few problems.
>
> I have a file that looks like:
> "
> ! comments
> ! comments
> ! comments
>
> @ text
> X
> Y
> Z
> @
>
> column1 column2 column3 column4
> column1 column2 column3 column4
> ... ... ...
> ... ... ...
> column1 column2 column3 column4
> column1 column2 column3 column4
> "
>
> My questions:
> I would to get the
> - "! coments"
> - what is between the @ (at sign).
> and
> column1 column2 -column3
> for each line..
>
> could anyone help me?
>
> Mina


Ed Morton

2008-02-12, 7:59 am



On 2/12/2008 6:57 AM, Minalba Jordao wrote:
> Hello!
>
> I am a newbie.
>
> I have a few problems.
>
> I have a file that looks like:
> "
> ! comments
> ! comments
> ! comments
>
> @ text
> X
> Y
> Z
> @
>
> column1 column2 column3 column4
> column1 column2 column3 column4
> ... ... ...
> ... ... ...
> column1 column2 column3 column4
> column1 column2 column3 column4
> "
>
>
> My questions:
> I would to get the
> - "! coments"
> - what is between the @ (at sign).
> and
> column1 column2 -column3
> for each line..
>
> could anyone help me?
>
> Mina


This MAY be what you want, but it's not clear:

awk '
/^@/ { a=!a; b=1; next }
a || /^!/ { print; next }
b { print $1,$2,$3 }
' file

If not, post some sample input and expected output.

Ed.

Ed Morton

2008-02-12, 7:59 am



On 2/12/2008 7:08 AM, Minalba Jordao wrote:

[please don't top-post, fixed below]

> On 12 fev, 10:57, Minalba Jordao <minalbajor...@gmail.com> wrote:
>
>
> Including the @ (at sign) and what is in between.
>


This MAY be what you want, but it's not clear:

awk '
/^@/ { print; a=!a; b=1; next }
a || /^!/ { print; next }
b { print $1,$2,$3 }
' file

If not, post some sample input and expected output.

Ed.

Minalba Jordao

2008-02-12, 7:59 am

On 12 fev, 11:15, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 2/12/2008 6:57 AM, Minalba Jordao wrote:
>
>
>
>
>
>
>
>
>
>
>
>
> This MAY be what you want, but it's not clear:
>
> awk '
> /^@/ { a=!a; b=1; next }
> a || /^!/ { print; next }
> b { print $1,$2,$3 }
> ' file
>
> If not, post some sample input and expected output.
>
> Ed.


that worked!
But it did`t include the @ sign.
' comments
! comments
! comments
!
X
Y
Z
1865.16833 6639.33984 -3278.68018
1865.21423 6539.07471 -3284.02954
1889.54529 6414.59814 -3287.10986


and it should be like
' comments
! comments
! comments
!
@
X
Y
Z
@
1865.16833 6639.33984 -3278.68018
1865.21423 6539.07471 -3284.02954
1889.54529 6414.59814 -3287.10986

Ed Morton

2008-02-12, 7:59 am



On 2/12/2008 7:23 AM, Minalba Jordao wrote:
> On 12 fev, 11:15, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>
>
> that worked!
> But it did`t include the @ sign.
> ' comments
> ! comments
> ! comments
> !
> X
> Y
> Z
> 1865.16833 6639.33984 -3278.68018
> 1865.21423 6539.07471 -3284.02954
> 1889.54529 6414.59814 -3287.10986
>
>
> and it should be like
> ' comments
> ! comments
> ! comments
> !
> @
> X
> Y
> Z
> @
> 1865.16833 6639.33984 -3278.68018
> 1865.21423 6539.07471 -3284.02954
> 1889.54529 6414.59814 -3287.10986
>


See my other post in this thread after you'd clarified that requirement in a
followup to your original post.

Ed.

Minalba Jordao

2008-02-12, 6:58 pm

On 12 fev, 11:33, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 2/12/2008 7:23 AM, Minalba Jordao wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> See my other post in this thread after you'd clarified that requirement in a
> followup to your original post.
>
> Ed.


Ed,
It did work. Thank you.
But I didn`t understand acctually what you did.

In the first line, you look for the @ sign in the begining of of the
line, then you print what you search. OK, then a=!a and b=1, I didn`t
understand what does this means.
/^@/ { print; a=!a; b=1; next }
a || /^!/ { print; next }
b { print $1,$2,"-"$3 }

What can I read to understand it better?
Thank you again.

If I need to to that, with many files? Could I do it with awk? Or will
I need to use some shell scripting?

$
for file in *.dat; do ./trans.sh $file > $file.new; done

transf.sh
#!/bin/sh
awk ' /^@/ { print; a=!a; b=1; next }
a || /^!/ { print; next }
b { print $1,$2,"-"$3 }' $1


Any suggestions?

Ed Morton

2008-02-12, 6:58 pm



On 2/12/2008 8:18 AM, Minalba Jordao wrote:
> On 12 fev, 11:33, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>
>
> Ed,
> It did work. Thank you.
> But I didn`t understand acctually what you did.
>
> In the first line, you look for the @ sign in the begining of of the
> line, then you print what you search. OK, then a=!a and b=1, I didn`t
> understand what does this means.


I'm just setting a couple of flags which I use later to print the records when
those flags are set. When the script starts "a" has the value "zero" (or "null"
if it were treated as a string) so when the first "@" is found, "a=!a" sets a to
"1" so that on the "a || ..." line, "a" is set and so the print occurs. WHen
the second "@" is found later in the file, "a=!a" flips the value of "a" back to
zero so that on the "a || ..." line the print does not occur (at least, not due
to being insid the "@...@" section). Setting "b=1" just makes sure that after
the final "@" is seen, printing of the first 3 fields will occur at the "b {
...." lines of the script.

> /^@/ { print; a=!a; b=1; next }
> a || /^!/ { print; next }
> b { print $1,$2,"-"$3 }
>
> What can I read to understand it better?


The archives of this NG.

> Thank you again.


You're welcome.

> If I need to to that, with many files? Could I do it with awk? Or will
> I need to use some shell scripting?


You can do it with awk:

awk ' FNR==1 { outfile=FILENAME ".new" }
/^@/ { print > outfile; a=!a; b=1; next }
a || /^!/ { print > outfile; next }
b { print $1,$2,"-"$3 > outfile}' *.dat

but your OS is typically better suited for the job, especially if you want to
replace the original files with the modified ones.

> $
> for file in *.dat; do ./trans.sh $file > $file.new; done
>
> transf.sh
> #!/bin/sh
> awk ' /^@/ { print; a=!a; b=1; next }
> a || /^!/ { print; next }
> b { print $1,$2,"-"$3 }' $1
>
>
> Any suggestions?


Anything other than the awk language is OT for this NG so you might get better
help on the shell script at comp.unix.shell, but to start with, always quote
your shell variables unless you know exactly what you're doing and have a very
specific reason not to.

Ed.


Ed Morton

2008-02-12, 6:58 pm



On 2/12/2008 8:48 AM, Ed Morton wrote:
>
> On 2/12/2008 8:18 AM, Minalba Jordao wrote:

<snip>
>
>
> You can do it with awk:
>
> awk ' FNR==1 { outfile=FILENAME ".new" }


Forgot to re-init the variables ("a" not strictly necessary):

awk ' FNR==1 { outfile=FILENAME ".new"; a=b=0 }

> /^@/ { print > outfile; a=!a; b=1; next }
> a || /^!/ { print > outfile; next }
> b { print $1,$2,"-"$3 > outfile}' *.dat
>
> but your OS is typically better suited for the job, especially if you want to
> replace the original files with the modified ones.


Sponsored Links







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

Copyright 2008 codecomments.com