Copy a column to another column in UNIX fixedwidth file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Copy a column to another column in UNIX fixedwidth file
# 1  
Old 12-20-2012
Copy a column to another column in UNIX fixedwidth file

Hi All,

I have a fixedwidth file of length 3000. Now i want to copy a column of 4 chars i.e( length 1678-1681) to column 1127 – 1171 to the same file.
Please let me know how can i achive using a single command in fixed width file.
Also source column length is 4 chars and target column length is 45 chars.
So while coying the column the value should be left adjusted to the column.

thanks,
Kiran

Last edited by jim mcnamara; 12-20-2012 at 11:16 AM.. Reason: remove font
# 2  
Old 12-20-2012
awk

Hi,

Try something like this,

Code:
 awk -v ORS='' '{l=substr($0,0,1126);r=substr($0,1172,length($0));print l;printf("%-45s",substr($0,1678,4));print r,"\n";}' input_file

Cheers,
RangaSmilie
# 3  
Old 12-20-2012
Not sure if all systems / awk versions will support that large numbers which might be well beyond LINE_MAX (check with getconf LINE_MAX -> 2048). I didn't test this one with looong lines, you may try yourself:
Code:
$ awk '{for (i=to;i<to+4;i++)$i=$(i-back)}1' to=30 back=10 FS="" OFS="" file

It replaces 4 chars at the "to" location with the chars "back" earlier... In your request, "to" would be 1127, and "back" were -551 ... you may want to play around with the signs and operators...
# 4  
Old 12-21-2012
Thanks Ranga.... your command working fine and i redirected to new file.
Code:
awk -v ORS='' '{l=substr($0,0,1126);r=substr($0,1172,length($0));print l;printf("%-45s",substr($0,1678,4));print r,"\n";}' 121128120912_C0003109_GL1025.dtr > 121128120912_C0003109_GL10251.dtr

But in the original file when i press end it is showing as 3001 but in the new generated file it is showing as 3002. Please let me know what could be the reason.

thanks,
Kiran

Last edited by Scrutinizer; 12-21-2012 at 07:17 AM.. Reason: code tags
# 5  
Old 12-21-2012
Hey,

Please use code tags for codes and data sample. You can find the code tag icon in editor. The code tag icon look like -> Image

Coming to point,

The reason behind the line mismatch may be new line character.

Use the
Code:
diff

or some other file compare tool to find out the real root cause.

Cheers,
RangaSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to copy a column of multiple files and paste into new excel file (next to column)?

I have data of an excel files as given below, file1 org1_1 1 1 2.5 100 org1_2 1 2 5.5 98 org1_3 1 3 7.2 88 file2 org2_1 1 1 2.5 100 org2_2 1 2 5.5 56 org2_3 1 3 7.2 70 I have multiple excel files as above shown. I have to copy column 1, column 4 and paste into a new excel file as... (26 Replies)
Discussion started by: dineshkumarsrk
26 Replies

2. Shell Programming and Scripting

Copy column string and put in different column

Hello Here is my input: SU3902 SU3902A NS29C (10) (00) Q1J1 0 SU3902 SU3902B VLR05 (20) (02) Q2H1 4 SU3902 SU3902C NBR22 (30) (06) Q3R5 8 SU3904 SU39047 NSV19 (11) (09) Q4k6 2 SU3904 SU39048 LB231 (12) (05) Q5k1 6 SU3904 SU39049 11VLT (13) (08) Q10C1 10 SU3904 SU3904A 25R05 (15) (06)... (3 Replies)
Discussion started by: pareshkp
3 Replies

3. Shell Programming and Scripting

copy data from different file with some same column

I have file1 have below data ABCDE2012 ABCDE2012120 -099.8 -099.4 00.4 ABCDE2012 ABCDE2012124 -104.6 -103.6 01.0 ABCDE2012 ABCDE2012128 -104.4 -103.1 01.4 file2 ABCDE2012 ABCDE2012120 15.4 ABCDE2012 ABCDE2012124 18.5 ABCDE2012 ABCDE2012128 20.1 ABCDE2012 ABCDE2012122 0.5 ... (1 Reply)
Discussion started by: pareshkp
1 Replies

4. UNIX for Dummies Questions & Answers

Copy column to another column in unix

Dear Gurus, I have a unix file 2 columns (# delimited) with the following data: ZTXX_CTBR1-ZBRAN1#ZTXX_CTBR1-ZBRAN_DESC# 01#CASH & CARRY# 02#DRUGSTORE - RX# 04#SUPERMARKET# 05#HYPERMARKET# 17#DRUGSTORE - NO RX# I would like to have a code that will do 2 things: 1. Delete the header... (16 Replies)
Discussion started by: chumsky
16 Replies

5. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

6. UNIX for Dummies Questions & Answers

How to copy one columns and print to the last column in unix?

I want to copy column no 3 to the end of column example : alter table RECOVER_USR.MPULKIXD rename to alter table RECOVER_USR.CS_ADV_PROMO rename to alter table RECOVER_USR.BCH_HISTORY_TABLE rename to alter table BILLOPS.HISHAM_DATAPLUS_FINAL rename to alter table... (8 Replies)
Discussion started by: arifahel
8 Replies

7. Shell Programming and Scripting

Copy the data column to adjacent column

Hi, i have file which contains only one record like below a|b|c|.... The no of columns is not known. The expected output is as below: a|a|b|b|c|c|... Please provide me your suggestions. OS: unix Thanks, Selva (7 Replies)
Discussion started by: bharathappriyan
7 Replies

8. 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

9. UNIX for Dummies Questions & Answers

copy column to another file

Hi guys, I want to use a quick one liner that can copy an entire column, and add it to a new file as another column. I've tried using cat, but that only appends my column to the bottom of the file. So now, my first file(file1) looks like this: 1995060101 8 1995060102 6 1995060102... (1 Reply)
Discussion started by: penlok
1 Replies

10. UNIX for Advanced & Expert Users

extracting/copy a column into a new column

Hello, Anybody out there knows how to copy a column data into a blank column using unix command? Thanks (1 Reply)
Discussion started by: folashandy
1 Replies
Login or Register to Ask a Question