Change line into fixed columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change line into fixed columns
# 8  
Old 10-10-2012
This will work for your given example, and shove in empty fields until total no. of fields is 6 (you may want to replace printf "," with printf "Blank,"):

Code:
awk -F, '{printf "%s", $1;
         for (i=0;i<6-NF;i++) printf ",";
         for (i=2;i<=NF;i++) printf ",%s", $i; print ""
         }' file

# 9  
Old 10-11-2012
Hello,

Thank you for the effort but I think that is doesn't work... I didn't get the prefered format.

I sent you the input an output I wanted.

Please have a look what I exaclty want. Do you have an other solution?

Please have a look on the attachment.

Thank you very much, Smilie

David
Change line into fixed columns-changejpg
# 10  
Old 10-11-2012
Example if you want to test:

Code:
alayouni,ou=SL7,ou=Operations,ou=testCARGO,ou=testGroup,ou=test,o=test.lu
apalermo,ou=Outstation,ou=Outstations,ou=Airline,ou=testGroup,ou=test,o=test.lu
dbruck,ou=Airline Safety,ou=Airline Safety,ou=Airline,ou=testGroup,ou=test,o=test.lu
pcoelhomonte,ou=SL7,ou=Operations,ou=testCARGO,ou=testGroup,ou=test,o=test.lu
jmartinsvaz,ou=SL7,ou=Operations,ou=testCARGO,ou=testGroup,ou=test,o=test.lu
hklein,ou=Atelier Mecanique,ou=Ground Equipment,ou=Direction testCARGO,ou=testCARGO,ou=testGroup,ou=test,o=test.lu
mcomazzi,ou=Communication,ou=Corporate Communication,ou=General Services,ou=testGroup,ou=test,o=test.lu
npuscasu,ou=Key Account,ou=Airport Services,ou=Airline,ou=testGroup,ou=test,o=test.lu
fladwein,ou=Assurance Qualite,ou=Assurance Qualite,ou=Airline,ou=testGroup,ou=test,o=test.lu
jcostasemedo,ou=Ramp Services,ou=Chefs d'equipes,ou=Rampistes,ou=Ramp Handling,ou=Airport Services,ou=Airport Services,ou=Airline,ou=testGroup,ou=test,o=test.lu
nekpe,ou=Ramp Services,ou=Chefs d'equipes,ou=Rampistes,ou=Ramp Handling,ou=Airport Services,ou=Airport Services,ou=Airline,ou=testGroup,ou=test,o=test.lu
rvieirasoare,ou=Ramp Services,ou=Chefs d'equipes,ou=Rampistes,ou=Ramp Handling,ou=Airport Services,ou=Airport Services,ou=Airline,ou=testGroup,ou=test,o=test.lu

Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 10-11-2012 at 10:43 AM.. Reason: code tags, please!
# 11  
Old 10-11-2012
Well, then, replace the "6-NF" in line 2 with "11-NF" and test and show the result. How come you expect a correct result on your real file if you give a sample that does not represent reality?
# 12  
Old 10-11-2012
Hello,

You're right.

It works! Thank you.

Kind regards,
David
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