Substitutions in a file from list in a 2nd file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Substitutions in a file from list in a 2nd file
# 1  
Old 03-11-2008
Substitutions in a file from list in a 2nd file

I made a rash statement yesterday that I could do this easier in Linux than on Windows with VBS but looking at it I'm a bit stuck.

I have a large fixed-width data file auto-generated by a database for printing delivery labels that's full of tags and flags for the printers. I need to compare the account numbers within the file to a list in a second file and add +14D to one of the fields for those accounts found in the list. So a line beginning %2403 needs a flag inserting at position 26(REPLACING 3 spaces to keep the field lengths correct) ONLY where the account number is in the list.

I'm a bit stumped by this one as to the most efficient way to read and compare the 2 files as speed is of the essence here. I'll probably use sed to do the substitutions.

I think what I'm going to look at is since the customer code is the first part of the file name, I'll have a lookup file (a bit like an ini file) with the customer code as the section heading and account codes below it - so for example:

[HLCCK]
123456
654321
789456
987654

[ABCDE]

or

[BBCMY]
3336662,567483,123456

[ZXCVB]

etc

Then all I need do is extract the cust. code from the file name then only look at those few account numbers, but still not sure how to do it. It's been a long time since I've had to do anything like this.
# 2  
Old 03-11-2008
Might be easier to give a sample of say 5 lines from each file if you can.

Using grep along with sed and or awk is probably the way to go.
# 3  
Old 03-11-2008
sample request

If you send samples, we can do this easily in a short Python or Perl script.

Basically:

Read check file into an array.

Look through big file, grabbing account # and checking if it's in the array with a hasIndex() function or similar. If found, do a simple replace function or regex.

If you want help with this, send sample files for both and I'll whip something up.

ShawnMilo
# 4  
Old 03-11-2008
The account list will be entries like:

[BBCMY]
3336662,567483,123456

The other text file looks like:

%2403+123336662 +1300050!
%2414+580714KENILWORTH !
%2403+12123456 +1300057!
%2414+580714BIRMINGHAM !

The lines with %2403+12 need to end up looking like:

%2403+1235932 +1300050+14D !
# 5  
Old 03-11-2008
Quote:
Originally Posted by fj1200
The account list will be entries like:

[BBCMY]
3336662,567483,123456

The other text file looks like:

%2403+123336662 +1300050!
%2414+580714KENILWORTH !
%2403+12123456 +1300057!
%2414+580714BIRMINGHAM !

The lines with %2403+12 need to end up looking like:

%2403+1235932 +1300050+14D !

I almost get it, but please send two examples of the exact same lines "before and after." Also, in the check file a single line of comma-separated values?
# 6  
Old 03-11-2008
Sorry

The account list will have 15 or so fields like this (Can be changed):

[TEST]
35932,43906,123456,654321,147258,963852,987654,654321,321012

[BBCMY]
3336662,567483,123456

[XYZ]
564656,84315831,15667

[ABCDE]
3336662,567483,123456

The line to be modified currently look like this:

%2403+1235932 +1304300!

So replacing the "!" with "+14D !"

Not sure now if the no. of spaces after the +14D is important as the "!" is the end of field marker but I can ammend that easily.
# 7  
Old 03-11-2008
Okay, but that still doesn't give me any before-and-after lines. In any case, I should be making you write the code yourself so I don't do your work for you.

Here's a functional but incomplete program that will do something a lot like what you want. Making all the changes are up to you.

This script opens a file and reads the first line, which contains comma-separated values for account numbers. It then loops through the second file and, when it finds one of those account numbers in a set position, it adds "ZZ" after it.

It doesn't preserve fixed-widths for different account number lengths, because I was lazy. But there should be plenty here to get you started.

Changes such as handling header records or having multiple lines with account files to check for I leave up to you. I hope this helps.

ShawnMilo

Code:
#!/usr/bin/env python

#Read a file for a list of numbers
#
#Read a second file and look for those
#numbers in a specific position
#
#If found, replace spaces after that
#position with a pre-set code

account_file = open("test.txt", 'r')

account_list = account_file.readline().rstrip("\n").split(",")

input_file = open("input.txt", 'r')

code = "ZZ"


for line in input_file:

    account_num = line[6:12].rstrip()
    if account_list.__contains__(account_num):
        start = line[0:6]
        end = line[14:19]
        print "%s%s%s%s" % (start, account_num, code, end)
    else:
        print line,

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with moving list of data to 2nd column of HTML file

Hi Team, Can you help me with writing shell script to printing the list output to 2nd column in HTML file. (2 Replies)
Discussion started by: veereshshenoy
2 Replies

2. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

3. Shell Programming and Scripting

Help i want to change the data of one file and apend it into 2nd file.

Hi All, Please help i have written an ksh script, where i am actually take count of lines in one file and want to update this count to 2nd field of a new file and apend the this into an existing file. Note the below script is in for loop -------- I am apending few records in a file... (7 Replies)
Discussion started by: iamnoone
7 Replies

4. Shell Programming and Scripting

remove values of a file one by one from 2nd file and then print the remaining values of 2nd file

Hi all, I have 2 files. One contains only 1 column and other one contains 2 columns, let say 1_col.txt and 2_col.txt respectively. Here, I will try to explain with an example. Input files : 1_col.txt 2_col.txt a a b x a c p ... (5 Replies)
Discussion started by: AshwaniSharma09
5 Replies

5. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

6. Shell Programming and Scripting

list all file whose 2nd char is digit!

how to list all files in current directory whose second character is a digit. i guess i hav to use grep command + ls for this. but dont know how to use? (6 Replies)
Discussion started by: Sgupta
6 Replies

7. Shell Programming and Scripting

Need help with awk - how to read a content of a file from every file from file list

Hi Experts. I need to list the file and the filename comes from the file ListOfFile.txt. Basicly I have a filename "ListOfFile.txt" and it contain Example of ListOfFile.txt /home/Dave/Program/Tran1.P /home/Dave/Program/Tran2.P /home/Dave/Program/Tran3.P /home/Dave/Program/Tran4.P... (7 Replies)
Discussion started by: tanit
7 Replies

8. Shell Programming and Scripting

compare two files and make 1st file same as 2nd file

I am trying to compare two file and make changes where ever its different. for example: Contents of file1 IP=192.165.89.11 NM=255.255.0.0 GW=192.165.89.1 Contents of file2 IP=192.165.89.11 NM=255.255.255.255 GW=192.165.89.1 NOTE HERE THAT NM IS DIFFERENT So i want the changes... (6 Replies)
Discussion started by: pradeepreddy
6 Replies

9. Shell Programming and Scripting

how to attach a variable from the 2nd file to the 1st file

Hi all, I am a newbie in unix shell script world. Say, I have a list of devices in file01 and then also have file02 with the list of the location. I need to telnet to each devices in file01 and set the location according to file02. Example: #cat file01 ttnpx01 ttnpx02 nncrd01 nncrd02... (5 Replies)
Discussion started by: lo tan
5 Replies

10. Shell Programming and Scripting

Reading value from one file & using it for 2nd file

In File A I have 1st col as numbers. Using these numbers, I need to find out the rows at this numbered index in other file. I know loose commands. To get the 1st col from file A : awk '{print $1}' fileA To get the row at the index, say 100 - from file B : sed '100p' fileB After... (7 Replies)
Discussion started by: videsh77
7 Replies
Login or Register to Ask a Question