convert # delimited text file to fixed width


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers convert # delimited text file to fixed width
# 1  
Old 07-07-2011
convert # delimited text file to fixed width

Hello gurus,
I have a file containing 5 columns delimited by '#' as shown in the example below:
Code:
HRP1000-PLVAR#HRP1000-OTYPE#HRP1000-OBJID#HRP1000-BEGDA#HRP1000-ENDDA#
99991231#AU7129#000000000#1 PROCTER & GAMBLE#
99991231#TT4283#1000013883#21111 LAUNDRY#
99991231#TT4283#1000013884#21121 DISH CARE#

The first row contains the the header, which I would like to remove in the final output, which should look like below:

Code:
99991231 AU7129 000000000  1        PROCTER & GAMBLE
99991231 TT4283 1000013883 21111 LAUNDRY
99991231 TT4283 1000013884 21121 DISH CARE

Field lengths should be as follows:
Column 1 = 8 (1-8)
Column 2 = 6 (10-15)
Column 3 = 10 (17-26)
Column 4 = 5 (28-32)
Column 5 = 30 (34-63)

Any help on a code to perform this conversion will be greatly appreciated.

Thanks!

Last edited by Franklin52; 07-07-2011 at 03:15 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 07-07-2011
https://www.unix.com/unix-dummies-que...xed-width.html

use one more \t in the awk command
# 3  
Old 07-07-2011
I am not sure I understand where the \t needs to be placed in the command. Assuming the file name is current.txt, how do I need to update the code:
$ nawk -F"\#" '{ if(NR>1) {for(i=1;i<=NF;i++){printf("%s\t",$i); if(i==NF){printf("\n")}}}}' test
# 4  
Old 07-07-2011
Try to understand the code and starts to learn from basics.

So, that you can write the command by yourself.

Code:
 
$ nawk -F"\#" '{ if(NR>1) {for(i=1;i<=NF;i++){printf("%s\t\t",$i); if(i==NF){printf("\n")}}}}' current.txt

This User Gave Thanks to itkamaraj For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert a fixed width file to a delimited file

Hi - this is a generic question .... is there any utility which can convert a fixed width file format to a delimited file (any given character delimited) ? (5 Replies)
Discussion started by: i4ismail
5 Replies

2. UNIX for Dummies Questions & Answers

Need to convert a pipe delimited text file to tab delimited

Hi, I have a rquirement in unix as below . I have a text file with me seperated by | symbol and i need to generate a excel file through unix commands/script so that each value will go to each column. ex: Input Text file: 1|A|apple 2|B|bottle excel file to be generated as output as... (9 Replies)
Discussion started by: raja kakitapall
9 Replies

3. Shell Programming and Scripting

Remove new line character and add space to convert into fixed width file

I have a file with different record length. The file as to be converted into fixed length by appending spaces at the end of record. The length should be calculated based on the record with maximum length in the file. If the length is less than the max length, the spaces should be appended... (4 Replies)
Discussion started by: Amrutha24
4 Replies

4. UNIX for Dummies Questions & Answers

Delete header row and reformat from tab delimited to fixed width

Hello gurus, I have a file in a tab delimited format and a header row. I need a code to delete the header in the file, and convert the file to a fixed width format, with all the columns aligned. Below is a sample of the file:... (4 Replies)
Discussion started by: chumsky
4 Replies

5. Shell Programming and Scripting

How to split a fixed width text file into several ones based on a column value?

Hi, I have a fixed width text file without any header row. One of the columns contains a date in YYYYMMDD format. If the original file contains 3 dates, I want my shell script to split the file into 3 small files with data for each date. I am a newbie and need help doing this. (14 Replies)
Discussion started by: bhanja_trinanja
14 Replies

6. UNIX for Dummies Questions & Answers

How to convert text to columns in tab delimited text file

Hello Gurus, I have a text file containing nearly 12,000 tab delimited characters with 4000 rows. If the file size is small, excel can convert the text into coloumns. However, the file that I have is very big. Can some body help me in solving this problem? The input file example, ... (6 Replies)
Discussion started by: Unilearn
6 Replies

7. Shell Programming and Scripting

Changing one column of delimited file column to fixed width column

Hi, Iam new to unix. I have one input file . Input file : ID1~Name1~Place1 ID2~Name2~Place2 ID3~Name3~Place3 I need output such that only first column should change to fixed width column of 15 characters of length. Output File: ID1<<12 spaces>>Name1~Place1 ID2<<12... (5 Replies)
Discussion started by: manneni prakash
5 Replies

8. Shell Programming and Scripting

how to convert Fixed length file to delimited file.

I have below fixed lenth file . I have to convert this to delimitted file. File1.txtE116005/29/19930E001E000 E12201/23/19940E001E003 E10406/4/19940E001E003 I want to convert this to : E116,0,05/29/1993,0,E001,E000 E122,0,1/23/1994,0,E001,E003 E104,0,6/4/1994,0,E001,E003 I have a... (7 Replies)
Discussion started by: satyam_sat
7 Replies

9. Shell Programming and Scripting

Converting a Delimited File to Fixed width file

Hi, I have a delimited file generated by a database and i need to convert it to fixed width file using the field length of the database. Can any body suggest me how can i proceed with it? :confused: Thanks Raghavan (2 Replies)
Discussion started by: raghavan.aero
2 Replies

10. Shell Programming and Scripting

Convert delimited to fixed length

Hi, I have to change a tab delimited file to a fixed length file. For text fields I need to left justify and NULL fill to the right and for number fields I need to right justify and zero fill to the left. If there are spaces between words in a text field I need to keep them as spaces. I am using... (14 Replies)
Discussion started by: nelson553011
14 Replies
Login or Register to Ask a Question