Home > database >  How to apply awk command to a specific field only
How to apply awk command to a specific field only

Time:01-23

I have found a partial solution to my issue in: "Changing the case of a string with awk", the command posted by @AndersJohansson is:

echo 'ABCD EFGH IJKL MNOP QRST UVWX' | awk '{for (i=1; i<=NF;   i) { $i=toupper(substr($i,1,1)) tolower(substr($i,2)); } print }'

Which ends printing Abcd Efgh Ijkl Mnop Qrst Uvwx, as expected. But, in my case I have the data separated by fields, like: ABCD EFGH; IJKL MNOP; QRST UVWX and I need to apply the command to the 3r (last) field only (actually in my data there are 9 fields, but for simplicity I put just 3).

Expected output: ABCD EFGH; IJKL MNOP; Qrst Uvwx

Inside Stackoverflow there is a good number of related questions, even one of my own (How to sort inside a cell captured by awk) , I went one by one trying to adapt them to my case without success, obviously I have not enough knowledge about awk as to overcome with the solution, so I will really appreciate any help on solving this... thanks.

CodePudding user response:

Maybe something like this:

echo "ABCD EFGH; IJKL MNOP; QRST UVWX" |
awk '
    BEGIN { FS = OFS = "; "}
    {
        n = split($NF,w," ");
        for(i=1; i<=n;   i)
            s = s " " toupper(substr(w[i],1,1)) tolower(substr(w[i],2))
    }
    {$NF = substr(s,2)}
    1
'

output:

ABCD EFGH; IJKL MNOP; Qrst Uvwx
  •  Tags:  
  • Related