|
|
filling left side of string
I am accepting a value entered from batch file using set /p command. I
need to add spaces to the left of the number entered inside an awk
script
nawk -v number=%number% -f test.awk test.dat
What function used to fill the left side of the number with spaces to
complete 7 characters as fixed length of any nymber entered.
Thanks
|
|
0
|
|
|
|
Reply
|
happytoday
|
3/2/2008 4:39:46 PM |
|
On Sun, 02 Mar 2008 08:39:46 -0800, happytoday wrote:
> I am accepting a value entered from batch file using set /p command. I
> need to add spaces to the left of the number entered inside an awk script
>
> nawk -v number=%number% -f test.awk test.dat
>
>
> What function used to fill the left side of the number with spaces to
> complete 7 characters as fixed length of any nymber entered. Thanks
You might like gawk better:
<http://gnuwin32.sourceforge.net/packages/gawk.htm>
The Windows version has a .hlp help file with lots of information.
You need printf() or sprintf(), depending on what you do with the padded
number.
printf "%7i", number
Note that you have to include a \n explicitly at the end of each line.
--
T.E.D. (tdavis@mst.edu) MST (Missouri University of Science and Technology)
used to be UMR (University of Missouri - Rolla).
|
|
0
|
|
|
|
Reply
|
Ted
|
3/2/2008 5:22:24 PM
|
|
On Mar 2, 7:22=A0pm, Ted Davis <tda...@umr.edu> wrote:
> On Sun, 02 Mar 2008 08:39:46 -0800, happytoday wrote:
> > I am accepting a value entered from batch file using set /p command. I
> > need to add spaces to the left of the number entered inside an awk scrip=
t
>
> > nawk -v number=3D%number% -f test.awk test.dat
>
> > What function used to fill the left side of the number with spaces to
> > complete 7 characters as fixed length of any nymber entered. Thanks
>
> You might like gawk better:
> <http://gnuwin32.sourceforge.net/packages/gawk.htm>
> The Windows version has a .hlp help file with lots of information.
>
> You need printf() or sprintf(), depending on what you do with the padded
> number.
>
> =A0 printf "%7i", number
>
> Note that you have to include a \n explicitly at the end of each line.
>
> --
>
> T.E.D. (tda...@mst.edu) MST (Missouri University of Science and Technology=
)
> used to be UMR (University of Missouri - Rolla).
But I need to use the variable in comparing function not in printing.
So with the set /p I have only numbers without left leading spaces .
filenumber variable in file has left spaces. So I need to fill the
left side of the number with spaces till I reach 7 characters legnth.
Thanks
nawk -v number=3D%number% -f test.awk test.dat
filenumber=3Dsubstr($0,1,7)
if (filenumber=3Dnumber)
=2E...
Thanks
|
|
0
|
|
|
|
Reply
|
happytoday
|
3/3/2008 6:57:35 AM
|
|
happytoday schreef:
> On Mar 2, 7:22 pm, Ted Davis <tda...@umr.edu> wrote:
>> On Sun, 02 Mar 2008 08:39:46 -0800, happytoday wrote:
>>> I am accepting a value entered from batch file using set /p command. I
>>> need to add spaces to the left of the number entered inside an awk script
>>> nawk -v number=%number% -f test.awk test.dat
>>> What function used to fill the left side of the number with spaces to
>>> complete 7 characters as fixed length of any nymber entered. Thanks
>> You might like gawk better:
>> <http://gnuwin32.sourceforge.net/packages/gawk.htm>
>> The Windows version has a .hlp help file with lots of information.
>>
>> You need printf() or sprintf(), depending on what you do with the padded
>> number.
>>
>> printf "%7i", number
>>
>> Note that you have to include a \n explicitly at the end of each line.
>>
>> --
>>
>> T.E.D. (tda...@mst.edu) MST (Missouri University of Science and Technology)
>> used to be UMR (University of Missouri - Rolla).
>
> But I need to use the variable in comparing function not in printing.
> So with the set /p I have only numbers without left leading spaces .
> filenumber variable in file has left spaces. So I need to fill the
> left side of the number with spaces till I reach 7 characters legnth.
>
> Thanks
>
> nawk -v number=%number% -f test.awk test.dat
>
> filenumber=substr($0,1,7)
> if (filenumber=number)
> ....
>
>
> Thanks
you should try something like:
echo -e '\n\n' | awk '{ x=sprintf("%7i", NR); print x; }'
and try to understand this, than you'll get the idea if what Ted wrote
(at least i hope you get it ;-)
another aproach might be to not compare ' 1' with ' x'
but do a numeric comparison
filenumber=substr($0,1,7)
if (0+filenumber==1*number)
--
Luuk
|
|
0
|
|
|
|
Reply
|
Luuk
|
3/3/2008 12:30:51 PM
|
|
On Sun, 02 Mar 2008 22:57:35 -0800, happytoday wrote:
> But I need to use the variable in comparing function not in printing. So
> with the set /p I have only numbers without left leading spaces .
> filenumber variable in file has left spaces. So I need to fill the left
> side of the number with spaces till I reach 7 characters legnth.
RTFM
if( string == sprintf "%7i", number ) do something
--
T.E.D. (tdavis@mst.edu)
|
|
0
|
|
|
|
Reply
|
Ted
|
3/3/2008 1:44:39 PM
|
|
|
4 Replies
270 Views
(page loaded in 0.089 seconds)
|
|
|
|
|
|
|
|
|