Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting string to lower case part of a string in bash shell scripting

I have a file.txt and each line of the file is like:

ABLED   EY B AH L D
ABLER   EY B AH L ER

I want to have the second part of each line: EY B AH L D or EY B AH L ER, for example, in lower case, keeping the rest upper case. How can I do it?

Thank you very much in advance.

like image 378
user1835630 Avatar asked Feb 19 '23 03:02

user1835630


1 Answers

while read first second; do
    second=$(echo "$second" | tr [:upper:] [:lower:])
    printf '%s\t%s\n' "$first" "$second"
done < file.txt

Output:

ABLED   ey b ah l d
ABLER   ey b ah l er

Two other ways to do it in KornShell, pdksh, or Bash without calling tr

set the "lowercase" flag on the variable (KornShell and compatible shells only):

typeset -l second
while read first second; do
    printf '%s\t%s\n' "$first" "$second"
done < file.txt

use Bash's case-modification parameter expansion modifier (Bash only!):

while read first second; do
    printf '%s\t%s\n' "$first" "${second,,}"
done < file.txt
like image 104
3 revs, 3 users 50% Avatar answered May 07 '23 01:05

3 revs, 3 users 50%