Format data to columns addind spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Format data to columns addind spaces
# 1  
Old 11-20-2009
Format data to columns addind spaces

Hi all,

I have a problem to format data from different database queries into one look. The input data are as follows, every line has the same number of values but a different number of characters:

Code:
adata, bdata,     cdata,    ddata
fffdata,  gdata,      hdata,    idata
jdata,    kdata,   ldata,   mdaata

What I want is a formatted output with the same number of characters in each line:

Code:
adata,   bdata, cdata, ddata
fffdata, gdata, hdata, idata
jdata,   kdata, ldata, mdaata

I´m useing Redhat-Linux and ksh.

Thanks for your support!

Last edited by old_mike; 11-20-2009 at 06:41 AM..
# 2  
Old 11-20-2009
What is the difference in your input and output ? Both looks same. Please put your input and output in tags.
# 3  
Old 11-20-2009
Hi dj,

thanks for your remark. I added the tags. As you probably see now the data of the lines start in the same column.
# 4  
Old 11-20-2009
Ok so you need formating of the output... there are varios ways
1.
Code:
echo "$VAR1  ,    $VAR2   ,    $VAR 3"

2.
Code:
awk  { printf("%s,    %s,    %s",$1,$2,$3 ) }

3. Using Perl FORMAT .. see this
Perl format Primer (1/2)

4.
Code:
echo -e "$VAR1,\t$VAR2,\t$VAR 3"

# 5  
Old 11-20-2009
Try:
Assuming the width is 10 characters.

Code:
awk '{ printf "%-10s%-10s%-10s%-10s\n",$1,$2,$3,$4 }'  filename

# 6  
Old 11-20-2009
bash
Code:
#!/bin/bash
while IFS="," read -r a b c d
do    
    printf "%-10s%-10s%-10s%-10s\n" $a,$b,$c,$d   
done <"file"

# 7  
Old 11-20-2009
@ all,

thanks a lot for your support, I checked the last post with the loop, it works perfect!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - print columns with text and spaces

Hi, I'm using awk to print columns from a tab delimited text file: awk '{print " "$2" "$3" $6"}' file The problem I have is column 6 contains text with spaces etc which means awk only prints the first word. How can I tell awk to print the whole column content as column 6? Thanks, (10 Replies)
Discussion started by: keenboy100
10 Replies

2. Shell Programming and Scripting

Triming spaces few columns

File contains 10 columns,i want trim the 2,4,10 columns using single unix command. Please suggest me how to do? Thanks (21 Replies)
Discussion started by: bmk
21 Replies

3. Shell Programming and Scripting

print columns with spaces

Hi All I've a file which is similar to the one given below column1 coulmn2 column3 column4 A B C D X Y F G H I would like to get it in this format A|B|C|D ||X|Y F||G|H Is... (4 Replies)
Discussion started by: Celvin VK
4 Replies

4. Programming

this code for addind polynomials using linked lists showed segmentation error..any help pls..

the error occurs in the function() "add" used... #include<stdio.h> #include<malloc.h> struct node { int exp; int coef; struct node * link; }; struct node * create_list(struct node *,int,int); void display(struct node *); struct node * add(struct node *,struct node *); ... (3 Replies)
Discussion started by: mscoder
3 Replies

5. Shell Programming and Scripting

conversion of spaces into CSV format file

INput file attached in thread : Column widths at 24,73,82,87,121 characters (sed 's/./,/24;s/./,/73;s/./,/81;s/./,/87;s/./,/121;s/ *, */,/g' fixedinputfile >output.csv ). The client wants instead of hard coding the column widths as they are not fixed .he has given the hint stating that ( ... (3 Replies)
Discussion started by: sreenath1037
3 Replies

6. Shell Programming and Scripting

Format row data into columns

I have a file which looks like this: /* ----------------- EDW$MOC139_R_NNA_BR_SUM_FACT2 ----------------- */ insert_job: EDW$MOC139_R_NNA_BR_SUM_FACT2 job_type: c command: /home/btchproc/load_process/batch_files/batch_nna_brn_split_sum_fact2.sh m machine: edwprod02.dsm.pwj.com #owner:... (29 Replies)
Discussion started by: Gangadhar Reddy
29 Replies

7. Shell Programming and Scripting

convert rows (with spaces) to columns

Hey all, I have a list in the format ; variable length with spaces more variable information some more variable information and I would like to transform that 'column' into rows ; variable length with spaces more variable information some more variable information Any... (8 Replies)
Discussion started by: TAPE
8 Replies

8. UNIX for Dummies Questions & Answers

converting a tabular format data to comma seperated data in KSH

Hi, Could anyone help me in changing a tabular format output to comma seperated file pls in K-sh. Its very urgent. E.g : username empid ------------------------ sri 123 to username,empid sri,123 Thanks, Hema:confused: (2 Replies)
Discussion started by: Hemamalini
2 Replies

9. Shell Programming and Scripting

Remove spaces from columns

I have a file with only one field something like this : 213.45 220.65 240.47 345.67 320.45 I want to remove all spaces in between. Is there any unix command for that ?Thanks in advance.. (2 Replies)
Discussion started by: jacks
2 Replies

10. Shell Programming and Scripting

removing spaces betweent columns

Hello Friends, Can any one help me with this issue: I would like to format a file: say if I have rows like: 4512 , SMITH , I-28984 ,, 4324 , 4343 42312 , SMITH , I-2EE8984 ,, 432E4E4 , 4343 I would like to have the output diaplayed like : 4512... (8 Replies)
Discussion started by: sbasetty
8 Replies
Login or Register to Ask a Question