awktouppertolower

AWK Sentence doing looping for in the code to capitalize some fields


The code

awk '{for(i=1;i<=NF;++i){$i=toupper(substr($i,1,1))tolower(substr($i,2));}print}'

I need to capitalize the first character of every word in some fields. This loop look for all the characters in the lines and replace the first charcater in the line and do the tolower sentence for the rest of the line if its not in the first place of the field. I need some example of this loop but assigning the sentence only for some field (one or more) please make an explanation of the answer and how it does work on the all the line and field.

I haved viewed without loop doing this.

awk '{print toupper(substr($0,1,1))tolower(substr($0,2))}'

The tolower part take all the line and if I change by some field for example the number 2:

$>  echo 'Aaaa Bbbb Cccc DDDD Eeee Ffff Gggg HHHH'  | awk '{print toupper(substr($0,1,1)) tolower(substr($2,2)) }'

$>  Abbb

The $0 tells to do toupper to all the line for the first char, the following part ($0)(,1,1) tells its for only one char, and last specify the lenght of this doing (1) but the tolower part said it take from the 2nd char of the field 2 and print together (because the statements are sticked) Please tell me if im wrong in some of this and the explanation hoping understanably.

I expect to do the sentence of Toupper of Tolower specifying the field to want to do this.

Example of what it´s expected:

Do the capitalize only to the field 4 and 8 in this space separated like before showed or in other example if I have and semicolon separated field how to do this, example:

>$ echo 'Aaaa Bbbb Cccc DDDD;Eeee Ffff;Gggg HHHH'

Do the same in the 4th word of the field 1 and the 2nd in the 3rd field. (now the fields are semicolon separated)

Resulting:

Aaaa Bbbb Cccc Dddd;Eeee Ffff;Gggg Hhhh

Solution

  • Since Do the same in the 4th word of the field 1 and the 2nd in the 3rd field. and the other answers and some of the other text and examples in your question suggest you want to change selected space-separated sub fields of selected semi-colon separated fields to start with upper case and then be all lower case, here's one approach:

    $ cat tst.awk
    BEGIN {
        split(nrs,tmp,/[ .]+/)
        for (i=1; i in tmp; i+=2) {
            tgtFldNrs[++numTgts]  = tmp[i]
            tgtSubFldNrs[numTgts] = tmp[i+1]
        }
        FS = OFS = ";"
        subFs = subOfs = " "
    }
    {
        for (tgtNr=1; tgtNr<=numTgts; tgtNr++) {
            fldNr    = tgtFldNrs[tgtNr]
            subFldNr = tgtSubFldNrs[tgtNr]
    
            numSubFlds = split($fldNr,subFlds,subFs)
            subFld = subFlds[subFldNr]
            subFlds[subFldNr] = toupper(substr(subFld,1,1)) tolower(substr(subFld,2))
    
            fld = subFlds[1]
            for (subFldNr=2; subFldNr<=numSubFlds; subFldNr++) {
                fld = fld subOfs subFlds[subFldNr]
            }
            $fldNr = fld
        }
    
        print
    }
    

    Given this sample input:

    $ cat file
    Aaaa Bbbb Cccc DDDD;Eeee Ffff;Gggg HHHH
    

    telling awk to update sub-field 4 of field 1 (1.4) and sub-field 2 of field 3 (3.2) (from Do the same in the 4th word of the field 1 and the 2nd in the 3rd field in the question) it will output:

    $ awk -v nrs='1.4 3.2' -f tst.awk file
    Aaaa Bbbb Cccc Dddd;Eeee Ffff;Gggg Hhhh
    

    Hopefully that's what you're trying to do, if not then please update your question to clarify your requirements and provide more comprehensive sample input/output.