LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-02-2008, 08:31 PM   #1
rounak94
LQ Newbie
 
Registered: Oct 2008
Posts: 1

Rep: Reputation: 0
How to modify a field in few lines in a file and save the new file - in Perl


Hi,

I have a small file the contents of which look like this:

########

Line x
PCC a b 10.0 d
PBB a b 12.3 d
PDD a b 14.5 d
Line x+4

########

The lines I am interested in starts with 'P' - here line x+1, x+2 and x+3 - these 3 lines.

I want to do the following things:

1. Go to the first line starting with 'P' (capital P, case-sensitive)
2. Read the 4th field (here 10.0), multiply it by (11/6) and then take the nearest number divisible by 4. For example, here 10*11/6 = 18.33. So I want to round it to 20, which is the nearest number divisible by 4. If it falls in between, say 18 (equidistant from 16 and 20) - I want to round UP to 20 (instead of setting it to 16).
3. Replace this 4th field by the new number.
4. Continue to next lines starting with 'P' and repeat the above operation on the 4th field.
5. If there is no more line starting with 'P' we are finished - save the file (it does not matter if it overwrites the original file)
6. I have a folder where I have multiple files with this same format. I want to do this operation on all the files in that folder at one step instead of doing it again and again for all the files.

I am new in Perl. Trying to learn some quick stuffs. Can anybody help me here please?

I would really appreciate any help.

Thanks.
 
Old 10-02-2008, 08:43 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Show us your code so far.
You'll need the open(),close(), while(), split() cmds.
Book mark these:
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing part (lines) of a file ( bash or perl ) zomane Programming 3 10-26-2007 05:10 PM
[Perl] cgi.pm - save input in .html file noir911 Programming 2 01-07-2007 03:36 PM
What file to modify to change a mac address or to save eth0 configuration ikw38 Linux - Networking 2 04-26-2006 05:52 PM
What is the data type field definition to save RTF file? Linux4BC Linux - General 3 06-02-2004 05:19 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 09:09 PM


All times are GMT -5. The time now is 06:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration