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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove trailing spaces

Status
Not open for further replies.

confuseddddd

Programmer
May 22, 2003
53
US
Want to be able to remove the trailing spaces from fixed length output records or even remove the trailing spaces from the fixed length input records. Just need to have the trailing spaces removed. Sometimes the record will contain all 512 bytes and sometimes will contain less.
Created a nawk script that reads an input file and setting a delimiter based on the second field in the record. Then output is created on what the second field contains and will have many output files.

Does anyone have any suggestions on how to remove trailing spaces from within a nawk script????



 

sub(/ *$/,"")

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
added the line:
sub(/ *$/,"")
before my print statment in my nawk script and still has the same size of the output as the input....

any other suggestions....
 
Are you sure the trailers are spaces and NOT tabs?

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
good question...
I am assuming its trailing spaces and the reason being is...
this nawk script is a rewrite of a huge cobol program that has the file declaration for organization set up as line sequential. With line sequential organization for the output file, the fixed length records, each containing one line of data, are stored with the trailing spaces removed.
I am trying to mirror the results of what comes out of the cobol program versus the nawk script, so in the nawk script I want the trailing spaces removed.
 
That's nice to know, but..... Are they spaces OR tabs?

with a POSIX compliant awk [/usr/xpg4/bin/awk under Solaris]:

/usr/xpg4/bin/awk -f a.awk myText.txt

#--------------- a.awk
{
sub(/[[:space:]]+$/, &quot;&quot;);
printf(&quot;rec->[%s]\n&quot;, $0);
}


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I have no idea if its spaces or tabs.
I did an od -hc on the file and the end of each row shows
\r \n, which means carriage return for \r and new line for \n, so it me its neither spaces or tabs.

Am I missing something...I see the trailing spaces after then end of each row until the processing encounters \r \n.

I am very new to unix scripting, so my questions and responses are elementary.
 
and CaKiwi's suggestion didn't work out?

what's your print statement AFTER the CaKiwi's suggested 'sub'?


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
THIS IS THE SCRIPT AND I ADDED the sub STATement before the print...did I put the sub statement in the incorrect place???


## FILE DECLARES
INFILE=&quot;data1&quot;
OUTFILE=&quot;&quot;

## BEGIN SCRIPT PROCESSING
## VARIABLE DECLARES
nawk 'BEGIN{
FS=&quot; &quot;;
PRE='`date -u +%Y%m%d`'

BPPOCNT=0
BTOTCNT=0
AAACNT=0
BBBCNT=0
CCCCNT=0
DDDCNT=0
EEECNT=0
}

{
if ( substr($1,1,4) == &quot;HEAD&quot; ) {
VAR=substr($2, 17, 3)

##Test if VAR is a valid user
##If not valid user, then set user to XXX and increment XXX count
if ( VAR != &quot;AAA&quot; && VAR != &quot;BBB&quot; && VAR != &quot;CCC&quot; && VAR != &quot;DDD&quot; && VAR != &quot;EEE&quot; )
{
VAR=&quot;XXX&quot;
XXXCNT+=1
}

## If valid user, then increment user id counts
else
if ( VAR == &quot;AAA&quot;)
{
AAACNT+=1
}
if ( VAR == &quot;BBB&quot;)
{
BBBCNT+=1
}
if ( VAR == &quot;CCC&quot;)
{
CCCCNT+=1
}
if ( VAR == &quot;DDD&quot;)
{
DDDCNT+=1
}
if ( VAR == &quot;EEE&quot;)
{
EEECNT+=1
}
sub(/ *$/,&quot;&quot;)

## Send input records to user output files
print $0 >> PRE&quot;_001.&quot;VAR

}

END {

}' $INFILE
 
Run od -hc on an output file and post a small sample.

CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
I don't quite understand the logic and the indentation, but the 'sub' looks ok.

Sorry I can't spend more time on this - maybe somebody else will be able to help you out.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Here is the od -hc output, only a sinpit though, first 2 records in the file...BBIL and BC01 are the start of each new row in the file

0000000 4242 494c 2020 2020 2020 2020 2020 2020
B B I L
0000020 2020 2020 2020 2020 2020 2032 3030 3231
2 0 0 2 1
0000040 3131 3931 3335 3831 3930 3953 4d50 3130
1 1 9 1 3 5 8 1 9 0 9 S M P 1 0
0000060 3220 2020 2020 2020 2020 2020 2020 2020
2
0000100 2020 2020 2020 2020 2020 2020 2020 2020

*
0000700 2020 2020 2020 2020 2059 2020 2020 2020
Y
0000720 2020 2020 2020 2020 2020 2020 2020 2020

*
0001000 0d0a 4243 3031 2037 3030 2020 2020 2020
\r \n B C 0 1 7 0 0
0001020 2020 2020 2020 2020 2020 2032 3333 3030
2 3 3 0 0
0001040 3331 3630 3230 3031 2020 2020 2020 2020
3 1 6 0 2 0 0 1
0001060 2020 2020 2020 2020 2050 4152 534f 4e20
P A R S O N
0001100 2020 2020 2020 2020 2020 2020 2020 2020

0001120 2020 2020 2020 204a 4153 4f4e 2020 2020
J A S O N
0001140 2020 2020 2020 2020 2020 204c 3532 3133
L 5 2 1 3
0001160 3733 3639 3220 2032 3030 3231 3031 3043
7 3 6 9 2 2 0 0 2 1 0 1 0 C
0001200 4f57 4330 3030 3030 3030 304d 2020 2020
O W C 0 0 0 0 0 0 0 0 M
0001220 2020 2020 2020 2020 2020 2020 2020 2020

*
0001420 2020 2020 2020 2020 4358 2020 2020 2020
C X
0001440 2020 2020 2020 2020 2020 2020 2020 2020

*
0002000 2020 0d0a 4250 3032 2036 3231 3736 3930
\r \n
 
I changed the sub to include a . to represent the penultimate character (in this case /r ) and it worked OK :
sub(/ *.$/,&quot;&quot;)


Dickie Bird (:)-)))
 
Yeah......that last command worked
sub(/ *.$/,&quot;&quot;)

thank you very much
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top