For Programmers: Free Programming Magazines  


Home > Archive > AWK > May 2004 > new separator









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 new separator
speed

2004-05-12, 7:19 pm

Hello all

I want to replace the third column of file with || and set Field Separator
to || when I'm trying to change it:

awk 'BEGIN {$3="||"; FS="||"} {print $2}' file

$2 is empty and after using {print $1} awk prints:
One 15 32 24 226
Two 15 24 34 228
Three 31 52 63 420
Four 16 34 29 208
Five 31 42 75 492
Six 24 34 67 436
Seven 15 34 47 316
Eight 13 55 37 277

which is the item of file.
Can you tell me where is the mistake??

Thanks

Ed Morton

2004-05-12, 7:19 pm



speed wrote:

> Hello all
>
> I want to replace the third column of file with || and set Field Separator
> to || when I'm trying to change it:
>
> awk 'BEGIN {$3="||"; FS="||"} {print $2}' file
>
> $2 is empty and after using {print $1} awk prints:
> One 15 32 24 226
> Two 15 24 34 228
> Three 31 52 63 420
> Four 16 34 29 208
> Five 31 42 75 492
> Six 24 34 67 436
> Seven 15 34 47 316
> Eight 13 55 37 277
>
> which is the item of file.
> Can you tell me where is the mistake??



If I understand you, the above is your input file. You're specifying
that fields are separated by "||". There is no "||" in the file, so each
line is completedly stored in $1 and so $2 is empty.

Without knowing what you want your output to look like, it's hard to
guess, but I THINK what you're trying to do is change the output field
separator, in which case you need to set "OFS" rather than "FS".

Ed.

> Thanks
>


speed

2004-05-12, 7:19 pm

I will describe more precisely what I want to do
I have the input file:

One 15 32 24 22
Two 15 24 3 4 228
Three 31 52 6 3 4 20
Four 16 34 29 2 08
Five 31 42 7 5 49 2
Six 24 34 67
Seven 15 34 47 3 16
Eight 13 55 3 7 2 77

And I need to print all fields after the 3rd column so I tried to replace
3rd with || and set FS="||" but I don't know how to do it. Do you have any
ideas??

speed

Ed Morton

2004-05-12, 7:19 pm



speed wrote:
> I will describe more precisely what I want to do
> I have the input file:
>
> One 15 32 24 22
> Two 15 24 3 4 228
> Three 31 52 6 3 4 20
> Four 16 34 29 2 08
> Five 31 42 7 5 49 2
> Six 24 34 67
> Seven 15 34 47 3 16
> Eight 13 55 3 7 2 77
>
> And I need to print all fields after the 3rd column so I tried to replace
> 3rd with || and set FS="||" but I don't know how to do it. Do you have any
> ideas??


There's various ways, here's a couple:

awk '{$1=$2=$3="";print}' file

awk '{for (i=3;i<=NF;i++) printf "%s ",$i; print ""}' file

or, what you were tring to do:

awk '{$3="||";print}' file | awk -F"||" '{print $2}'

Regards,

Ed.

> speed
>


Patrick TJ McPhee

2004-05-12, 7:19 pm

In article <_IGdnVH-yu4smjzdRVn-ig@comcast.com>,
Ed Morton <morton@lsupcaemnt.com> wrote:

% awk '{$3="||";print}' file | awk -F"||" '{print $2}'

Does this work? If FS is longer than one character, it's treated
as a regular expression, and | is special in regular expressions.
On my system, nawk and mawk complain about the RE, and gawk
prints an empty string. It does work if you set -F like this

awk '{$3="||";print}' file | awk -F'\\|\\|' '{print $2}'

I'm can't explain why I have two back-slashes there, except that
gawk and nawk both complained about having one back-slash only,
while all three awks worked correctly with two.
--

Patrick TJ McPhee
East York Canada
ptjm@interlog.com
Ed Morton

2004-05-12, 7:19 pm



Patrick TJ McPhee wrote:
> In article <_IGdnVH-yu4smjzdRVn-ig@comcast.com>,
> Ed Morton <morton@lsupcaemnt.com> wrote:
>
> % awk '{$3="||";print}' file | awk -F"||" '{print $2}'
>
> Does this work?


Nope. I didn't test it, or think about it very much before posting.

If FS is longer than one character, it's treated
> as a regular expression, and | is special in regular expressions.
> On my system, nawk and mawk complain about the RE, and gawk
> prints an empty string. It does work if you set -F like this
>
> awk '{$3="||";print}' file | awk -F'\\|\\|' '{print $2}'
>
> I'm can't explain why I have two back-slashes there, except that
> gawk and nawk both complained about having one back-slash only,
> while all three awks worked correctly with two.


Awk magic.... Luckily there's better solutions!

Ed.

Sponsored Links







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

Copyright 2008 codecomments.com