sed - String substitution within specified section in ini type file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed - String substitution within specified section in ini type file
# 1  
Old 12-29-2013
sed - String substitution within specified section in ini type file

Hello.
I am trying to modify a config file which is in windows *.ini type file.
I have found a piece of code here :linux - Edit file in unix using SED - Stack Overflow

As I can't make it doing the job , I am trying to find a solution step by step.

here a modified sample file : my_sample.ini for my first question
Code:
[handler_syncserver]
class = StreamHandler
args = (sys.stderr,)
level = INFO
formatter = generic

handler_syncserver_errors
class = handlers.RotatingFileHandler
args = ('/tmp/sync-error.log',)
level = ERROR
formatter = generic

[formatter_generic]
format = %(asctime)s,%(msecs)03d %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %Y-%m-%d %H:%M:%S

The first question is about range adress :
When I use this command :
Code:
sed -n /^handler_syncserver_errors/,/^$/p  my_sample.ini

The correct range is printed on console screen :
Code:
handler_syncserver_errors
class = handlers.RotatingFileHandler
args = ('/tmp/sync-error.log',)
level = ERROR              
formatter = generic

Now using the real sample file :
Code:
[handler_syncserver]
class = StreamHandler
args = (sys.stderr,)
level = INFO
formatter = generic

[handler_syncserver_errors]
class = handlers.RotatingFileHandler
args = ('/tmp/sync-error.log',)
level = ERROR
formatter = generic

[formatter_generic]
format = %(asctime)s,%(msecs)03d %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %Y-%m-%d %H:%M:%S

The modified command does not work, and remove all header section
Code:
sed -n /^\[handler_syncserver_errors\]/,/^$/p  my_sample.ini

Code:
class = StreamHandler
args = (sys.stderr,)
level = INFO
formatter = generic

class = handlers.RotatingFileHandler
args = ('/tmp/sync-error.log',)
level = ERROR
formatter = generic

datefmt = %Y-%m-%d %H:%M:%SLINUX-TEST-123:/opt/firefox/favoris_sync/server-full #

Iam using sed on opensuse :
Code:
13.1 kernel 3.11.6-4-desktop #1 SMP PREEMPT Wed Oct 30 18:04:56 UTC 2013 (e6d4a27) x86_64 x86_64 x86_64 GNU/Linux

Code:
sed --version
sed (GNU sed) 4.2.2
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Jay Fenlason, Tom Lord, Ken Pizzini,
and Paolo Bonzini.
GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-sed@gnu.org>.
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.

The second question will come after this one

Any help is welcome.

---------- Post updated at 18:19 ---------- Previous update was at 18:10 ----------

Found answer to first question here
Trouble with sed and ini file parsing | Unix Linux Forums | Shell Programming and Scripting

--> add single quote
Code:
sed -n '/^\[handler_syncserver_errors\]/,/^$/p'  my_sample.ini

# 2  
Old 12-29-2013
this is a general algorithm for getting the specific block you want. you can use it in any language you want.

Code:
#!/usr/bin/env ruby -w


match = false
str   = ""
File.open("file").each_line do|line|      # open file for processing
  if match and line[/^$/]                 # if match and empty line
    puts str                              # print string
    break
  end
  if line[/handler_syncserver_errors/]    # set match if found your string
    match = true   
  end
  if match                                # gather your lines if match
    str = str + line
  end
end

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed substitution or awk, need to direct change the file

I want change the file when the line contains $(AA) but NOT contains $(BB), then change $(AA) to $(AA) $(BB) eg: $(AA) something $(AA) $(BB) something (7 Replies)
Discussion started by: yanglei_fage
7 Replies

2. Shell Programming and Scripting

Find string in XML file, copy contents of section

I am new, really new to bash scripts. I want to search an XML file for a certain string, say "1234567890" Once found, I want to copy the entire contents from the previous instance of the string "Entity" to the next instance of "/Entity" to a txt file. And then continue searching for the... (4 Replies)
Discussion started by: jrfiol
4 Replies

3. Shell Programming and Scripting

Trouble with sed and ini file parsing

hi people, i'm having a hard time trying to extract a list of vars delimited by section inside a ini file ... let's consider this ini file : ; config file DESC = "channel synchro TGG01" DMM_VER = DMM23 PATH_FIFO = /users/tgg00/fifo QRT = BTS01.TGG.01.2 MODE_TRACE... (5 Replies)
Discussion started by: odium74
5 Replies

4. Shell Programming and Scripting

Extract section of file based on word in section

I have a list of Servers in no particular order as follows: virtualMachines="IIBSBS IIBVICDMS01 IIBVICMA01"And I am generating some output from a pre-existing script that gives me the following (this is a sample output selection). 9/17/2010 8:00:05 PM: Normal backup using VDRBACKUPS... (2 Replies)
Discussion started by: jelloir
2 Replies

5. Shell Programming and Scripting

SED 4.1.4 - INI File Change Problem in Variables= in Specific [Sections] (Guru Help)

GNU sed version 4.1.4 on Windows XP SP3 from GnuWin32 I think that I've come across a seemingly simple text file change problem on a INI formatted file that I can't do with SED without side effects edge cases biting me. I've tried to think of various ways of doing this elegantly and quickly... (5 Replies)
Discussion started by: JakFrost
5 Replies

6. Shell Programming and Scripting

Problem with sed string substitution

Hi, heres my problem: echo "aaaa(aaaa(aaa" | sed 's/a.*(//g' gives aaa but it should give aaaa(aaa .*( should find any string to the appearance of (, but it finds any string to the last appearance, any idea why, and how to do this? and what if the string ist... (2 Replies)
Discussion started by: funksen
2 Replies

7. Shell Programming and Scripting

Using Sed to duplicate a section of a file....

hello all, I have a file like this: section 1 blah1 blah2 section 2 blah1 blah2 section 3 blah1 blah2 and I want to use sed to duplicate section 2, like this: section 1 blah1 blah2 section 2 blah1 blah2 section 2 blah1 (2 Replies)
Discussion started by: nick26
2 Replies

8. Shell Programming and Scripting

sed & awk--get section of file based 2 params

I need to get a section of a file based on 2 params. I want the part of the file between param 1 & 2. I have tried a bunch of ways and just can't seem to get it right. Can someone please help me out.....its much appreciated. Here is what I have found that looks like what I want....but doesn't... (12 Replies)
Discussion started by: Andy Cook
12 Replies

9. Shell Programming and Scripting

Sed - substitution for whole string

Hello I have several files where a string similar to this appears: /home/workload/bin/ProcDly/scrpts/T54.sh > $LOG I need to change it to something like this: $VARIABLE > $LOG However, due to the configuration of the rest of the files, I should only find this string by the... (4 Replies)
Discussion started by: Scarlos
4 Replies

10. UNIX for Dummies Questions & Answers

Sed String Substitution

Hi! I've the following script code with an input parameter: sed 's/oldstring/$1/g' myfile > newfile (I launch it with comman line: $ MyShell newstring) Problem: the substituion doesn't work (oldstring becomes $1, instead of newstring). How could I solve this situation? Thanks, ... (2 Replies)
Discussion started by: pciatto
2 Replies
Login or Register to Ask a Question