Change line into fixed columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change line into fixed columns
# 1  
Old 10-10-2012
Tools Change line into fixed columns

Hello People,

I want to create a script in korn shell that splits every lines of an LDAP export into columns. After, I want to import it as csv into Excel.


Let me give an example 2 lines :


Code:
user1,ou=SL7,ou=Operations,ou=TEST,o=company

Code:
user2,ou=SL3,ou=Operations,ou=TEST,ou=company,o=company



Export:

user1 ou=SL7 ou=Operations ou=TEST o=company
user2 ou=SL3 ou=Operations ou=TEST o=company
So every line has to be splitted after "," character. Every line have to be sorted by last column. (Not ever line has the same number of columns).
If may be that user3 has a supplemantary column.
user3,ou=Admin Group ,ou=SL3,ou=Operations,ou=TEST,ou=company,o=company


Is this possible in shell or is perl the better language?


Thanks for every answer
Smilie

Kind regards,

David

Last edited by Scrutinizer; 10-10-2012 at 05:59 AM.. Reason: error; code tags
# 2  
Old 10-10-2012
I don't understand the requirements. That file is already comma separated, so EXCEL should read it like a charm. What do you mean by
Quote:
every line have to be sorted by last column.
?
# 3  
Old 10-10-2012
Hello,

Sorry it is quite difficult to explain.

Right, it is possible to import the fields in excel because is is comma seperated. The problem is that some users have not the same number of columns than others.

Example :

Code:
user1=ou=xxx,ou=yyy,o=zzz
user2=ou=www,ou=xxx,ou=yyy,o=zzz

so in this case if I'll import in excel,

xxx i in the same column than www. That's not what I want. I want to sort it right ajusted.

Code:
user1 "-   blank     " ou=xxx,ou=yyy,o=zzz
user2 ou=www  ou=xxx,ou=yyy,o=zzz

I hope that it is understandable now ;-)

Thanks,
David

Last edited by Scrutinizer; 10-10-2012 at 05:57 AM.. Reason: code tags
# 4  
Old 10-10-2012
Quote:
Originally Posted by david-1978

Code:
user1=ou=xxx,ou=yyy,o=zzz
user2=ou=www,ou=xxx,ou=yyy,o=zzz

Ok. Now tell me..
1) Is all other columns are same for other users too?(i know you have used dummy value here but still have a doubt)
2) If any column value is missing then are sure that which column it is?(here columns second). If not how we can check which column value is missing..?

Please explainSmilie
# 5  
Old 10-10-2012
Still not clear. Where does the "-" sign come from? When to output blanks, when commas? Is there a max count of columns? Is there a fixed set of column values? What is "right-adjusted"?
# 6  
Old 10-10-2012
Ok I try to explain again:

Some lines have fields that are 5 times comma seperated, other lines are 3 times comma seperated. Now I want that the last field (after the last comma) of every line is on the same column. the field before (after the second last comma) of every line is on the same column.

line1:aa,bb,cc,dd,ee
line2:cc,dd,ee
line3:bb,cc,dd,ee

line 1 output in 5 columns (column 5th position: ee, 4th position: dd, 3rd position: cc, 2nd position: bb, 1st position: aa)
line 2 output in 3 columns (column 5th position: ee, 4th position: dd, 3rd position: cc) 2nd and 1st are blank.
line 3 output in 4 columns (column 5th position: ee, 4th position: dd, 3rd position: cc, 2nd position: bb) 1st is blank

I hope it is clear now.

Kind regards,
David
# 7  
Old 10-10-2012
try something like this..

This works for your given above input only..

Code:
awk -F "[,:]" '{for(i=2;i<=(7-NF);i++){$i=" Blank "}}1' OFS="," file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Request: How to Parse dynamic SQL query to pad extra columns to match the fixed number of columns

Hello All, I have a requirement in which i will be given a sql query as input in a file with dynamic number of columns. For example some times i will get 5 columns, some times 8 columns etc up to 20 columns. So my requirement is to generate a output query which will have 20 columns all the... (7 Replies)
Discussion started by: vikas_trl
7 Replies

2. UNIX for Dummies Questions & Answers

Fixed length file extracting values in columns

How do I extract values in a few columns in a row of a fixed length file? If there are 8 columns and I need to extract values of 2nd,4th and 6 th columns, how do i do that? I used cut command, this I used only for one column. How do I do it more than one column? The below command will give... (1 Reply)
Discussion started by: princetd001
1 Replies

3. Shell Programming and Scripting

Removing duplicates in fixed width file which has multiple key columns

Hi All , I have a requirement where I need to remove duplicates from a fixed width file which has multiple key columns .Also , need to capture the duplicate records into another file . File has 8 columns. Key columns are col1 and col2. Col1 has the length of 8 col 2 has the length of 3. ... (5 Replies)
Discussion started by: saj
5 Replies

4. Shell Programming and Scripting

How to parse fixed-width columns which may include empty fields?

I am trying to selectively display several columns from a db2 query, which gives me a fixed-width output (partial output listed here): --------- -------------------------- ------------ ------ 000 0000000000198012 702 29 000 0000000000198013 ... (9 Replies)
Discussion started by: ahsh79
9 Replies

5. Shell Programming and Scripting

awk to print fixed length columns to right side

Hi, I am in a situation to print the message on a column, where the each line starting position should be same. For example code: HOSTNAME1="1.2.3.4.5.6.7" TARGET_DIR="/tmp" echo "HOSTNAME1:" "$HOSTNAME1" | awk -v var="Everyone" '{len=55-length;printf("%s%*s\n",$0,len,var)}' echo... (4 Replies)
Discussion started by: tprabhaker
4 Replies

6. UNIX for Dummies Questions & Answers

Filling the empty columns in a fixed column file

Hi, I have a file with fixed number of columns (total 58 columns) delimeted by pipe (|). Due to a bug in the application the export file does not come with fixed number of columns. The missing data columns are being replaced by blank in the output file. In one line I can have 25 columns (33... (1 Reply)
Discussion started by: yale_work
1 Replies

7. Shell Programming and Scripting

splitting a huge line of file into multiple lines with fixed number of columns

Hi, I have a huge file with a single line. But I want to break that line into lines of with each line having five columns. My file is like this: code: "hi","there","how","are","you?","It","was","great","working","with","you.","hope","to","work","you." I want it like this: code:... (1 Reply)
Discussion started by: rajsharma
1 Replies

8. Shell Programming and Scripting

Unix sort for fixed length columns and records

I was trying to use the AIX 6.1 sort command to sort fixed-length data records, sorting by specific columns only. It took some time to figure out how to get it to work, so I wanted to share the solution. The sort man page wasn't much help, because it talks about field delimeters (default space... (1 Reply)
Discussion started by: CheeseHead1
1 Replies

9. Shell Programming and Scripting

Printing Fixed Width Columns

Hi everyone, I have been working on a pretty laborious shellscript (with bash) the last couple weeks that parses my firewall policies (from a Juniper) for me and creates a nifty little columned output. It does so using awk on a line by line basis to pull out the appropriate pieces of each... (4 Replies)
Discussion started by: cixelsyd
4 Replies

10. Shell Programming and Scripting

Combining Two fixed width columns to a variable length file

Hi, I have two files. File1: File1 contains two fixed width columns ID of 15 characters length and Name is of 100 characters length. ID Name 1-43<<11 spaces>>Swapna<<94 spaces>> 1-234<<10 spaces>>Mani<<96 spaces>> 1-3456<<9 spaces>>Kapil<<95 spaces>> File2: ... (4 Replies)
Discussion started by: manneni prakash
4 Replies
Login or Register to Ask a Question