Sed Insert Line with Spaces to a Specific Line

sed insert line with spaces to a specific line

You can escape the space character, for example to add 2 spaces:

sed -i "${line} i \ \ ${text}" $file

Or you can do it in the definition of your text variable:

text="\ \ hello world"

Inserting spaces before the word while adding lines using sed

You can use this sed:

sed -i '/abc/a\    123' file

Ex:

$ sed '/abc/a\    123' file
abc
123
def

Unix sed command to insert a new line with same space indentation before the matching string in yaml file

The attempted solution is almost there. You just need to capture the initial spaces/tabs:

  echo "  fields:" | sed 's/^\([[:space:]]*\)\(fields:\)/\1ignore_older: 24h\n\1\2/'

Explanation:

  • s/^\([[:space:]]*\)\(fields:\) if a line begins with zero or more spaces and "fields:"
  • /\1ignore_older: 24h\n\1\2/ add a new line consisting of spaces and "ignore_older: 24h"

sed, insert whitespace at the Nth position after a delimiter

Given the limited input:

sed '3,$s/ ../& /g'

From line 3 to the end of the file 3,$, match a space followed by any 2 characters ... Use & to mean "what was matched" and add a space after it. Use g to do repeatedly.

Using sed to append a line ignoring the white space for a specific line

You pattern is incorrect. Try this instead

sed -e '/a *b *c/a new line' file.txt

If you want to modify the original file in-place, add the -i flag. It would probably better to test it without it first though.

Sed - Insert line with text after match pattern between two strings

Data set:

$ cat test.txt
text text
text text
[textabc pattern 1]
text text
text text
xyz = 123
text text
[textdef pattern 2]
text text
text text

A couple small changes to OPs current sed command:

# current

sed '/^\[textabc pattern 1\]$/,/^\[textdef pattern 2\]/ ^xyz .*/a NEW STRING/' test.txt

# new/proposed (2 lines); the 'a'ppend option requires a new line before the end '}'

sed -e '/^\[textabc pattern 1\]$/,/^\[textdef pattern 2\]/{/^xyz .*/aNEW STRING
}' test.txt

# new/proposed (1 line); break into 2 segments via a 2nd '-e' flag to eliminate need for embedded newline character

sed -e '/^\[textabc pattern 1\]$/,/^\[textdef pattern 2\]/{/^xyz .*/a'"NEW STRING" -e '}' test.txt

Both of the above new/proposed sed commands generate the following:

text text
text text
[textabc pattern 1]
text text
text text
xyz = 123
NEW STRING
text text
[textdef pattern 2]
text text
text text

NOTE: Once OP is satisfied with the results the -i flag can be reintroduced to allow sed to make in-place changes to data file.

sed or other - remove specific html tag text from file

Should I be using the sed command for desired results?

Actually grep suits it better with:

grep -Ev '</(body|html)>' file

Hello World

If you want to remove specific <body>\n</html>\n string only then use this sed that would work with any version of sed:

sed '/<\/body>/{N; /<\/html>/ {N; s~</body>\n</html>\n~~;};}' file

Hello World


Related Topics



Leave a reply



Submit