Sponsored Content
Top Forums Shell Programming and Scripting setter and getter functions for file manipulation with sed Post 302602895 by Corona688 on Tuesday 28th of February 2012 04:39:27 PM
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..
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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 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

7. 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

8. 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

9. 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
GIT-VAR(1)							    Git Manual								GIT-VAR(1)

NAME
git-var - Show a git logical variable SYNOPSIS
git var ( -l | <variable> ) DESCRIPTION
Prints a git logical variable. OPTIONS
-l Cause the logical variables to be listed. In addition, all the variables of the git configuration file .git/config are listed as well. (However, the configuration variables listing functionality is deprecated in favor of git config -l.) EXAMPLE
$ git var GIT_AUTHOR_IDENT Eric W. Biederman <ebiederm@lnxi.com> 1121223278 -0600 VARIABLES
GIT_AUTHOR_IDENT The author of a piece of code. GIT_COMMITTER_IDENT The person who put a piece of code into git. GIT_EDITOR Text editor for use by git commands. The value is meant to be interpreted by the shell when it is used. Examples: ~/bin/vi, $SOME_ENVIRONMENT_VARIABLE, "C:Program FilesVimgvim.exe" --nofork. The order of preference is the $GIT_EDITOR environment variable, then core.editor configuration, then $VISUAL, then $EDITOR, and then finally vi. GIT_PAGER Text viewer for use by git commands (e.g., less). The value is meant to be interpreted by the shell. The order of preference is the $GIT_PAGER environment variable, then core.pager configuration, then $PAGER, and then finally less. DIAGNOSTICS
You don't exist. Go away! The passwd(5) gecos field couldn't be read Your parents must have hated you! The passwd(5) gecos field is longer than a giant static buffer. Your sysadmin must hate you! The passwd(5) name field is longer than a giant static buffer. SEE ALSO
git-commit-tree(1) git-tag(1) git-config(1) GIT
Part of the git(1) suite Git 1.7.10.4 11/24/2012 GIT-VAR(1)
All times are GMT -4. The time now is 03:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy