Flat File column manipulation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Flat File column manipulation
# 1  
Old 03-03-2009
Flat File column manipulation

Hi All,

I have a tab delimited input file with say 4 fields (columns) as below :

0000443 1AGPR061 2006 Daiml
0002198 1B3XG0K2 1989 Chdds
0002199 1Bd64J0L 1990 Ch34s
0002275 1B3s4J0K 1989 Chadys
0002276 1B465302 2002 Dageml
0002290 1B45430K 1989 Cays

I want the 2nd column in file to be replaced with only first 2 chars and 6th char of the 2nd column values, so output should look like :

0000443 1A0 2006 Daiml
0002198 1B0 1989 Chdds
0002199 1BJ 1990 Ch34s
0002275 1BJ 1989 Chadys
0002276 1B3 2002 Dageml
0002290 1B3 1989 Cays

Can anyone help me with this Smilie?
# 2  
Old 03-03-2009
Code:
cat temp.txt  | perl -T\t -ane '$F[1] =~ s/^(.{2}).{3}(.).*$/$1$2/; print join("\t", @F) . "\n"'

The -T flag tells Perl to split the string into an array. In this case, by tabs. Then you do a regex on the second element of the array, then re-join them with tabs.
# 3  
Old 03-03-2009
Code:
gawk -F'\t' '{print $1, substr($2, 1, 2) substr($2, 6, 1), $3, $4}' file

# 4  
Old 03-03-2009
With sed:

Code:
sed 's[     \(..\)...\(.\)..[       \1\2[' infile

# 5  
Old 03-03-2009
Code:
awk '{printf "%s\t%s%s\t%s\t%s\n", $1, substr($2,1,2),substr($2,6,1), $3, $4}' file

Smilie
# 6  
Old 03-05-2009
Thanks everyone for good and quick solutions provided ...Its working Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed flat file manipulation

Hello, I have a large flat file where i need to change data in columns 131-133 based on what is in columns 172-173. I am not sure if I need to read the file line by line and make the change or if I can do this in a single statement. thank you (3 Replies)
Discussion started by: gblmin
3 Replies

2. UNIX for Advanced & Expert Users

Column manipulation of a file

In a space delimited file how would you go about removing 2 and 3 column of a file and add "word" to the first column of the file in both awk and vi? (2 Replies)
Discussion started by: cokedude
2 Replies

3. Shell Programming and Scripting

Find the position of a field/column in a flat file

Hi, Let say I have a file which has around 400 fields. SampleFile ========= PATIENTID|FACILITY|................|TIME_LAST_VISITED_BY_MD|.....|STATUS| How is it possible to find out which field is TIME_LAST_VISITED_BY_MD?fro example by seeing the above structure we can saw FACILITY... (5 Replies)
Discussion started by: machomaddy
5 Replies

4. Shell Programming and Scripting

Sort flat file by 3rd column in perl

Hello Guys I want to sort a flat file by the third column (numeric ) and store it in some other name I/P 9924873|20111114|00000000000013013|130|13|10/15/2010 12:36:22|W860944|N|00 9924873|20111114|00000000000013009|130|09|10/15/2010 12:36:22|W860944|N|00... (12 Replies)
Discussion started by: Pratik4891
12 Replies

5. Shell Programming and Scripting

Summation of column value in flat file

Hello Guys Please find my below requirement I have a flat file with column headers in first line and data The structure like below col1 col2 col3 A 1 2 B 3 4 C 5 6 Say I have to take the summation of col2 (that will depend on the... (2 Replies)
Discussion started by: Pratik4891
2 Replies

6. Shell Programming and Scripting

How to read the first column in a flat file with ~ as delimiter

I have one flat file like below id1~col~batch1 id2~col2~batch2 id3~col3~batch3 I need to read the first column one by one and I need to write one db2 query based on that column1 Like for (i=0;i<=10;i++) do insert into table column (con_id) values (select column from table where... (4 Replies)
Discussion started by: siri_886
4 Replies

7. Shell Programming and Scripting

Flat file manipulation

Hi I have flat file with 100 records ,I need to fabricate data into flat file . ex: file.txt 102345 1000 200 300 ............................... .............................. 102346 2000 300 4000 In above file the 4th row, 1 st column valu to be increment by 1 and rest of... (13 Replies)
Discussion started by: mohan705
13 Replies

8. Shell Programming and Scripting

Converting Column to Rows in a Flat file

Hi, Request To guide me in writing a shell program for the following requirement: Example:if the Input File contains the follwing data Input File Data: 80723240029,12,323,443,88,98,7,98,67,87 80723240030,12,56,6,,,3,12,56,6,7,2,3,12,56,6,7,2,3,88,98,7,98,67,87... (5 Replies)
Discussion started by: srinikal
5 Replies

9. Shell Programming and Scripting

Flat file manipulation, (this could be a tough one)

Howdy everyone, I have need of a scripting brain to help me through the following issue: *This will help if I explain what I am doing this for and then it may make sense. I need to change a flat file to prepend a 3 digit code to a certain field in the file going on the value in another scetion of... (2 Replies)
Discussion started by: mrbungle50
2 Replies

10. Shell Programming and Scripting

Look up column in a flat file

Here is on more go ! Need a shortcut for my problem ! problem is i have a look_update with fixed sequence of column that is : MANDT:SERAIL:SERSCHA:SEREX:EQTYP:BSTVP I will be getting data in a flat file having same number of column but the sequence could be different in each... (5 Replies)
Discussion started by: jambesh
5 Replies
Login or Register to Ask a Question