Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 10-02-2008, 07:31 PM   #1
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


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.

Old 10-02-2008, 07:43 PM   #2
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,396

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
Show us your code so far.
You'll need the open(),close(), while(), split() cmds.
Book mark these:


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 04:10 PM
[Perl] - save input in .html file noir911 Programming 2 01-07-2007 02:36 PM
What file to modify to change a mac address or to save eth0 configuration ikw38 Linux - Networking 2 04-26-2006 04:52 PM
What is the data type field definition to save RTF file? Linux4BC Linux - General 3 06-02-2004 04:19 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:55 AM.

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