Re: change value and concatenate



On Wednesday 1 October 2008 04:11, rani.randy@xxxxxxxxx wrote:

Okay, I think a gurl has to be a little bit more clear about her
background.

Well, sorry for assuming you were a boy :-|

-- sample file --
"AXXB ","M ","00000708","1354"," ","LOCATION 1","Div-VoiceMail"
"AXXC ","M ","00000708","1354"," ","LOCATION 2","Div-VoiceMail"


--script --
#!/usr/bin/gawk -f

BEGIN {
FS="\",\"";
OFS=",";

}

{
print $1","$2","$3" "$4","$5","$6","$7
}


-- output ---
"AXXB ,M ,00000708 1354, ,LOCATION 1,Div-VoiceMail"
"AXXC ,M ,00000708 1354, ,LOCATION 2,Div-VoiceMail"


I want to now do some changes so my output would be:

AXXB ,M ,2008-07-08 13:54, ,LOCATION 1,Div-VoiceMail
AXXC ,M ,2008-07-08 13:54, ,LOCATION 2,Div-VoiceMail

In this case, your CSV format is tractable enough, since your fields are ALL
quoted and separated in a regular way. To begin with, you can do

FS='^"|","|"$'

so you have your fields in $2...$(NF-1). Fields $4 and $5 must be modified
and unified. Thus:

awk -v FS='^"|","|"$' -v OFS=',' '{
$4="2008-"substr($4,5,2)"-"substr($4,7,2);
$4=$4" "substr($5,1,2)":"substr($5,3,2);
for(i=1;i<NF-2;i++){
if(i<4)$i=$(i+1); else $i=$(i+2)
}
NF=NF-3;print
}' file

The purpose of the for loop is to shift down all the fields so that, at the
end, they are all in positions 1...NF-2. After the 4th field, positions are
shifted down by two sonce $4 and $5 have been unified. NF=NF-3 completes
the job, telling awk that the new line has NF-3 fields (compared to the
original, leading and trailing empty fields and the former $5 are missing).
The line is then printd using "," as OFS.

With your sample input, that produces

AXXB ,M ,2008-07-08 13:54, ,LOCATION 1,Div-VoiceMail
AXXC ,M ,2008-07-08 13:54, ,LOCATION 2,Div-VoiceMail

Hope that helps.

.



Relevant Pages

  • Re: loops in perl automated ftp
    ... do the loop 5 times unless one of the putcalls ... You probably want to avoid repeating the loop all over your code, ... try N times to run CODE, and do ERR every time it fails ... my $code = shift @_; ...
    (comp.lang.perl.misc)
  • RE: listing prime numbers in a range (Beginning Perl exercise)
    ... > stuart>Hi David, ... > stuart>I figure that the exercise wouldn't be in the ... and move the 'print' to after the loop. ... When 'shift' has no argument, it shifts '@_' which is parameter ...
    (perl.beginners)
  • 68k chip, Diab Compiler and odd behavior
    ... The first 16 times through the loop ... the debugger, line 12 is executed and the debugger skips 13 and 14, ... ulword tmpfaultvalue; ... ulword shift; ...
    (comp.arch.embedded)
  • Re: curious behavior with shift (perl 5.8)?
    ... my $c = shift @; print $b,$c;} 615243 ... If the variable is preceded with the keyword "my", then it is lexically scoped, and is therefore visible only within the loop. ... If VAR is omitted, $_ is set to each value. ... If any element of LIST is an lvalue, you can modify it by modifying VAR ...
    (comp.lang.perl.misc)
  • Re: MS SQL geek wants to jump ship, plz help on first Perl script
    ... Next step is the MEAT inside the loop. ... Then you don't understand shift(). ... automatically works on the @_ array (ie, ... # a lot of memory, coz it reads the whole arrary into memory first, ...
    (comp.lang.perl.misc)