Formatted Output

Hi
I have the following lines in a file

SWPRC000001NOT STATED 1344
SWPRC000001NOT STATED 1362
SWPRC000001NOT STATED 1418
SWPRC000001NOT STATED 1436
SWPRC000001NOT STATED 1437
SWPRC000001NOT STATED 1438
SWPRC000001NOT STATED 1493
SWPRC000001NOT STATED 1498

I want to print it in the format as below

format is (%s %-50s %6s %-6s)

SWPRC NOT STATED 000001 1344
SWPRC NOT STATED 000001 1362
SWPRC NOT STATED 000001 1418
SWPRC NOT STATED 000001 1436
SWPRC NOT STATED 000001 1437
SWPRC NOT STATED 000001 1438
SWPRC NOT STATED 000001 1493

If it is in sed or awk? Can you give me an example where i can implement this logic. Please note that i don;t want to read line by line of this file and do the formatting.

Let me know if you want to understand more

Regards
Dhana

If you don't want to read the file line by line, how else can you read it? Or do you mean you don't want to write a shell script which reads each line one by one? sed and awk both operate specifically on one line at a time by default. Other than that, awk sounds like the tool of choice here.

Is the input file fixed-length, or is there some particular separator? Assuming this is fixed length, columns 1-5, 6-11, 12-22, and the rest, something like:

awk '{ one=substr($0, 1, 5); two=substr($0, 6, 6);
  three=substr($0, 12, 10); rest=substr($0, 23);
  printf ("%s %-50s %6s %-6s\n", one, three, two, rest) }' file

Please use code tags for clarity when posting sample data.

Hi

What i mean to say is 'reading line by line is'

if i use cut -c 1-6 for example it will fetch the whole column of 1-6 characters and say another column 7-12.
The output of these two columns should be in the format

printf ("%s %-50s").
This way we need not read the whole file line by line.

Meanwhile i am trying to work out the logic that you have suggested.

Let me know if you still have questions.

Regards
Dhana

If the lines were much longer than an input block (512 bytes on some ancient machines; something around 2 to 8 kbytes on contemporary U*ces I'd guess) then taking care to not read the file line by line might make sense, but if the sample data is representative, reading the file line by line is probably the most efficient you can get. Anyway, the awk script above should hopefully work, perhaps with some minor modifications.

Hi
Thanks, your logic did work fast. Earlier i was using sed to cut each line and then do a printf on it and it was taking lot of time.

Now i have opened a new thread with one more additional question of how to use grep inside awk.
Let me know if you have any thoughts on that.

Regards
Dhana

era,
is there any reason for using the temp variables? could have directly substituted that in the print/printf statement

Absolutely right, there's no need to use the temporary variables, except maybe marginally for readability.