PDA

View Full Version : [SOLVED] BASH: get substing AND do case conversion in one line?



justleen
August 1st, 2008, 01:15 PM
I have a CSV text file, which contains userid,first and lastname

a line looks like
DRN90002; Doe, John

What I need from the user name is the first three letters as uppercase, and i need the whole userid as lowercase.

What i have so far is this:

while read line; do
USERNAME=${line%;*}
USERNAME=$(echo $USERNAME | tr A-Z a-z)
REGIO=${line:0:3}
echo $REGIO $USERNAME
done < ${NEWFILE}


Now.. this does exactly what I want. But, I am wondering, can I somehow combine the 2 lines that create the $USERNAME that I need?

As in, can I grab the first part of the line and to a tr on it, without splitting over 2 lines (and adding another proccess since I launch tr)?

c2olen
August 1st, 2008, 01:20 PM
I have a CSV text file, which contains userid,first and lastname

a line looks like
DRN90002; Doe, John

What I need from the user name is the first three letters as uppercase, and i need the whole userid as lowercase.

What i have so far is this:

while read line; do
USERNAME=${line%;*}
USERNAME=$(echo $USERNAME | tr A-Z a-z)
REGIO=${line:0:3}
echo $REGIO $USERNAME
done < ${NEWFILE}
Now.. this does exactly what I want. But, I am wondering, can I somehow combine the 2 lines that create the $USERNAME that I need?

As in, can I grab the first part of the line and to a tr on it, without splitting over 2 lines (and adding another proccess since I launch tr)?

Would this work for you?


USERNAME=$(echo ${line%;*} | tr A-Z a-z)

ghostdog74
August 1st, 2008, 01:26 PM
awk -F";" '{print substr($0,1,3),tolower($2)}' file

justleen
August 1st, 2008, 01:43 PM
Yep, that would work.. cheers!

but, thats still requires a second proccess, awk or tr..

Is their any way you can do this with "plain substition" eg, with only bash and not starting an extra binary?

I know this is quite trivial, and it doenst really matter for my script, but Im just curious how far I can stretch the string manipulation possiblities of ${}

ghostdog74
August 1st, 2008, 02:08 PM
if you happen to know ksh, there's a typeset -u you can use, however bash (AFAIK) doesn't support -u. The portable way to convert cases, is tr (or awk)

justleen
August 1st, 2008, 02:40 PM
if you happen to know ksh, there's a typeset -u you can use, however bash (AFAIK) doesn't support -u. The portable way to convert cases, is tr (or awk)

Hm, Ill look into that..


thnx peeple!