perl script file manipulation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl script file manipulation
# 1  
Old 08-22-2008
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 to file2 as per the variables..

e.g. file1

var1 <correct description>
var2 <correct description>
abc1 <correct description>
bcd3 <correct description>

.....

file2

var1 cl1 cl2 cl3 <wrong description1>
var1 b1 b3 b4 <wrong description3>
abc1 1 2 3 4 <wrong description1>
abc2 1 2 3 4 <wrong description7>
unknon m n po e <wrong description7>
unknon sf dvd dvd <wrong description7>
bcd3 p q r t <wrong description7
bcd3 n mb c <wrong description1>
......

result would be in file2


var1 cl1 cl2 cl3
var1 b1 b3 b4 <correct description>
abc1 1 2 3 4
abc2 1 2 3 4 <correct description>
unknon m n po e
unknon sf dvd dvd <wrong description7>
bcd3 p q r t <wrong description7
bcd3 n mb c <wrong description1>
# 2  
Old 08-22-2008
Code:
Tsunami file_manipulation # cat file1 
var1 <correct description>
var2 <correct description>
abc1 <correct description>
bcd3 <correct description>
Tsunami file_manipulation # cat file2 
var1 cl1 cl2 cl3 <wrong description1>
var1 b1 b3 b4 <wrong description3>
abc1 1 2 3 <wrong description1>
abc2 1 2 3 <wrong description7>
unknon m n po <wrong description7>
unknon sf dvd dvd <wrong description7>
bcd3 p q r <wrong description7
bcd3 n mb c <wrong description1>
Tsunami file_manipulation # perl -ane 'if (/^((.*?)\s(?:.*?\s){3})(.*?)$/) { print "$1".(defined $h{$2}?$h{$2}:$3)."\n"; } else { /^(.*?)\s(.*?)$/; $h{$1} = $2; }' file1 file2 
var1 cl1 cl2 cl3 <correct description>
var1 b1 b3 b4 <correct description>
abc1 1 2 3 <correct description>
abc2 1 2 3 <wrong description7>
unknon m n po <wrong description7>
unknon sf dvd dvd <wrong description7>
bcd3 p q r <correct description>
bcd3 n mb c <correct description>
Tsunami file_manipulation #

My solution stores every description of file1 in memory so if you have a really big file1 or file2 a line-by-line solution would fit best. Anyway, hope it helps.
PS: if an entry in file2 is not found in file1 I just keep file2's description "wrong description".
# 3  
Old 08-25-2008
No changes at all its just cat the two files.. i need to keep file2 as it is except description (that should be same as file1). Can we add/overwrite the description for second line variable only in file2 ( as file 2 has two same variables in first column).

Nyway Thanks as ur only one at least reply on this....
# 4  
Old 08-25-2008
The code I gave you is not changing anything but is doing what you asked: replace description on file2 from entries found in file1. If you wish the code to replace file2, just:
Code:
perl -ane 'if (/^((.*?)\s(?:.*?\s){3})(.*?)$/) { print "$1".(defined $h{$2}?$h{$2}:$3)."\n"; } else { /^(.*?)\s(.*?)$/; $h{$1} = $2; }' file1 file2 > file2.new && mv file2.new file2

# 5  
Old 08-26-2008
It is not working.. Is it coz of tabs between the words instead of single space..
# 6  
Old 08-26-2008
Nop. The problem is that probably you have spaces after the tabs! Something like:
entry1<tab><space>entry2<space><tab>...

Code:
perl -ane 'if (/^((.*?)\s+(?:.*?\s+){3})(.*?)$/) { print "$1".(defined $h{$2}?$h{$2}:$3)."\n"; } else { /^(.*?)\s+(.*?)$/; $h{$1} = $2; }' file1 file2 > file2.new && mv file2.new file2

Try this now.

PS: if you used the proper code tags I would have been able to see the tabs.
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. UNIX for Dummies Questions & Answers

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... (6 Replies)
Discussion started by: jeyd
6 Replies

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

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

5. Shell Programming and Scripting

Perl Text Manipulation

I'm in need of help for a project that I'm working on. I believe Perl would be the best way of handling the string manipulation, however, I've barely used perl, and I'm used to BASH scripting. Another note is, this project is in a Windows environment, so I can use Perl, but I do not have shell... (1 Reply)
Discussion started by: drewrockshard
1 Replies

6. Shell Programming and Scripting

Perl Text manipulation

Hello All, I have been working on a great script to remotely gather server info and store it in a .txt that can be imported to .xls I have been reading the hostnames that are in the /.shh/known_hosts file so I don't have to mess with passing a password - via ssh (not easy to do , by the... (1 Reply)
Discussion started by: dfezz1
1 Replies

7. Shell Programming and Scripting

Array manipulation in perl

hi all, i am trying to append the output of a find command (for different paths)in an array as below... my $res_array; $i=0; $dir="/orn/ops/regs"; foreach $block("am","xb"){ $bdir="$dir/$block"; $res_array=`find $bdir ! -user mainuser -printf \"\%u \%h\\n\"`; $i++; } i... (6 Replies)
Discussion started by: saapa
6 Replies

8. Shell Programming and Scripting

Perl to perfrom string manipulation

Hi, Please advise me how to perform checking in perl on the following input below. The output criteria will be 1. field 1 will begin as 913354744 for each as example 2. field 4 must be unique. 3. field 7 is showing dd/mm/yyyy, but it require checking if unixtime should covert to... (5 Replies)
Discussion started by: rauphelhunter
5 Replies

9. Shell Programming and Scripting

Perl, string manipulation

Hi all, Supposing the following string: XXXXXXXXXXNEAXXXXXX How can I check if this string has the substring NEA? Best regards Chris (3 Replies)
Discussion started by: chriss_58
3 Replies

10. Shell Programming and Scripting

Data manipulation in perl

Hello guys.. I have the following question. lets have that i have the following variable: $field=werfiurd383nd93bc93 c93 d93 d9e3 ddd or array=werfiurd383nd93bc93 c93 d93 d9e3 ddd what i would like to do is to store the first 4 characters of gthe aboce variable in variable... (1 Reply)
Discussion started by: chriss_58
1 Replies
Login or Register to Ask a Question