Re: change value and concatenate
- From: pk <pk@xxxxxxxxxx>
- Date: Wed, 01 Oct 2008 10:26:59 +0200
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.
.
- References:
- Re: change value and concatenate
- From: rani . randy
- Re: change value and concatenate
- Prev by Date: Re: change value and concatenate
- Next by Date: Parse a text file with a script AWK
- Previous by thread: Re: change value and concatenate
- Next by thread: Parse a text file with a script AWK
- Index(es):
Relevant Pages
|