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 with SED but it seems to me that I'm going to have to try with AWK or finally learn PERL and move to it.
However, before I take these drastic measures let me present the problem here to the SED gurus.
The problem basically stems from the fact that INI format text files do not have a closing tag for the end of the [section], the start of the next section or end of file is the end of the section. This prevents me from using the AWK address range "/ADDR1/,/ADDR2/" feature for limiting the edits to only the section because of the annoying side effect that is the /ADDR2/ range is "inclusive" as per the SED info - 3.2 Selecting lines with `sed' page.
This /ADDR2/ inclusiveness causes problems since once it hits for the next section start then /ADDR1/ doesn't hit again so code for that address range is skipped entirely for the next section causing the SED script to hop-scotch across the INI file skipping every other match eligible section.
Here is an actual sample INI file to work on as an example of the format in case someone is unfamiliar with it. The idea is to change the values in the [supplies_res_*] sections without touching the [supplies_generic] or [trade_generic_sell] sections.
The code below shows the problem with trying to create the address range using the beginning of the next section as the end range, since the inclusiveness of /ADDR2/ address makes the code skip any changes in that section.
SED Code Example - Broken = Hop-Scotch Section Skipping
Code:
# Section Address Range
/^[[:space:]]*\[[[:space:]]*%_section%[[:space:]]*\]/I , /^[[:space:]]*\[/
If I try other /ADDR2/ ranges such as "$" for end of file then all sections that contain the same variable will be changed to the end of the file, not something that I want. I cannot use blank line /^[[:space:]]*$/ for ADDR2 since the sections have blank spaces and lines inside them.
If I try to get really clever and do something like below then it doesn't work either no matter what I try to do or think of.
SED Code Example - Broken = Manual Line Reading Doesn't Work (Late Night Idea)
Code:
# INI Section
/^[[:space:]]*\[[[:space:]]*%_section%[[:space:]]*\]/I {
# Read Line
: read;
n;
# Next Section Jump Out
/^[[:space:]]*\[[[:space:]]*%_section%[[:space:]]*\]/I! {
/^[[:space:]]*\[/ b;
};
# Variable Line
/^[[:space:]]*%_variable%[[:space:]]*=/I {
# Value Replace
# Note: Retain same whitespace after replacement except for value.
s/^([[:space:]]*%_variable%[[:space:]]*=[[:space:]]*)(.*)[[:space:]]*$/\1%_value%/I;
};
# Loop
b read;
};
Here is a data file, which I believe is in YAML. I am trying to retrieve just the 'addon_domains" section, which doesnt seem to be as easy as I had originally thought. Any help on this would be greatly appreciated!! I have been trying to do this in awk and mostly bash scripting instead of perl... (3 Replies)
I've been trying to remove some lines of a xml file that looks like this:
<parent>
<child>name1</child>
<lots_of_other tags></lots_of_other_tags>
</parent>
<parent>
<child>name2</child>
<lots_of_other tags></lots_of_other_tags>
</parent>
<parent>
<child>name3</child>
... (5 Replies)
using sed to replace a specific string on a specific line number using variables
this is where i am at
grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean
grep -n Destination... (2 Replies)
Ok so all i'm trying to do here is output a file and change the color of a specific word. I can't use grep with color because I need all lines of the file not just lines that match the pattern.
I can get this substitution to work but when it displays it shows exactly what i'm putting it rather... (14 Replies)
Hello,
I have this file.
aaa
port=1234
time
bbb
port=2233
name
ccc
port=4444
name
Is there any way with sed to change only the occurence of "port" which comes after section to have as output : (12 Replies)
hello!
I am trying to use sed to copy specific set of lines from a file for which the starting and ending line numbers of the lines to be copied are stored in shell variables. How can i copy those lines?
if the input_file is something like this
and if the following is the script
a=2
b=4... (4 Replies)
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)
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... (1 Reply)
Hi,
I am writing a shell script where I want that # should be added in all those lines as the first character where the pattern matches.
file has lot of functions defined
a.sh
#!/bin/bash
fn a {
beautiful evening
sunny day
}
fn b {
}
fn c {
hello world .its a beautiful day
... (12 Replies)
Hello I have a script that searches a file for a specific string and then changes the nth column after that string. I have searched online for how to do this with sed but have not seemed to find a solution that works for me. I am using bash.
Some background info:
- Currently I am using awk to... (4 Replies)