![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| cut - columns with formatted Output | dhanamurthy | Shell Programming and Scripting | 9 | 05-19-2008 10:20 AM |
| Formatted output - awk | dhanamurthy | Shell Programming and Scripting | 3 | 05-11-2008 11:25 PM |
| formatted output with commas | joeyg | Shell Programming and Scripting | 4 | 03-04-2008 03:54 PM |
| Formatted output in KSH | psynaps3 | Shell Programming and Scripting | 1 | 07-05-2006 08:03 AM |
| Validating a formatted message | BrianOsburn | Shell Programming and Scripting | 10 | 08-12-2003 05:13 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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: Code:
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
|
|
||||
|
Quote:
era, is there any reason for using the temp variables? could have directly substituted that in the print/printf statement |
|
||||
|
Formatted Output
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.
|
|
||||
|
Formatted Output
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|