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!

Removing trailing <blanks>

Status
Not open for further replies.

mrgulic

Technical User
Sep 18, 2001
248
US
Without writing a novel to try and explain what I am trying to do. I will simply ask what I need help on.

lib_dlt1=`echo ${lib_array[$i]} | cut -c 1-18`
#this works fine except for the white spaces.

I was trying to add another step to take out the blank space with the following

lib_dlt=${lib_dlt1#*\ }

except I can't get it right. The previous takes out everything BUT the white spaces.

Please help.
 
not sure how it relates to 'awk', but.....
Code:
#!/bin/ksh

a='123   '

echo "[${a%% *}]"

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Could be SED. Thats how much I know.
 
Ok, if the following is for deleting trailing whitespaces(spaces, tabs) from end of each line

how do you use it?

awk '{sub(/[ \t]+$/, "");print}'


I have the variable $lib_dlt that I need the trailing space removed. what is the syntax?

thanks
 
Code:
#!/bin/ksh

lib_dlt=$(echo "${lib_dlt}" | nawk '{sub(/[ \t]+$/, "");print}')

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hi

The [tt]awk[/tt] part could be shorter. At least in [tt]gawk[/tt] and [tt]mawk[/tt]. Is it working in [tt]nawk[/tt] too ?
Code:
lib_dlt=$(echo "${lib_dlt}" | awk 'sub(/[ \t]*$/, "")')
( Ok, sure the speed could suffer, but that is another story. )

Feherke.
 
[tt]lib_dlt=$(echo "${lib_dlt}" | awk 'sub(/[ \t]*$/, "")')[/tt]
[tt]*[/tt] means "zero or more". If there are zero blanks, nothing needs to be done. Here I would prefer [tt]+[/tt].
Code:
lib_dlt=$(echo "${lib_dlt}" | awk 'sub(/[ \t]+$/, "")')
 
Hi

Yes, but with [tt]+[/tt] the [tt]sub()[/tt] will return 0 on lines without trailing blanks, so that lines will not be printed out. With my stupid short form the [tt]*[/tt] is a must.

Feherke.
 
I know it's the awk forum, but I think the original ksh thought was neater.

# is for removing prefixes, you want to use % to remove suffix, and in this case %% to do it greedily.

Code:
lib_dlt="${lib_dlt1%%+( )}"

+( ) is the filename expansion syntax for "one-or-more-spaces", you can't use normal REs here.

In truth you might have just got away with this:

Code:
lib_dlt=$lib_dlt

Because without quotes around "$lib_dlt" the shell would discard the white space anyway, however if there was other white space at the beginning or in the middle of the variable it would either fail or you would lose that too.

Annihilannic.
 
Hi

Annihilannic said:
I know it's the awk forum, but I think the original ksh thought was neater.
No intention to flame, but are you sure that is original [tt]ksh[/tt] ? Is just the same syntax and behaviour on [tt]bash[/tt] too. Again, no flame, just curiosity.

And regarding your second code, does not works for me neighter on [tt]ksh[/tt] ( in fact [tt]pdksh[/tt] ) or [tt]bash[/tt].

Feherke.
 
When I said "original" I was referring to the thought, not the ksh. :)

Sorry, you're correct about the second one, I think this is what I was thinking of:

Code:
lib_dlt=`echo $lib_dlt`

Annihilannic.
 
I did the following and it worked great

#!/usr/bin/sh
lib_dlt1=`echo ${lib_array[$i]} | cut -c 1-18`

lib_dlt=$(echo "${lib_dlt1}" | awk '{sub(/[ \t]+$/,"");print}')

Thanks


BTW: Is there a good place to learn about how to use awk and sed (other then the man pages).

Thanks again.
 
Why not simply this ?
eval lib_dlt1=`echo ${lib_array[$i]} | cut -c 1-18`

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Because it cuts out the white spaces and everything else.......lol

Thanks anyhow.
 
TheAccessHack said:
BTW: Is there a good place to learn about how to use awk and sed (other then the man pages).

The O'Reilly book on the subject is well respected I believe.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top