Click to See Complete Forum and Search --> : awk split ???
rbermejo
10-15-2002, 11:38 AM
I have a 17 character string right justified ... eg .... " 123456789" . Here we have 8 blanks in front. The blanks may vary. It may be 1, 2 even 16. Now if I want to write this string left justified ie .... "123456789 " what I need to do ?
If there are 16 blanks in front of the string I have to keep it back after left justifying the string. Do u have any idea about the "split" functionality in awk ? I think this can be done in awk by using "split"
thks.
bwkaz
10-15-2002, 12:05 PM
I don't know much awk, but C's printf() has a format specifier to right- and left-justify numbers in a field width.
You could read the number with scanf(), and then print it back with printf(), left-justified, in a field width of 17, space-padded.
Actually, I think awk may have a printf function. Or does it?
mingshun
10-15-2002, 12:42 PM
Originally posted by bwkaz
Actually, I think awk may have a printf function. Or does it?
Yes, awk does come with printf.
I agree with bwkaz as to use printf. After all, the 'f' in print is meant for formatting.
rbermejo:
Are you sure split is the correct function?
From my school's solaris man page:
split(s, a, fs)
Split the string s into array elements a[1], a[2], ... a[n], and returns n. The separation is done with the regular expression fs or with the field separator FS if fs is not given.
rbermejo
10-15-2002, 02:23 PM
Thanx for ur help.
I worked it out and used SPLIT to get it done
Example :
teststring=" 1234567" (17 char string with 10 spaces in
the front)
split(teststring,a," ")
printf("%-17s",a[1])
This will print : "1234567 " (17 char string with 10 spaces
at the end)
thks