How to update a specific key in a specific section using sed? - sed

I have an ini file similar to the following one:
[seciton1]
key1 = 0
key2 = 0
[section2]
key1 = 0
key2 = 0
I want to update key1 of section "section2" from 0 to 1. what will be the accurate sed command for the same.
also is sed command supported in SSH.NET c# library? if not then what are the ways using ssh.net we can update ini file.

This will update key2 once when its after section2:
awk '/section2/ {f=1} /key2/ && f {$3=1;f=0} 1' file
[seciton1]
key1 = 0
key2 = 0
[section2]
key1 = 0
key2 = 1
To write it back to the original file, do:
awk '/section2/ {f=1} /key2/ && f {$3=1;f=0} 1' file > tmp && mv tmp file

This might work for you (GNU sed):
sed -i '/^\[/h;G;/section2/s/\(key1 = \).*/\11/m;P;d' file
Make a copy of the section header and append it to each line. If the current section header is section2 substitute 1 for the value of key1.
N.B. The m flag in the substitute command restricts the .* to the remainder of the first line (of the now two lines in the pattern space i.e. the current line and the appended section header) only.

Related

Using sed to remove string between two other stings in specific column

I want sed to look at only the first column of a tab-delimited file and remove everything between some string and the first tab for all rows. Sorry if this is repetitive, have searched other entries and cannot seem to find anything quite right.
For example, input:
blah-a_blah-b.13_blah-x_blah-y 0 0 0 0 0 17.983559
desired output:
blah-a_blah-b.13 0 0 0 0 0 17.983559
Tried unsuccessfully various iterations of below:
sed -i 's/\(\.[0-9]\).*\([^\t]\)//1' file
Advice?
This should work in gnu-sed:
sed -E 's/^([^\t]*\.[0-9]+)[^\t]+/\1/' file
blah-a_blah-b.13 0 0 0 0 0 17.983559
For non-gnu sed use:
sed -E 's/^([^[:blank:]]*\.[0-9]+)[^[:blank:]]+/\1/' file

Sed: How to insert a pattern which includes single ticks?

I'm trying to use sed to replace a specific line within a configuration file:
The pattern for the line I want to replace is:
ALLOWED_HOSTS.*
The text I want to insert is:
'$PublicIP' (Including the single ticks)
But when I run the command:
sed 's/ALLOWED_HOSTS.*/ALLOWED_HOSTS = ['$PublicIP']/g' /root/project/django/mysite/mysite/settings.py
The line is changed to:
ALLOWED_HOSTS = [1.1.1.1]
instead of:
ALLOWED_HOSTS = ['1.1.1.1']
How shall I edit the command to include the single ticks as well?
You could try to escape the single ticks , or better you can reassign the variable including the simple ticks:
PublicIP="'$PublicIP'".
By the way even this sed without redifining var, works ok in my case:
$ a="3.3.3.3"
$ echo "ALLOWED_HOSTS = [2.2.2.2]" |sed 's/2.2.2.2/'"'$a'"'/g'
ALLOWED_HOSTS = ['3.3.3.3']
Even this works ok:
$ echo "ALLOWED_HOSTS = [2.2.2.2]" |sed "s/2.2.2.2/'$a'/g"
ALLOWED_HOSTS = ['3.3.3.3']

Replacing first occurence except comments using sed

I have a text file of the form:
a = 1
#b = [2,3]
c = 4
d = [5,6]
e = [7,8]
I want to replace the pattern inside the brackets (and the brackets) with a number, but ignore matches in the comments, preferably using sed.
For files with exactly one matching line, I've used
sed -i "/^#/!s/\[.*\]/9/" myfile
How can this be modified to replace only the first match if there are more?
This is the correct, because changing just the first occurence.
awk '!end && /^[^#]+ = \[/ {$3="9"; end=1}1' myfile
if there is not end flag and line is not beggining from # and match to = [, then change the third column and set the flag to prevent changing in next occurences.
a = 1
#b = [2,3]
c = 4
d = 9
e = [7,8] <--- this is not changed as you want
This one-liner should do the job:
sed '/^\s*#/!{s/\[[^]]*\]/9/}' file
add the -i option if you like to do the change in place.

Using tab as sed separator

I would like to include tab as delimited new row to a file inp.txt.
This is the input produced by R:
inp <- 'AX-1 1 125
AX-2 2 456
AX-3 3 3445'
inp <- read.table(text=inp, header=F)
write.table(inp, "inp.txt", col.names=F, row.names=F, quote=F, sep="\t")
That´s what I am trying to do:
sed -i '1i The name\tThe pos\tThe pos2\' inp.txt
However, those three col names: 1- The name, 2- The pos, 3- The pos2 are not separated by tab in the output file. It just contain the \t string. Someone can help me here with the syntax?
Put the tab in a variable:
tab=$(echo "\t")
or
tab=$'\t'
Then you can use it in your sed script:
sed -i "1i The name${tab}The pos${tab}The pos2" inp.txt

sed: Delete first line of hold space?

How do I delete the first line of the hold space in sed?
I've tried
x;
s/.*\n//;
x;
But .*\n matches up to the last newline, deleting all the lines except for the last one.
this should remove the 1st line from "hold space"
x;s/[^\n]*\n//
Example:
kent$ sed -n 'H;${x;p}' <(seq 3)
1
2
3
remove the first empty line:
kent$ sed -n 'H;${x;s/[^\n]*\n//;p}' <(seq 3)
1
2
3
Simple put any random string with h i.e 1h;1d, by default it's empty.