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 leading/trailing white space 1

Status
Not open for further replies.

kornShellScripter

Programmer
Apr 27, 2007
24
GB
Hi,

I'm using ksh and am analysing reports which have fixed column widths (candidate for the cut command).

The values in that particular column might not be as wide as the column, so
Code:
VALUE=$(cut -c10-25 file)
might set
Code:
VALUE="GB 123 ABC G    "
and what I want is
Code:
VALUE="GB 123 ABC G"
(so I want to strip the trailing spaces)

So far, the best I've come up with is either
Code:
VALUE=$(cut -c5-10 file | sed 's/  *$//')
which is neat and VALUE ends up with precicely what I want, but it uses an external (sed)

or
Code:
VALUE=$(cut -c5-10 file) ; VALUE=${VALUE%% *}
which keeps things within the shell, but is ugly

Any ideas improving on the above?
 
Why not using awk as analysing tool instead of ksh ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi

Use [tt]sed[/tt] instead of [tt]cut[/tt] + [tt]sed[/tt] :
Code:
VALUE=$( sed 's/.\{5\}\(.\{5\}\).*/\1/;s/  *$//' file )
Of course, only if your [tt]sed[/tt] supports it.

Feherke.
 
PHV said:
Why not using awk as analysing tool instead of ksh ?

I'd do it all in perl if I had my way :)

It's a requirement to do the bulk of the script in ksh, and to make it highly readable for the non-unix BAs and the non-unix production support staff.

Also, my awk/nawk is non-existant. Is it possible to combine the cut + sed to strip leading&trailing spaces into one awk/nawk command?

I've settled on the readable, albeit cumbersome,
Code:
OUT_TEXT_COL_3_PERIOD=$(echo $j | cut -c12-14 | sed 's/^  *//' | sed 's/  *$//')
(which strips leading and trailing spaces)
 
Hi

If you want to increase the readability, you may try to split/indent/format like below. Not my style, I just saw scripts & know people who likes it.
Code:
VALUE=$(
  echo $j |
  sed '
    s/.\{5\}\(.\{5\}\).*/\1/
    s/^  *//
    s/  *$//
  '
)

Feherke.
 
kornShellScripter said:
Code:
VALUE=$(cut -c5-10 file) ; VALUE=${VALUE%% *}

That would strip off everything after the first space, leaving you with just "GB".

Try this instead:

Code:
VALUE=$(cut -c5-10 file) ; VALUE=${VALUE%%+( )}

Annihilannic.
 
Annihilannic said:
That would strip off everything after the first space, leaving you with just "GB".

Oops! Sorry, my careless copying.

I should have put two spaces in between ## and *

This works:
Code:
$ cat file
020 PPP  GB 123 ABC G    XXX
$ VALUE=$(cut -c10-25 file) ; VALUE=${VALUE%%  *} ; echo "$VALUE" | od -ta
0000000   G   B  sp   1   2   3  sp   A   B   C  sp   G  lf
0000015
$

Also, for completeness, doing it your way:
Code:
$ cat file
020 PPP  GB 123 ABC G    XXX
$ VALUE=$(cut -c10-25 file) ; VALUE=${VALUE%%+( )} ; echo "$VALUE" | od -ta
0000000   G   B  sp   1   2   3  sp   A   B   C  sp   G  lf
0000015
$
 
As for readability, [blue]this[/blue] also helps:

Code:
striptrblanks()
{
 [blue]# this function removes any trailing spaces off a variable
 # it builds a commandline like
 # var=$(var%%+( )}
 # which it then evaluates in the same shell by the eval builtin
 # use: striptrblanks variable_name[/blue]
 eval "$1=\${$1%%+( )}"
}

VARNAME=$(cut -c10-25 file) [blue]# read column X[/blue]
striptrblanks VARNAME       [blue]# strip off trailing blanks[/blue]

example (without the comments):
Code:
# striptrblanks()
{
 eval "$1=\${$1%%+( )}"
}
# var="azerty   "
# echo "=$var="
=azerty   =
# striptrblanks var
# echo "=$var="
=azerty=

HTH,

p5wizard
 
An awk way:
VALUE=$(awk '{v=substr($0,10,16);gsub(/(^ +| +$)/,"",v);print v}' file); echo "$VALUE" | od -ta

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi p5wizard,

There's various schools of thought I've encountered at my various client sites about the use of comments, and the school of thought they have here is that comments are not there to teach the reader unix commands, they're there to explain the business reasons behind code logic.

So, at this client site,
Code:
if [[ $AR = 1 ]]
would not be commented as
Code:
# Test to see if the value of AR is 1
but instead might be commented
Code:
# Test to see if the Accounts Receivable is enabled
(yes, they should have used a better variable than "AR", but this is legacy code)

So my brief/style is to make the actual code as readable as possible, compromising on efficiency if necessary (so don't get too clever) and only use comments to explain the business reasons behind the code, not explain what the code actually does. If I'm explaining what the code's doing, then I'm being too clever and complicated which doesn't help these guys if they come to change it after contract end.
 
Hi again p5wizard

Sorry, was answering a different train of thought :)

Completely understand the use of functions.

Thanks for your suggestion.

(as an aside - this legacy code I'm working on in parallel is heavily functioned, and uses lines like the "if [[ $AR = 1 ]]" which leaves me asking, "What is this doing?" It is [bold]horrendous[/bold] to debug and enhance. That is another reason I try not to function everything. I try for a "read from the top down, read from left to right" approach just for supportability reasons - some poor soul needs to support my code after I've left)
 
As long as the functions (like striptrblanks) just "function" as an enhancement to the programming language and not perform some of the program's logic, the script would still be readable top to bottom an left to right.

Also I don't see the harm in explaining seldom used variable-substitution constructs or shell builtins in that function. You could even take out the functions from the script and put em in a shell-function library, then just source that file at the beginning of your script like so.

Code:
#!/bin/ksh
###
# program logic explained
###
# install some shell string functions for use in this program
. /path/to/shell/function/library/file
###
# actual code starts here
...


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top