Global update on a file based on a table

 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support Global update on a file based on a table
# 1  
Old 01-18-2010
Error Global update on a file based on a table

Hi,

I 'd like to update the below highlighted values in a sample file based on the following table:


8283879A25918000000000005400000000000065629TTF3
8683884F40273000000000003900000000000047399TTF3
8883884FG0063000000000002600000000000031599TTF3
8283884HJ0074000000000001400000000000017015TTF3
8983884KJ0058000000000001800000000000021876TTF3
--
--
--

Table

-0 }
-1 J
-2 K
-3 L
-4 M
-5 N
-6 O
-7 P
-8 Q
-9 R

So, the result should be

8283879A2591800000000000540000000000006562RTTF3
8683884F4027300000000000390000000000004739RTTF3
8883884FG006300000000000260000000000003159RTTF3
8283884HJ007400000000000140000000000001701NTTF3
8983884KJ005800000000000180000000000002187OTTF3
--

--

Please advise.

Thanks
er_ashu

Last edited by er_ashu; 01-18-2010 at 02:48 AM..
# 2  
Old 01-18-2010
If you are using perl make a key value pair of Table of values using hash and the find replace the values with matching values from the file.
# 3  
Old 01-18-2010
Please advise if any awk or shell script can accomplish this. Any code snippets will be helpful.

Thx
# 4  
Old 01-18-2010
Something like this? Note that it'll only work if the character you're trying to replace is a single digit.
Code:
#!/usr/bin/perl -W

use strict;
use warnings;

my @lookup = qw/} J K L M N O P Q R/;

while(<>){
    s/(\d)(TTF3)$/$lookup[$1]$2/;
    print;
}

Code:
$ cat data.txt
8283879A25918000000000005400000000000065629TTF3
8683884F40273000000000003900000000000047399TTF3
8883884FG0063000000000002600000000000031599TTF3
8283884HJ0074000000000001400000000000017015TTF3
8983884KJ0058000000000001800000000000021876TTF3
$ perl -i.bak chcode.pl data.txt
$ cat data.txt
8283879A2591800000000000540000000000006562RTTF3
8683884F4027300000000000390000000000004739RTTF3
8883884FG006300000000000260000000000003159RTTF3
8283884HJ007400000000000140000000000001701NTTF3
8983884KJ005800000000000180000000000002187OTTF3

Or, as a one-liner:
Code:
perl -pi.bak -e '@lookup = qw/} J K L M N O P Q R/; s/(\d)(TTF3)$/$lookup[$1]$2/;' data.txt

# 5  
Old 01-18-2010
If you do not need the script to literally read in the table file then here is a sample script using sed rather then awk:

Code:
$ cat globalinputfile
8283879A25918000000000005400000000000065629TTF3
8683884F40273000000000003900000000000047399TTF3
8883884FG0063000000000002600000000000031599TTF3
8283884HJ0074000000000001400000000000017015TTF3
8983884KJ0058000000000001800000000000021876TTF3
$ sed -e 's/0TTF3$/}TTF3/' -e 's/1TTF3$/JTTF3/' -e 's/9TTF3$/RTTF3/' -e \
's/5TTF3$/NTTF3/' -e 's/6TTF3$/OTTF3/' globalinputfile
8283879A2591800000000000540000000000006562RTTF3
8683884F4027300000000000390000000000004739RTTF3
8883884FG006300000000000260000000000003159RTTF3
8283884HJ007400000000000140000000000001701NTTF3
8983884KJ005800000000000180000000000002187OTTF3
$

This code ensure that only ?TTF3 at of each end of the gets substituted.
# 6  
Old 01-18-2010
awk:
Code:
awk 'NR==FNR{T[$2]=$3;next}$43=T[$43]' FS="[ -]" table FS="" OFS="" sample

Code:
8283879A2591800000000000540000000000006562RTTF3
8683884F4027300000000000390000000000004739RTTF3
8883884FG006300000000000260000000000003159RTTF3
8283884HJ007400000000000140000000000001701NTTF3
8983884KJ005800000000000180000000000002187OTTF3

Code:
$ cat table
-0 }
-1 J
-2 K
-3 L
-4 M
-5 N
-6 O
-7 P
-8 Q
-9 R

# 7  
Old 01-20-2010
Code:
$ awk -F "" 'NR==FNR{a[$2]=$4;next}{$(NF-4)=a[$(NF-4)]}1' OFS="" table sample
8283879A2591800000000000540000000000006562RTTF3
8683884F4027300000000000390000000000004739RTTF3
8883884FG006300000000000260000000000003159RTTF3
8283884HJ007400000000000140000000000001701NTTF3
8983884KJ005800000000000180000000000002187OTTF3

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update value based on pattern match in another file

In the awk, thanks you @RavinderSingh13, for the help in below, hopefully it is close as I am trying to update the value in $12 of the tab-delimeted file2 with the matching value in $1 of the space delimeted file1. I have added comments for each line as well. Thank you :). awk awk '$12 ==... (10 Replies)
Discussion started by: cmccabe
10 Replies

2. Shell Programming and Scripting

Perl to update field in file based of match to another file

In the perl below I am trying to set/update the value of $14 (last field) in file2, using the matching NM_ in $12 or $9 in file2 with the NM_ in $2 of file1. The lengths of $9 and $12 can be variable but what is consistent is the start pattern will always be NM_ and the end pattern is always ;... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

awk to update file based on 5 conditions

I am trying to use awk to update the below tab-delimited file based on 5 different rules/conditions. The final output is also tab-delimited and each line in the file will meet one of the conditions. My attemp is below as well though I am not very confident in it. Thank you :). Condition 1: The... (10 Replies)
Discussion started by: cmccabe
10 Replies

4. UNIX for Dummies Questions & Answers

Select last update data based on file name

Hi All, I need to remove all files except the most update data based on date on filename Input data_AIDS_20150312.txt data_AIDS_20150311.txt data_AIDS_20150411.txt data_AIDS_20140312.txt the most updated data is data_AIDS_20150411.txt, so I'll remove other files. My expected output... (3 Replies)
Discussion started by: radius
3 Replies

5. Shell Programming and Scripting

Update the table using values from a csv file

i want to run update query for oracle which is in up.sql taking values from a.csv. I have implemented shell script to do it. extn="perl" ls -1 | while read file do echo "$file,$extn" > a.csv done up.sql contains update file_list set filename=$1 where extn=$2; The code to update is... (2 Replies)
Discussion started by: millan
2 Replies

6. UNIX for Dummies Questions & Answers

How to Update DB table from txt file using CRONJOB in Unix Shell Script

Hi Experts, can guide how we can Update a Database Table using a txt file source Using Unix Shell Scripts. What are the Cron Jobs codes can written to Update DB table. txt file contains record like data. US 09/03/2012 User DocType DocID. these above feilds in txt files need to be updated in... (4 Replies)
Discussion started by: mahesh.sap
4 Replies

7. Shell Programming and Scripting

clean passwd file based on db table (master)

The purpose of this script is to scan the /etc/passwd file one line at a time comparing the usernames to the usernames found in a database table. I will later locked every account which is not in the database table. I have export the userlist from the database in a file (/tmp/userlist). It... (1 Reply)
Discussion started by: Banks187
1 Replies

8. Shell Programming and Scripting

Update a field in a file based on condition

Hi i am new to scripting. i have a file file.dat with content as : CONTENT_STORAGE PERCENTAGE FLAG: /storage_01 64% 0 /storage_02 17% 1 I need to update the value of FLAG for a particular CONTENT_STORAGE value I have written the following code #!/bin/sh threshold=20... (1 Reply)
Discussion started by: kichu
1 Replies

9. UNIX for Advanced & Expert Users

unix script for update or insert records from a file to a oracle table

Hi, I have delimited file(|). Sample data: 1|name|50009|DS24|0|12 2|name|30009|DS24|0|13 3|name|20409|DS24|0|14 4|name|20009|DS24|0|15 5|name|10009|DS24|0|16 I want to load this data into a oracle table (update and insert) Please help me the commands and also... (1 Reply)
Discussion started by: unihp1
1 Replies
Login or Register to Ask a Question