setter and getter functions for file manipulation with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting setter and getter functions for file manipulation with sed
# 1  
Old 02-28-2012
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:
Code:
setNumber()
{
    sed -i '/^'$1'/s/[0-9][0-9]*\.*[0-9]*/'$2'/g' file
}

getNumber()
{
...
}

setParam()
{
...
}

getParam()
{
...
}

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
number 1.1;
float 1.2;
double 1.3;
real 1.4;
stuff 1.5;
decimal 1.6;
fruit cherry;
veg pumpkin ;
vegetable pepper;
Thanks!
# 2  
Old 02-28-2012
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 ----------

Also, what shell are you using?
# 3  
Old 02-28-2012
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.

Thank you for your time!

unihub.ru/tools/ofservice/browser/trunk/2.0.1/OpenFOAM-2.0.1/tutorials/incompressible/simpleFoam/motorBike/system/controlDict

use https://, it is working here

Last edited by Toorop; 02-28-2012 at 04:22 PM.. Reason: url addition
# 4  
Old 02-28-2012
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 ----------

Quote:
Originally Posted by Toorop
unihub.ru/tools/ofservice/browser/trunk/2.0.1/OpenFOAM-2.0.1/tutorials/incompressible/simpleFoam/motorBike/system/controlDict
403 forbidden...I think.

---------- Post updated at 02:24 PM ---------- Previous update was at 02:17 PM ----------

Got it. Nowhere near as easy as what you asked for. Working on it.
# 5  
Old 02-28-2012
First of all, thanks a lot.

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!
# 6  
Old 02-28-2012
Quote:
Originally Posted by Toorop
First of all, thanks a lot.

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:

Code:
#!/bin/bash
cp data data-backup

awk 'function get(N)
{
        split(LINE[VAR[N]], A);
        sub(/;$/, "", A[2]);
        return(A[2]);
}

function set(N,V)
{
        if(!VAR[N])
        {
                LINE[++C]=N "\t" V ";"
                VAR[N]=C;
        }
        else
        {
                $0=LINE[VAR[N]];
                $2=V ";"
                LINE[VAR[N]]=$0
        }
}

/{/ && LL       {       PP[++L]=VP;     VP=VP LL "."    }
/}/             {       VP=PP[L--];                     }

{       LL=$0;  NAME=""; VALUE="";      LINE[++C]=$0     }

(NF==2) && /;$/ {       VAR[VP $1]=NR   }

END {
        set("FoamFile.version", "3.0");
        set("newvalue", "slartibartfast");
        for(N=1; N<=C; N++)    print LINE[N];
}' data-backup > data

Edit the part in red to your heart's content.

Last edited by Corona688; 02-28-2012 at 05:11 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

File manipulation place 0 before the number using sed

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)
Discussion started by: akopocpoypoy
4 Replies

2. Shell Programming and Scripting

sed for string manipulation

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)
Discussion started by: esham
10 Replies

3. Programming

Simplify setter and getter of java class

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)
Discussion started by: yifangt
6 Replies

4. Shell Programming and Scripting

sed flat file manipulation

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)
Discussion started by: gblmin
3 Replies

5. Shell Programming and Scripting

Manipulation with the string using sed

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)
Discussion started by: anand.shah
2 Replies

6. Shell Programming and Scripting

SED/AWK file read & manipulation

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)
Discussion started by: sal_tx
4 Replies

7. Shell Programming and Scripting

File manipulation with AWK and SED

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)
Discussion started by: Pauline mugisha
5 Replies

8. Shell Programming and Scripting

How to use sed for string manipulation

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)
Discussion started by: hikrishn
2 Replies

9. Shell Programming and Scripting

file name Manipulation using sed

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)
Discussion started by: Zak
6 Replies
Login or Register to Ask a Question