Reputation: 153
I am trying to manipulate a text stream where the second column needs to be lower case along with the fourth..(this works) However, in my case, the fifth may have a word or words.. in that case, I want to capitalize the first letter and lower case the rest of the word (or words)
cat payload/consolidated.csv | awk -F'","|^"|"$' '{print tolower($2),"~",tolower($4),"~",toupper(substr($5,1,1)) tolower(substr($5,2))}'
but this is failing to capitalize subsequent words.. it works only for the first word in that fifth column match
ideas?
Likewise and as a follow-on, how would I put a conditional if for that fifth column such that if it matched USA (example), that I would keep it USA. and not be Usa
Appreciated.
Sample Data input
"IGNORE","Why","IGNORE","Where","FirstName LastName Country"
Desired output
why~where~Firstname Lastname Country
(and then I want to be able to conditionally modify Country such that if it is actually USA, then I ignore the tolower() )
Upvotes: 0
Views: 46
Reputation: 37404
Here is one for GNU awk:
$ awk 'BEGIN {
FPAT = "([^,]*)|(\"[^\"]+\")" # using FPAT to separate fields
OFS="~" # output field separator
}
{
for(i=2;i<=NF;i++) # remove quotes from fields
gsub(/^"|"$/,"",$i)
b="" # buffer the case play
while(match($5,/ *[A-Za-z]+ */)) { # collect the "words" and upper first char, lower rest
b=b toupper(substr($5,RSTART,1)) tolower(substr($5,RSTART+1,RLENGTH-1)) # sorry 2Pac...
$5=substr($5,RSTART+RLENGTH)
}
print tolower($2),tolower($4),b
}' file
Output:
why~where~Firstname Lastname Country
Waiting for that conditional casing example.
Upvotes: 1
Reputation: 133508
Since I don't have FPAT
in my system so going with usual FS
field separator setting here.
awk '
BEGIN{
FS="[ |,|\"]"
s1=" "
OFS="~"
}
{
print tolower($5),tolower($11),\
toupper(substr($14,1,1)) \
tolower(substr($14,2)) s1 \
toupper(substr($15,1,1)) \
tolower(substr($15,2)) s1 \
toupper(substr($16,1,1)) \
tolower(substr($16,2))
}' Input_file
In case of you want to know field number and their respective field values for lines then you could run following command it will be eaiser to understand above more.
awk 'BEGIN{FS="[ |,|\"]";OFS="~"} {for(i=1;i<=NF;i++){print i,$i}}' Input_file
Upvotes: 1