For Programmers: Free Programming Magazines  


Home > Archive > AWK > December 2006 > repeating a string a given number of times









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 repeating a string a given number of times
Seb

2006-12-03, 6:56 pm

Hi,

I have to standardize some *.csv files so that they all have the same
number of fields. They all should have 6 fields, but some lines have less
than that (never more than that). To fix the offending lines, they should
have commas appended at the end of the line. I can't find a function
taking a string argument and repeating it x times, which would do this
easily. So my failed attempt was:


BEGIN {FS=OFS=","}; NF < 6 {print $0; for (i=NF; i < 6; i++) print ","}


which of course prints each added comma in a separate line. Any
suggestions on how to properly append those commas welcome.


Cheers,

--
Seb
Janis Papanagnou

2006-12-03, 6:56 pm

Seb wrote:
> Hi,
>
> I have to standardize some *.csv files so that they all have the same
> number of fields. They all should have 6 fields, but some lines have less
> than that (never more than that). To fix the offending lines, they should
> have commas appended at the end of the line. I can't find a function
> taking a string argument and repeating it x times, which would do this
> easily. So my failed attempt was:
>
>
> BEGIN {FS=OFS=","}; NF < 6 {print $0; for (i=NF; i < 6; i++) print ","}
>
>
> which of course prints each added comma in a separate line. Any
> suggestions on how to properly append those commas welcome.


If it's just a couple fields I'd use...

BEGIN {FS=OFS=","}{print $1,$2,$3,$4,$5,$6}


Janis
Seb

2006-12-03, 6:56 pm

On Sun, 03 Dec 2006 17:01:11 +0100,
Janis Papanagnou <Janis_Papanagnou@hotmail.com> wrote:

[...]

> If it's just a couple fields I'd use...


> BEGIN {FS=OFS=","}{print $1,$2,$3,$4,$5,$6}


Right! I forgot that if the field doesn't exist, nothing gets printed,
but the OFS does.


Thank you Janis,

--
Seb
Kenny McCormack

2006-12-03, 6:56 pm

In article <871wnht1l3.fsf@patagonia.sebmags.homelinux.org>,
Seb <spluque@gmail.com> wrote:
>Hi,
>
>I have to standardize some *.csv files so that they all have the same
>number of fields. They all should have 6 fields, but some lines have less
>than that (never more than that). To fix the offending lines, they should
>have commas appended at the end of the line. I can't find a function
>taking a string argument and repeating it x times, which would do this
>easily.


Well, it looks like you found another way to solve your underlying need,
but to return to the stated problem:

TAWK has strdup() as a built-in, which is nice, but it is easily
replicated on lesser platforms. Here is my usual implementation:

function strdup(s,n, tmp) {
tmp = sprintf("%*s",n,"")
gsub(/ /,s,tmp)
return tmp
}

Seb

2006-12-03, 6:56 pm

On Sun, 3 Dec 2006 16:39:20 +0000 (UTC),
gazelle@xmission.xmission.com (Kenny McCormack) wrote:

[...]

> TAWK has strdup() as a built-in, which is nice, but it is easily
> replicated on lesser platforms. Here is my usual implementation:


> function strdup(s,n, tmp) {
> tmp = sprintf("%*s",n,"")
> gsub(/ /,s,tmp)
> return tmp
> }


Very handy, thanks!


--
Seb
Bill Marcum

2006-12-11, 7:00 pm

On Sun, 03 Dec 2006 15:56:42 GMT, Seb
<spluque@gmail.com> wrote:
> Hi,
>
> I have to standardize some *.csv files so that they all have the same
> number of fields. They all should have 6 fields, but some lines have less
> than that (never more than that). To fix the offending lines, they should
> have commas appended at the end of the line. I can't find a function
> taking a string argument and repeating it x times, which would do this
> easily. So my failed attempt was:
>
>
> BEGIN {FS=OFS=","}; NF < 6 {print $0; for (i=NF; i < 6; i++) print ","}
>
>
> which of course prints each added comma in a separate line. Any
> suggestions on how to properly append those commas welcome.
>

Use printf to print without a newline.


--
Good day to let down old friends who need help.
William James

2006-12-11, 7:00 pm

Seb wrote:
> Hi,
>
> I have to standardize some *.csv files so that they all have the same
> number of fields. They all should have 6 fields, but some lines have less
> than that (never more than that). To fix the offending lines, they should
> have commas appended at the end of the line. I can't find a function
> taking a string argument and repeating it x times, which would do this
> easily. So my failed attempt was:
>
>
> BEGIN {FS=OFS=","}; NF < 6 {print $0; for (i=NF; i < 6; i++) print ","}
>
>
> which of course prints each added comma in a separate line. Any
> suggestions on how to properly append those commas welcome.
>
>
> Cheers,
>
> --
> Seb


BEGIN{FS=OFS=","}{$6=$6; print}

Seb

2006-12-11, 7:00 pm

On 7 Dec 2006 11:55:26 -0800,
"William James" <w_a_x_man@yahoo.com> wrote:

[...]

> BEGIN{FS=OFS=","}{$6=$6; print}


Thanks! This was even simpler than I thought.

--
Seb
Sponsored Links







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

Copyright 2008 codecomments.com