setter and getter functions for file manipulation with sed
Hi,
I would really appreciate some help, I couldn't nail my problem:
I would like to create some setter and getter functions to make my life easier.
my sample file contains:
keyword - some tabs - value - semicolon
number 12.1;
float .3;
double 12;
real 12.2324;
stuff .234;
decimal 15.;
fruit apple;
veg carrot;
vegetable tomato;
my functions:
The setNumber works with all the different number forms, except the one which starts with a decimal point. Unfortunately, I'm not an expert in text manipulation.
The getters need to return the key value.
Maybe the two setter and getter functions can be merged, but I'm curious how to accomplish the get/set number as well.
Desired functionality:
setNumber number 1.1
setNumber float 1.2
setNumber double 1.3
setNumber real 1.4
setNumber stuff 1.5
setNumber decimal 1.6
setParam veg pumpkin
setParam vegetable pepper
setParam fruit cherry
Why not show us what you want to do, not the way you want to do it? There's got to be a better way. sed is not a function, sed is an external utility which can process thousands of lines at once. Running it once per value is like making 10,000 phone calls to say 10,000 words. Depending on these functions is going to make any scripts made from them extremely slow.
What I'd do is I'd load the data into memory, so you don't have to keep running sed over and over. How to do this, again, depends on what you're trying to do...
---------- Post updated at 01:45 PM ---------- Previous update was at 01:39 PM ----------
Hi there,
I got your point, but I don't think I made a mistake. I provided a small text file that resembles my actual real world case. I listed some numbers that may occur and can cause some trouble. And that's all. If the file that I tried to change is huge I would stressed that in the first place.
So to get it straight:
my real file is an OpenFOAM controlDict file like - cant post URL, forum engine wont allow, sorry
I just wanted to make some manipulation on it in an elegant way, without to much code duplication and overhead.
My shell is bash.
post it without the http in front, then. What it actually looks like makes the difference between whether any of our answers will work or fail. What little I've managed to google up suggests that it does in fact have internal structure you haven't shown, structure which would be destroyed by any of the methods I've tried so far.
If your goal is to avoid overhead, functions running individual instances of sed to do each little thing are exactly what you want to avoid. You want to read the file once, change what you want to change, and write the result out.
---------- Post updated at 02:17 PM ---------- Previous update was at 02:08 PM ----------
Hopefully, there's no misunderstanding between us, since I still don't know why the original task is much easier.
'setParam application newSolver' should change the line which starts with "application" and change the value to "newSolver"
application simpleFOAM; ---> application newSolver;
IMHO, its the same problem as 'setParam fruit cherry' in the initial task!
Hopefully, there's no misunderstanding between us, since I still don't know why the original task is much easier.
// No comments.
No /* multi-line comments. */
No { code blocks. }
No "strings".
Your code only might work under very particular circumstances, can't tell code blocks apart from each other, and may edit the heck out of the comments by accident.
I can give you something to do a specific task easily. You wanted something general, and that's hard.
This is why I asked what you actually wanted to do, not what manner you wanted to accomplish it in. Replacing values is easy enough. Giving you 'getters' and 'setters' so you can replace in exactly the manner you think you should isn't.
---------- Post updated at 03:39 PM ---------- Previous update was at 02:59 PM ----------
I still can't handle multi-line comments, so you'd better hope the variable names never show up in comments. But:
Edit the part in red to your heart's content.
Last edited by Corona688; 02-28-2012 at 05:11 PM..
I'm new with sed, and i am really confused with slashes, backslashes, parentheses, I've tried reading some beginner's guide, but still trouble fixing this problem, do you have any tips where or what to read to learn more about sed? can you also help me with my problem?
Note: I was tasked to use... (4 Replies)
I have a file which contains contents like below
proxy.config.cluster.mc_group_addr 224.0.1.37
proxy.config.log.logging_enabled 3
proxy.config.log.squid_log_enabled 1
Need to modify to
'proxy.config.cluster.mc_group_addr': '224.0.1.37'
'proxy.config.log.logging_enabled': '3'... (10 Replies)
I am trying to verify my understanding on setter and getter on java class with this example:
//MaximumFinder2.java
import java.util.Scanner;
public class MaximumFinder2
{
public static void main (String args)
{
Scanner input = new Scanner(System.in);
... (6 Replies)
Hello, I have a large flat file where i need to change data in columns 131-133 based on what is in columns 172-173. I am not sure if I need to read the file line by line and make the change or if I can do this in a single statement. thank you (3 Replies)
hello All,
When I run find command on certain directory I may get one of the following output depending on configuration
A. ./rel/prod/libpam.a
B. ./rel/fld/libpam.a
C. ./deb/detail/libpam.a
D. ./deb/err/libpam.a
I want to get output as below
A. rel/prod
B.... (2 Replies)
I have large number of data files, close to 300 files, lets say all files are same kind and have extension .dat , each file have mulitple lines in it.
There is a unique line in each file containing string 'SERVER'. Right after this line there is another line which contain a string 'DIGIT=0',... (4 Replies)
Hello
How do i check that correct input files are used while using AWk and SED for file manipulation?
e.g
awk '/bin/ {print $0 }' shell.txt
sed 's/hp/samsung/' printers.txt
how do i ensure that the correct input files I am working with are used? (5 Replies)
Hi,
I would like to know How to use use sed for manipulating string for the following situation.
Basically my objective is to check validity of the filename in my shell script.
I am getting a parameter like this for my shell script.
Check my folder is having some space.
$1=/root/krishna... (2 Replies)
Hi,
I have a file name, for which I want to strip out the first bit and leave the rest...
So I want to take the file name .lockfile-filename.10001 ,strip it and have only filename.10001 ...
Thanking you all inadvance,
Zak (6 Replies)