Sed replace line start with space & # character beside varriable substituation - sed

i have a little problem in sed, i want to replace the following line :
space here#Include "/usr/local/apache/conf/userdata/std/2/varr1/var2/*.conf"
or
space here# Include "/usr/local/apache/conf/userdata/std/2/varr1/varr2/*.conf"
(note the space after #)
to the following
Include "/usr/local/apache/conf/userdata/std/2/varr1/varr2/*.conf"
the following code works, with line that doesn't start with space :
sed -i "s/# Include \"\/usr\/local\/apache\/conf\/userdata\/std\/2\/$varr1\/$varr2\/\*.conf\"/Include \"\/usr\/local\/apache\/conf\/userdata\/std\/2\/$varr1\/$varr2\/\*.conf\"/" file.name
any help will be appreciated,
thank all

You don't need to write the path, just capture it:
sed -i 's!# *Include \("[^"]*"\)!Include \1!' input.file

Instead of using / as delimiter, you can pick up any character you want instead.
By example :
sed -i 's###g' file
So, no need to put backslashes on everything, this is difficult to read for humans beings.
Finally, try doing this :
sed -i 's#^ *# *Include \+"/usr/local/apache/conf/userdata/std/2/varr1/var\+2/\*\.conf"#Include "/usr/local/apache/conf/userdata/std/2/varr1/varr2/*.conf"#g' file.name
NOTE
* character mean zero or N occurrences in regex
^ character mean start of line
* & . are special characters, so they are backslashed. Later means any single character

Related

How to insert space after a character if there is no space already using SED?

Say there is a character like this #ま. If I wanted to add a space after this i could do sed -i "s/#ま/#ま\ /g" * to add a space. But I would like to not do that because it would create a problem where there are double spaces.
Given a text such as #あの #ま高校生の頃に. #あの #ま 高校生の頃に. How do I add a space after #ま if there is no space in the beginning?
so the output would be something like
#あの #ま 高校生の頃に. #あの #ま 高校生の頃に
You can use sed with -r for regular expression.
For your example, you can use this command:
sed -i -r 's/#ま([^ ])/#ま \1/g' <file>
Essentially, you are searching for #ま[^ ], which is a #ま followed by exactly one non-space character. Then, you are replacing all such matches with #ま<space><that non-space character>.

Replace variable num in double quotes - SED

I have a line like this:
"abc/x-y-z": "^1.4"
I need to replace ^1.4with * in the same file such that the output is "abc/x-y-z": "*"
The num inside the double quotes could be any variable number.
I tried this but it is highly specific to ^1.4 number:
sed -i '21s/^1.4/*/' abc.json
With your shown samples, please try following. You need to escape ^ here to make it literal character and you need to escape . dot as well to make it treat as literal character.
sed 's/\^1\.4/*/' Input_file
OR as per OP's comment to make it dynamic try:
sed 's/\^[0-9]+\.[0-9]+/*/' Input_file
Also if you are performing it on 21st line of your file then use 21s like you tried in your attempt. This code will substitute only very 1st occurrence of ^1.4 here in case you want to substitute all occurrences then use g(globally substitution) option for above code.
I have not used -i option(to do inplace update into Input_file itself) once you are happy with results then use sed -i option in above code.
When making changes to files, I prefer the file editor ed to the stream editor sed (ed is standard, sed -i isn't, and different versions have different quirks that bite people here on a regular basis).
ed -s input.txt <<EOF
21s/"^[[:digit:]]\{1,\}\(\.[[:digit:]]\{1,\}\)\{0,1\}"/"*"/
w
EOF
On line 21, matches a quote followed by a carat followed by 1 or more digits, optionally followed by a period and another sequence of 1 or more digits and finally the trailing quote character. All that is replaced by "*", and finally the changed file is written back to disk.
Posix BREs are a pain, no? GNU ed 1.17 and newer, and NetBSD ed can take EREs instead:
ed -Es input.txt <<EOF
21s/"\^[[:digit:]]+(\.[[:digit:]]+)?"/"*"/
w
EOF
which is a lot easier to read.

How can I swap seperated ":" columns in sed?

How can I swap columns seperated ":" using sed?
for example
string1:string2
string3 string4:string5
string6:string7-string8
into
string2:string1
string5:string3 string4
string7-string8:string6
thanks!
This code will swap the columns around : in a file named example.txt -
sed -i -r 's/(.+):(.+)/\2:\1/' example.txt
Explanation -
-i is for in-place substitution
-r forces sed to use an extended regular syntax
.+ says look for any character any number of times. This is a very "greedy" regular expression but works in this case. Then, parentheses are used to capture the text.
Then, finally used \1 and \2 in reverse order to swap the columns around :

Matching strings even if they start with white spaces in SED

I'm having issues matching strings even if they start with any number of white spaces. It's been very little time since I started using regular expressions, so I need some help
Here is an example. I have a file (file.txt) that contains two lines
#String1='Test One'
String1='Test Two'
Im trying to change the value for the second line, without affecting line 1 so I used this
sed -i "s|String1=.*$|String1='Test Three'|g"
This changes the values for both lines. How can I make sed change only the value of the second string?
Thank you
With gnu sed, you match spaces using \s, while other sed implementations usually work with the [[:space:]] character class. So, pick one of these:
sed 's/^\s*AWord/AnotherWord/'
sed 's/^[[:space:]]*AWord/AnotherWord/'
Since you're using -i, I assume GNU sed. Either way, you probably shouldn't retype your word, as that introduces the chance of a typo. I'd go with:
sed -i "s/^\(\s*String1=\).*/\1'New Value'/" file
Move the \s* outside of the parens if you don't want to preserve the leading whitespace.
There are a couple of solutions you could use to go about your problem
If you want to ignore lines that begin with a comment character such as '#' you could use something like this:
sed -i "/^\s*#/! s|String1=.*$|String1='Test Three'|g" file.txt
which will only operate on lines that do not match the regular expression /.../! that begins ^ with optional whiltespace\s* followed by an octothorp #
The other option is to include the characters before 'String' as part of the substitution. Doing it this way means you'll need to capture \(...\) the group to include it in the output with \1
sed -i "s|^\(\s*\)String1=.*$|\1String1='Test Four'|g" file.txt
With GNU sed, try:
sed -i "s|^\s*String1=.*$|String1='Test Three'|" file
or
sed -i "/^\s*String1=/s/=.*/='Test Three'/" file
Using awk you could do:
awk '/String1/ && f++ {$2="Test Three"}1' FS=\' OFS=\' file
#String1='Test One'
String1='Test Three'
It will ignore first hits of string1 since f is not true.

Insert newline after pattern with changing number in sed

I want to insert a newline after the following pattern
lcl|NC_005966.1_gene_750
While the last number(in this case the 750) changes. The numbers are in a range of 1-3407.
How can I tell sed to keep this pattern together and not split them after the first number?
So far i found
sed 's/lcl|NC_005966.1_gene_[[:digit:]]/&\n/g' file
But this breaks off, after the first digit.
Try:
sed 's/lcl|NC_005966.1_gene_[[:digit:]]*/&\n/g' file
(note the *)
Alternatively, you could say:
sed '/lcl|NC_005966.1_gene_[[:digit:]]/G' file
which would add a newline after the specified pattern is encountered.
sed 's/lcl|NC_005966\.1_gene_[[:digit:]][[:digit:]]*/&\
/g' file
You need to escape . as it's an RE metacharacter, and you need [[:digit:]][[:digit:]]* to represent 1-or-more digits and you need to use \ followed by a literal newline for portability across seds.