How to manupulating data using PERL?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to manupulating data using PERL?
# 1  
Old 06-28-2010
How to manupulating data using PERL?

Hi All,

I want to manipulate the date using PERL.
anyone can help me to resolve this issue.
Code:
@command = `cat FILE.txt |cut -d "|" -f1 >OUTPUT.txt`;

the above command gives the out put is:
Code:
346566
676875
768674
567567
344556

Now i want to calculate (-2 ) in in each data of output file ( Eg: 346566-2=346564) and save the final output to another file.

Kindly help me to perform this task.

Last edited by Franklin52; 06-28-2010 at 09:37 AM.. Reason: Please use code tags
# 2  
Old 06-28-2010
Code:
perl -e '@command = `cat FILE.txt |cut -d "|" -f1`; print map{$_-2 ."\n"}@command' > OUTPUT.txt

# 3  
Old 06-28-2010
thanks for yout prompt reply.

however i'm getting the below error

syntax error at t.pl line 1, near "perl -e "
Execution of t.pl aborted due to compilation errors.

kindly suggest me?
# 4  
Old 06-28-2010
That is because what I provided is not script, but command that you can use at command line. Here is code that you could use in script:
Code:
open FILE, ">OUTPUT.txt"; 
@command = `cat FILE.txt |cut -d "|" -f1`; 
print FILE map{$_-2 ."\n"}@command;

# 5  
Old 06-28-2010
Thanks a lot burtus11 Smilie
# 6  
Old 06-28-2010
Or, you could skip all those costly calls to external programs, especially that Useless Use Of Cat, and do it in Perl directly.
1-liner:
Code:
perl -nle 'split /\|/; print $_[0]-2;' FILE.txt > OUTPUT.txt

Part of a script:
Code:
open my $in,  '<bla.txt';
open my $out, '>OUTPUT.txt';
print $out map { chomp; split /\|/; $_[0] - 2, "\n" } <$in>;
close $out;
close $in;

# 7  
Old 06-28-2010
Thank you all.
I got the answer searching cut command,chomp,map.

Last edited by cola; 06-28-2010 at 11:16 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies

2. Shell Programming and Scripting

Perl :: reading values from Data Dumper reference in Perl

Hi all, I have written a perl code and stored the data into Data structure using Data::Dumper module. But not sure how to retreive the data from the Data::Dumper. Eg. Based on the key value( Here CRYPTO-6-IKMP_MODE_FAILURE I should be able to access the internal hash elements(keys) ... (1 Reply)
Discussion started by: scriptscript
1 Replies

3. Shell Programming and Scripting

send data with Perl

Hello! I want to sent data (pictures: pic.jpg or textfiles) to a server. The pictures should be stored in \my_pictures\beautiful_images. How can I do it with Perl? I think, that I should start with a socket?? Do you have a tutorial or a site, where I can read about it? I use the IDE Padre /... (1 Reply)
Discussion started by: la_dy82
1 Replies

4. Shell Programming and Scripting

Manupulating Records in a fixed width file

I am trying to determine what would be a fast and simple way to manipulate data that comes in a fixed width format. This data has 6 segments within a record. Each record needs to written out with a header and the 6 segments. Based on the value in column #6 the fields will be defined accordingly.... (4 Replies)
Discussion started by: Muga801
4 Replies

5. Shell Programming and Scripting

perl data structure

Hi All, I want to create a data structure like this $VAR1 = { 'testsuite' => { 'DHCP' => { 'failures' => '0', 'errors' => '0', 'time' =>... (3 Replies)
Discussion started by: Damon_Qu
3 Replies

6. Shell Programming and Scripting

Reformat Data (Perl)

I am new to Perl. I need to reformat a data file as the last part of a script I am working on. I am stuck on this. Here is the current format: CUSTOMER Filename 09/04/07-08:49 CUSTOMER Filename 09/04/07-08:52 CUSTOMER Filename 09/04/07-08:52 CUSTOMER2 Filename 09/04/07-08:49 CUSTOMER2... (3 Replies)
Discussion started by: flood
3 Replies

7. Shell Programming and Scripting

Need help in manupulating string

Hi, I am new to Unix.I got an assignment as follows. Wanted to know the procedure to convert a string 12345 to 1234E and 21342 to 2134B and so on. I mean to say the last digit should be converted to the corresponding alphabet. Thank you in advance. Regards Susant (5 Replies)
Discussion started by: susant.igate
5 Replies

8. UNIX for Dummies Questions & Answers

How to skip first line from a file while manupulating the file

I need to put single quotes on the columns of a .csv file. The first row contains the column headers. I need to skip the first row and put quotes for rest of the rows. Would please someone help me with this. Thanks JP (4 Replies)
Discussion started by: JPalt
4 Replies

9. Shell Programming and Scripting

PERL data - sorting

Hello, I have a page where multiple fields and their values are displayed. But I am able to sort only a few fields. When I looked into the issue, it is seen that the for each row of info , an unique id is generated and id.txt is generated and saved. Only those fields which are inside that id.txt... (3 Replies)
Discussion started by: eagercyber
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