How to format String in ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to format String in ksh
# 1  
Old 12-02-2011
How to format String in ksh

Hi,
I have 3 columns to display to target file and the display of the columns with specified position in target file.
I have tried with printf command
The Columns position like:
Column1= 0 to 30
Column2=30 to 60
Column3=60 to 90

The source data:
Code:
EMPNO,ENAME,SAL
1,11,100
2,22,200
3,33,400

Target file:
Code:
EMPNO               ENAME                SAL
1                       11                      100
2                       22                      200
3                       33                      300


Last edited by Franklin52; 12-03-2011 at 11:47 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 12-02-2011
Try
Code:
awk -F, '{printf "%30s%30s%30s\n", $1, $2, $3}' inputfile > outputfile

(or %-30s if you want them left-justified)

EDIT: A ksh solution:
Code:
while IFS=, read aa bb cc
do
   printf "%30s%30s%30s\n" $1 $2 $3
done < inputfile > outputfile


Last edited by CarloM; 12-02-2011 at 05:13 AM..
# 3  
Old 12-02-2011
Thanks...it working now.
# 4  
Old 12-02-2011
global solution
Code:
# awk -F',' '{len=30;for(i=1;i<NF;i++)printf "%-'${len}' s ",$i ;print $NF;len=len+30}' infıle

# 5  
Old 12-02-2011
Quote:
Originally Posted by ygemici
global solution
Code:
# awk -F',' '{len=30;for(i=1;i<NF;i++)printf "%-'${len}' s ",$i ;print $NF;len=len+30}' infıle

In this code ${len} has to be a shell variable - and whatever you do to the awk script len variable is irrelevant.

A purely awk version might be:
Code:
awk -F',' 'BEGIN{len=30} {for(i=1;i<=NF;i++) {printf "%-"len"s",$i} printf "\n"}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to filter string between specific string in ksh

My argument has data as below. 10.9.9.85 -rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr5.scr 127869538 -rwxr-xr-x user1 2019-10-15 17:40 /app/scripts/testingscr56scr 127869538 ....... (note all these between lines will start with hyphen '-' ) -rwxr-xr-x user1 2019-10-15 17:40... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. UNIX for Dummies Questions & Answers

Covert textfile to pdf format in ksh UNIX

Dear Friends, Is there anyway we can convert the textfile to pdf file. I have used mv a.txt a.pdf command but dont able open the file in pdf. Plz advice . (2 Replies)
Discussion started by: i150371485
2 Replies

3. Shell Programming and Scripting

ksh scripting- array format

Hi Everyone, I have a ksh script that queries a database. The query output looks like this: $queryResult = echo ${queryResult} = name1 echo ${queryResult} = age1 echo ${queryResult} = name2 echo ${queryResult} = age2 ... I need to insert those values into a new array that would... (2 Replies)
Discussion started by: practitioner
2 Replies

4. Shell Programming and Scripting

Read format problem in ksh script

I have ksh script reading file like while read -r line do echo $line sleep 1 #process_file $line done<$file_nm I want to write this line to another file after some manuplation.Put $line giving me line after changing line format(space removed).Please help me how can i read line by... (3 Replies)
Discussion started by: Mandeep
3 Replies

5. Shell Programming and Scripting

change output format using ksh

I have a script that reaches out to several systems and pulls back infomation in serveral files. I would like to take the infomation returned and reformat it so I can export it to excel. Below is current output: File1:item1:abc=1 File1:item2:efg File2:item1:ab=1 File2:item2:efg... (3 Replies)
Discussion started by: oldman2
3 Replies

6. Shell Programming and Scripting

How to format sql result as amount - ksh

I am currently returning an sql result with a number value that I want to format as an amount. The sql runs smoothly on its own, but when run inside my ksh script an error is encountered: ERROR at line 3: ORA-01481: invalid number format model My sql is -- select distinct ... (6 Replies)
Discussion started by: avillanueva
6 Replies

7. UNIX for Dummies Questions & Answers

Converting binary file to readable format in Ksh

In Unix/Ksh, when I try to look inside a file it says that the file may be a binary file and if I want to see it anyway. When i say 'yes', it shows me the content filled with unreadable symbols (looks like binary). Is there a command that I can run from the Unix prompt to convert/translate that... (3 Replies)
Discussion started by: arthurs
3 Replies

8. Shell Programming and Scripting

String format in KSH script

Hello, I am attempting to format the output of my unix ksh script. Currently looks like ... FTP TO HR : Down FTP FROM HR 4011a : Up FTP FROM HR 4019a : Down FTP FROM HR : Down Would like the status to be aligned as follows: ... (4 Replies)
Discussion started by: dvella
4 Replies

9. Shell Programming and Scripting

format decimal in ksh

hi all, in ksh, how do i format a decimal number (e.g 3381.19) to integer (e.g 3381). This number is average time response for server in millisec and i wanted to display it in a more readable format i.e in seconds without any decimals. thanks in advance. (2 Replies)
Discussion started by: cesarNZ
2 Replies

10. Shell Programming and Scripting

How to format number/string in ksh

Hi, How to format a number in ksh. For example x=RANDOM $$ I want x to be of 20 digits long, so if x = 12345 I want it to be left paded with 15 zeros. Thanks. (2 Replies)
Discussion started by: GNMIKE
2 Replies
Login or Register to Ask a Question