The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-12-2008
dhanamurthy dhanamurthy is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 105
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
  #2 (permalink)  
Old 05-12-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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
Please use code tags for clarity when posting sample data.
  #3 (permalink)  
Old 05-13-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Quote:
Originally Posted by era View Post
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
Please use code tags for clarity when posting sample data.

era,
is there any reason for using the temp variables? could have directly substituted that in the print/printf statement
  #4 (permalink)  
Old 05-13-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Absolutely right, there's no need to use the temporary variables, except maybe marginally for readability.
  #5 (permalink)  
Old 05-12-2008
dhanamurthy dhanamurthy is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 105
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
  #6 (permalink)  
Old 05-12-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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.
  #7 (permalink)  
Old 05-12-2008
dhanamurthy dhanamurthy is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 105
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:04 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0