Formatted Output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Formatted Output
# 1  
Old 05-12-2008
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  
Old 05-12-2008
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  
Old 05-12-2008
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
# 4  
Old 05-12-2008
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.
# 5  
Old 05-12-2008
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
# 6  
Old 05-13-2008
Quote:
Originally Posted by era
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
# 7  
Old 05-13-2008
Absolutely right, there's no need to use the temporary variables, except maybe marginally for readability.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script for formatted output

06/26/2017 23:40:40 CAUAJM_I_10082 06/26/2017 23:40:40 CAUAJM_I_40245 EVENT: CHANGE_STATUS STATUS: STARTING JOB: IOALPPRXXBD_ALPGLGENFAALL MACHINE: aspsun14 06/26/2017 23:40:42 CAUAJM_I_40245 EVENT: CHANGE_STATUS STATUS: RUNNING JOB:... (10 Replies)
Discussion started by: Sandeep Behera
10 Replies

2. Programming

Formatted output in PERL

Hi Techies, I'm a newbie to PERL, Please help me with following problem. I have an input text file like below cat Input.txt 418673132,P 492538858,P 384535478,P 521522357,I 529435679,I 183617024,P 184414408,I 735510689,P 736238343,I 411642045,I 412690979,I 104232783,I (2 Replies)
Discussion started by: mahi_mayu069
2 Replies

3. HP-UX

Formatted TOP command output in file

Hi All, I want generate HP-UX overall system performance report. I tried executing top command and write that out put to file. but am not able to view the report in proper format. I can see report like below in file but i can see properly in terminal. Please suggest how can i get... (2 Replies)
Discussion started by: lravip123
2 Replies

4. UNIX for Dummies Questions & Answers

Request for Formatted Output

Can you please tell me how to just get only the output of dealers I & V information along with their subtotals in the next line of the file and create a new file, The dealer position along with corresponding totals may change everyday to any position above or below in the file, please help Thanks (2 Replies)
Discussion started by: Ariean
2 Replies

5. Shell Programming and Scripting

output - tab formatted - awk

Dear All, Good Day. I would like to hear your suggestions for the following problem: I have a file with 5 columns with some numbers in 16 lines as shown below. Input file: Col 1 Col 2 Col 3 Col 4 Col 5 12 220 2 121 20 234 30 22 9... (3 Replies)
Discussion started by: Fredrick
3 Replies

6. Shell Programming and Scripting

Formatted output of shell script

Hello, I am working on one script which is giving output as a pipe "|" separated abcd | efgh | 20090745 abcdefgh | efg | 20090622 Can any one please help me i want it to be formatted as pipe will be aligned, or the output looks like a table. (2 Replies)
Discussion started by: vikash_k
2 Replies

7. Shell Programming and Scripting

cut - columns with formatted Output

Hi I have the input file as below ***TEST10067 00567GROSZ 099 00567CTCTSDS90 ***TEST20081 08233GROZWEWE 00782GWERW899 ***TEST30088 08233GROZWEWE 00782GWERW899 I am finding the lines starting with *** and outputing as below TEST10067 TEST20081 TEST30088 I need a space between TEST1... (9 Replies)
Discussion started by: dhanamurthy
9 Replies

8. Shell Programming and Scripting

Formatted output - awk

Hi I have the following records in a file SABN YOURTUBE 000514 7256 SACN XYOUDSDF 000514 7356 SADN KEHLHRSER 000514 7656 SAEN YOURTUBE 000514 7156 SAFN YOURTUBE 000514 7056 I need to put this in the format like this printf '%s %-50s %6s %-6s\n' I am not going to read individual... (3 Replies)
Discussion started by: dhanamurthy
3 Replies

9. Shell Programming and Scripting

formatted output with commas

var=12345 echo $var >>>12345 printf "%8.1f \n" $var >>> 12345.0 How to get this as 12,345? I suppose I could break into sections by dividing by 1000 or 1000000. But, is the a trick to this? (4 Replies)
Discussion started by: joeyg
4 Replies

10. Shell Programming and Scripting

Formatted output in KSH

Hi, Is there some way to get formatted output in ksh? Something like a properly alligned tabular format. I tried adding '\t' to echo statements, but it doesn't come properly alligned 'hello' A simple Hello 'helloworld' A helloworld statement I need the second coloumn to... (1 Reply)
Discussion started by: psynaps3
1 Replies
Login or Register to Ask a Question

Featured Tech Videos