Help with file manipulation script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help with file manipulation script
# 1  
Old 05-25-2012
Java Help with file manipulation script

I am a UNIX newbee . I have been doing some reading lately but really need help with an an urgent requirement.After almost two days of googling I end up with 0 luck. Hopefully a guru on here will be able to help me out. Here is my requirement:

I have 20 files in a directory with detail records in them. Each detail file has a corresponding Trailer file in the same directory . Both detail and trailer files are fixed width. I need to get the contents of each trailer file and append to its corresponding detail file. Then delete all the trailer files eg.

Detail File: "MyDetailFile.TXT_01-02-2002"
01-02-2002aaaaa23334 JanetBrown female active y D
01-02-2002aaaaa23334 MaryBondfemale active n D
01-02-2002aaaaa23334 JimanJones male active y D
01-02-2002aaaaa23334 LinusWrightmale active n D

Trailer file :"MyTrailerFile.DAT_01-02-2002"
01-01-200201-31-20021 T

Final output for each detail file in the directory should be like this:
MyDetailFile.TXT_01-02-2002
01-02-2002aaaaa23334 janejones female active y D
01-02-2002aaaaa23334 MaryBondfemale active n D
01-02-2002aaaaa23334 janejones male active y D
01-02-2002aaaaa23334 MaryBondfemale active n D
01-01-200201-31-20021 T

Anyone who can help with this I will greatly appreciate....Thanks
# 2  
Old 05-26-2012
This is an "urgent requirement" .... because?

Your homework is due?
# 3  
Old 05-26-2012
No this is not homework.I am not a student. This is for a work project. I have written code with another program to genrate those files but the program does not support merging or and appending files because of the dynamic manner it which the files aare getting generated. My colleague told me that maybe it can be done in unix or Java. Since I am not a UNIX nor JAVA person I reached out to both communities to see if someone can assist. The project has a very tight deadline and this is preety much why I said it is an urgent requirement.
# 4  
Old 05-26-2012
you can go with this if the trailercontent is the lastline in your trailerline.

Appends the detailfilename as the last line..
tail -n1 trailerfilename >> detailfilename
# 5  
Old 05-27-2012
JayAbram thanks much but it is not quite what i am looking for. With you solution I will need to change the filenames manually 20 times. Sometimes my program will generate more than 20 files and I will have to do the same as many time. I want something like a little for loop which will loop through the list of files and append the content of each trailers to its respective detail file.

Detail File: "MyDetailFile.TXT_01-02-2002"
01-02-2002aaaaa23334 JanetBrown female active y D
01-02-2002aaaaa23334 MaryBondfemale active n D
01-02-2002aaaaa23334 JimanJones male active y D
01-02-2002aaaaa23334 LinusWrightmale active n D

Trailer file :"MyTrailerFile.DAT_01-02-2002"
01-01-200201-31-20021 T

Final output for each detail file in the directory should be like this:
MyDetailFile.TXT_01-02-2002
01-02-2002aaaaa23334 janejones female active y D
01-02-2002aaaaa23334 MaryBondfemale active n D
01-02-2002aaaaa23334 janejones male active y D
01-02-2002aaaaa23334 MaryBondfemale active n D
01-02-200201-31-20024 T

Note: the first 10 characters in the detail file name always matches with the last 10 charaters of the matching trailer , this is away to identify each detail and trailer pair for the merging.
# 6  
Old 05-27-2012
Howdy,

written a simple script as below assuming MyDetail and MyTrailer files..
you can add further commands..




#!/usr/bin/sh

for file in '/home/Jay/My*'
do
if [ $file="/home/Jay/MyDetailFile.TXT_01-02-2002" ]
then
echo "found the file"
hcount=`cut -c1-10 MyDetailFile.TXT_01-02-2002 | head -n1`
printf "\n\n"
echo "headercount is $hcount"
tcount=`cut -c1-10 MyTrailer* | head -n1`
echo "trailer count is $tcount"
printf "\n\n"
if [ $hcount=$tcount ]
then
cat $file > lastf ## output is directed to another textfile
fi
else
echo "file not found"
exit 1
fi
done
This User Gave Thanks to JayAbram For This Post:
# 7  
Old 05-28-2012
Thanks againn for your time and effort Jay. I tried the above but I am still not able to get the correct output. I tried to modify it a bit but to no avail. When I tried with more than a detail/traler pair for example with 6 files i.e 3 detail files and their respective trailer file ,I got everthing in one file. If there are 6 files after I run the script I shoud have only 3 files left in the directory and each one should have the contents of the respective trailer that was deleter appended to it.

-----Once the appending and/or merging of each pair of files is done the trailer files are deleted.

---------- Post updated 05-28-12 at 11:03 AM ---------- Previous update was 05-27-12 at 04:28 PM ----------

I figure out another alternative. Thanks again.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File Manipulation - UNIX script

Hi, I have a file about 100 lines. Each line is about 2000 characters (each line is fixed length). In middle of each line is following constant value 0000040029892586 Now, I want to go through each line and increment by 1. So, line 1 will have 586 line 2 will have 587, line 3 will have... (4 Replies)
Discussion started by: jakSun8
4 Replies

2. Shell Programming and Scripting

Populating File data with custom manipulation on file names

Hi, I am confused how to proceed firther please find the problem below: Input Files: DCIA_GEOG_DATA_OCEAN.TXT DCIA_GEOG_DATA_MCRO.TXT DCIA_GEOG_DATA_CVAS.TXT DCIA_GEOG_DATA_MCR.TXT Output File Name: MMA_RFC_GEOG_NAM_DIM_LOD.txt Sample Record(DCIA_GEOG_DATA_OCEAN.TXT):(Layout same for... (4 Replies)
Discussion started by: Arun Mishra
4 Replies

3. Shell Programming and Scripting

Help with data manipulation script

Y,T,,H05,6,6,0,0 -> TH05_6 D,5,BT,B -> BT_KIOSK P,KQC222 -> KQC222 G,B,2 -> BRANI_GATE_2 fileA TPM658 Y,T,,H05,6,6,0,0 TPM110 D,5,BT,B TPM136 P,KQC222 TPM180 P,BQC913 TPM575 Y,B,,T05,14,14,0,0 IPM760 G,B,2 TPM011 I need to use second column $1,$2,$3,$4..... if first char... (6 Replies)
Discussion started by: ment0smintz
6 Replies

4. Shell Programming and Scripting

Awk to convert a text file to CSV file with some string manipulation

Hi , I have a simple text file with contents as below: 12345678900 971,76 4234560890 22345678900 5971,72 5234560990 32345678900 71,12 6234560190 the new csv-file should be like: Column1;Column2;Column3;Column4;Column5 123456;78900;971,76;423456;0890... (9 Replies)
Discussion started by: FreddyDaKing
9 Replies

5. UNIX for Dummies Questions & Answers

Script for data manipulation

Hi all! my first post here, so mods -- if this should ideally be in the scripts section, please move there. Thanks! I have data in the following format: key1:value1 key2:value2 key3:value3 A B C D key1:value4 key2:value5 key3:value6 A1 B1 key1: ... and so on I want an output... (2 Replies)
Discussion started by: gnat01
2 Replies

6. UNIX for Dummies Questions & Answers

Filtering records from 1 file based on some manipulation doen on second file

Hi, I am looking for an awk script which should help me to meet the following requirement: File1 has records in following format INF: FAILEd RECORD AB1234 INF: FAILEd RECORD PQ1145 INF: FAILEd RECORD AB3215 INF: FAILEd RECORD AB6114 ............................ (2 Replies)
Discussion started by: mintu41
2 Replies

7. Shell Programming and Scripting

Difficult problem: Complex text file manipulation in bash script.

I don't know if this is a big issue or not, but I'm having difficulties. I apoligize for the upcoming essay :o. I'm writing a script, similar to a paint program that edits images, but in the form of ANSI block characters. The program so far is working. I managed to save the image into a file,... (14 Replies)
Discussion started by: tinman47
14 Replies

8. Shell Programming and Scripting

Shell script text file manipulation.

Hello, I have mysql binary file which logs all the database queries and i to insert all queries log in to database. First i coverted binary file to text file. and start playing with it. Text file contains following queries, some samples are, SET INSERT_ID=1; INSERT INTO test... (0 Replies)
Discussion started by: mirfan
0 Replies

9. Shell Programming and Scripting

perl script file manipulation

Please let me know.. How to manipulate the data from two files..? I have two files, file1 has two columns one is variable and second is description. file2 has five columns first column is variable and last one is description..Appreciate the help I need to overwrite the description from file1... (5 Replies)
Discussion started by: ddk2oo5
5 Replies

10. UNIX for Dummies Questions & Answers

data manipulation script

I have a folder called {homedata} Within this folder there are 12 subfolders 200601.......200612 Within each subfolder there are 8 sets of files Each filename commences with A B C D E F G or H, so {filename}* can be used. I am trying to write a script which will from the top level go... (1 Reply)
Discussion started by: grinder182533
1 Replies
Login or Register to Ask a Question