Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Truncate trailing characters from a variable? (POSIX sh) 2

Status
Not open for further replies.

SJSFoxPro

Technical User
Jun 19, 2003
110
US
In a POSIX shell script, I have a variable that I append to in a loop to create a list. If a condition is met in the loop the variable is appended as follows:
strVar=${strVar}\ $strOwner\,

When the loop completes, an example of the value of strVar is: BVR, HRS,

After the loop completes, I want the results echoed in a log. I want the echo to appear as follows:
*** Review log for problems in BVR, HRS. ***

The following is an example of what I have tried:
echo "\n*** Review log for problems in " \
echo $strVar | awk '{ print substr($0,1,length($0)-1) }' >> $script_log_path

This produces: *** Review log for problems in BVR, HRS

This is almost what I want. I have tried every possible way of coding this to get the ending ". ***" without success.

What I really wanted to do was to reformat strVar after the loop to simply truncate the trailing ", " (comma space) from the string. Then use the resulting strVar in my echo. I thought I could do that using some form of substr (like I did with awk).

I would be satisfied with the above method if I could figure out how to append the remaining ". ***".

I am totally open to a complete recode using any other method.

Can anyone help me out?
 
I resolved my own issue... but, I would welcome any other alternatives to see other methods!

strVar=$( echo $strVar | awk '{ print substr($0,1,length($0)-1) }' )
echo "\n*** Review log for problems in $strVar. ***
 
How about...
Code:
echo "\n*** Review log for problems in ${strVar%,}. ***\n"
That will take the comma off the end.
 
Very cool! Thanks for sharing that ;-)

I always appreciate the short, concise way of doing things.

I'm still trying to learn... If you have a minute, can you point me in the direction of any decent documentation? I can't find anything that would have led me to use the % as you did here and I'm trying to "read" and understand it (so to remember it easier).

Thanks again!
Susan ;-)
 
Hi

Both in [tt]bas[/tt] and [tt]ksh[/tt] man pages look at the Parameter Expansion section. ( But in som [tt]ksh[/tt] version's man page the section is called Parameters. )

To make you curious, here is a list of different syntaxes ( some versions may lack some of them, or have more ) :

${parameter}
${#parameter}
${#vname[*]}
${#vname[@]}
${!vname}
${!vname[subscript]}
${!prefix*}
${parameter:-word}
${parameter:=word}
${parameter:?word}
${parameter:+word}
${parameter:eek:ffset:length}
${parameter:eek:ffset}
${parameter#pattern}
${parameter##pattern}
${parameter%pattern}
${parameter%%pattern}
${parameter/pattern/string}
${parameter//pattern/string}
${parameter/#pattern/string}
${parameter/%pattern/string}

Feherke.
 
Feherke,

Thanks for the "bread crumb". I've passed by the area before and now I'm giving it a closer read.

Searches in the forum had led me down the road of using sed and awk. Although powerful, I find the parameter substitution, in this case, to be the most straight forward solution.

I appreciate your help!
Susan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top