LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu
User Name
Password
Ubuntu This forum is for the discussion of Ubuntu Linux.

Notices


Reply
  Search this Thread
Old 02-01-2012, 01:30 PM   #1
mostofas
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Rep: Reputation: Disabled
How to edit a line from a file?


Hello, I want to edit some parameters from a file.
Actually, I am not a programmer, so finding some difficulties in the basic.
Here is the file that I want to edit:
*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: 2.0.1 |
| \\ / A nd | Web: www.OpenFOAM.com |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class dictionary;
object blockMeshDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

convertToMeters 0.1;

vertices
(
(0 0 0)
(1 0 0)
(1 1 0)
(0 1 0)
(0 0 0.1)
(1 0 0.1)
(1 1 0.1)
(0 1 0.1)
);

blocks
(
hex (0 1 2 3 4 5 6 7) (20 20 1) simpleGrading (1 1 1)
);

edges
(
);

boundary
(

movingWall
{
type wall;
faces
(
(3 7 6 2)
);
}
fixedWalls
{
type wall;
faces
(
(0 4 7 3)
(2 6 5 1)
(1 5 4 0)
);
}
frontAndBack
{
type empty;
faces
(
(0 3 2 1)
(4 5 6 7)
);
}
);

mergePatchPairs
(
);

// ************************************************************************* //


For eg,
I want to change the parameters after the vertices which is bold.

Till now, I have written the code to open and read the file but couldnt do replacing or editing the required parameters.
Here is my code:

// reading a text file

#include <fstream>
#include <iostream>
#include <string>
#include<stdio.h>

using namespace std;

int main()
{
string line;
int count=0;
ifstream myfile ("blockMeshDict");

while(! myfile.eof()) {
getline(myfile, line);
count++;
if(count>=19 && count<=29) {
cout << line << endl;

}
}

// now to open an output file
//ofstream out ("blockMeshDict1");

if (count == 21){

line = line.replace("(2 2 2)","(0 0 0)");
cout << line << endl;
}


return 0;
}

.................................................................................................... ....

If anyone can help, I will really appreciate.
Thanks.
 
Old 02-01-2012, 02:33 PM   #2
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Rep: Reputation: 198Reputation: 198
Put CODE tags around your code. It makes it a lot easier to read. (Look for the # button when composing/editing posts.)

You state your problem as "I want to edit some parameters from a file." Is there a reason you're trying to write C code to do that rather the just opening it in a text editor?

If you have to be using C to do this for some reason you'd probably do better posting it in the Programming section. What you're trying to do is not Ubuntu specific.
 
Old 02-01-2012, 02:40 PM   #3
rustek
Member
 
Registered: Jan 2010
Location: Melbourne, IA, USA
Distribution: Ubuntu
Posts: 93

Rep: Reputation: 8
Can you alter the original file so the vertices structure is one line and everything still works?

ie. can you replace;
Code:
vertices 
(
(0 0 0)
(1 0 0)
(1 1 0)
(0 1 0)
(0 0 0.1)
(1 0 0.1)
(1 1 0.1)
(0 1 0.1)
);
with;
Code:
vertices ((0 0 0)(1 0 0)(1 1 0)(0 1 0)(0 0 0.1)(1 0 0.1)(1 1 0.1)(0 1 0.1));
And have it still work ok?

If the answer is yes, then it would probably be easier to find and replace the whole line rather than find the structure and then edit lines within it.

I realize that changing the problem is cheating, but whatever works...
 
Old 02-01-2012, 02:51 PM   #4
flamelord
Member
 
Registered: Jun 2011
Distribution: Arch Linux
Posts: 151

Rep: Reputation: 34
sed, perl, or python would be better choices for this problem than c.
sed would be the simplest if you are just changing one line, perl might be better if it is more complicated.
 
Old 02-02-2012, 07:25 AM   #5
mostofas
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
"No. Actually, the file content would be same everytime and its format cannot be changed."
 
Old 02-02-2012, 07:28 AM   #6
mostofas
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
"@flamelord - I am using Ajunta to do the coding. I am not sure if I can use perl in ubuntu."
 
Old 02-02-2012, 11:15 PM   #7
flamelord
Member
 
Registered: Jun 2011
Distribution: Arch Linux
Posts: 151

Rep: Reputation: 34
I would think that perl would be installed by default on Ubuntu.
 
  


Reply



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
[SOLVED] Need to edit last word on each line in file emmalg Linux - General 10 06-01-2010 10:36 AM
[SOLVED] edit multiple lines of a text file into 1 line: schneidz Programming 2 04-09-2009 11:22 AM
Trying to edit a file via Command line or script seefor Linux - General 4 10-21-2008 12:39 PM
bash to edit one line of a file Frayed Programming 8 04-22-2008 07:13 AM
how to edit a file when not in X (ie from the command line) ludwig W Linux - Newbie 12 04-22-2003 04:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu

All times are GMT -5. The time now is 09:25 AM.

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
Open Source Consulting | Domain Registration