Search code examples
stringbashawkuppercase

Change case of first word of each line


From command line, how to change to uppercase each first word of a line in a text file?

Example input:

hello   world  
tell    me who you are!  

Example output:

HELLO   world  
TELL    me who you are!  

There are no empty lines, it's ASCII, and each line starts with an alphabetic word followed by a tab.

Tools to use: anything that works on command line on macOS (bash 3.2, BSD sed, awk, tr, perl 5, python 2.7, swift 4, etc.).


Solution

  • Use awk one-liner:

    awk -F$'\t' -v OFS=$'\t' '{ $1 = toupper($1) }1' file