How to append a string at end of a specific line in a file in bash
Using sed
and the pattern described:
sed '/192.168.1.2/s/$/ myalias/' file
Using sed
and a specific line number:
sed '2s/$/ myalias/' file
Append line after [stuff] using the sed command
The address /[stuff]/
is interpreted as a regular expression, i.e., matches any line that contains any of the characters s
, t
, u
or f
. To avoid interpretation as a bracket expression, you have to escape the square brackets:
sed -i "/\[stuff\]/ a variable = 1" testfile
How to append data at a particular line in a file using sed , where data is from another file
Why not have sed
write its own script?
sed -e "$(sed -e 's|^\(.*\)_data_to_be_appended=\(.*\)|/^\1=.*/ s//\& \2/|' cfg)" script
Inner command reads the config file and emits /^flag=.*/ s//& xyz/
which is then applied to the script file.
Output:
./file.config
flag=abc xyz
echo $flag
The two escaped parenthesis pairs capture key and value as \1
and \2
.
In s//& \2/
the //
is the null regex which matches the last
regex used (in /^…/
) and replaces the entire match (&
) followed
by the captured value.
sed insert line command OSX
You should put a newline directly after the \
:
sed '3i\
text to insert' file
This is actually the behaviour defined by the POSIX specification. The fact that GNU sed allows you to specify the text to be inserted on the same line is an extension.
If for some reason you need to use double quotes around the sed command, then you must escape the backslash at the end of the first line:
sed "3i\\
text to insert" file
This is because a double-quoted string is processed first by the shell, and \
followed by a newline is removed:
$ echo "abc\
def"
abcdef
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 + how to append lines with indent
Apparently the first sequence of characers has to be escaped
sed '/missingok/a\\trotate 1\n\tsize 1k' /etc/logrotate.d/httpd
Append line after last match with sed
Using single awk you can do:
awk 'FNR==NR{ if (/thing4/) p=NR; next} 1; FNR==p{ print "foo" }' file file
Header
thing0 some info
thing4 some info
thing4 some info
thing4 some info
foo
thing2 some info
thing2 some info
thing3 some info
Earlier Solution: You can use tac + awk + tac
:
tac file | awk '!p && /thing4/{print "foo"; p=1} 1' | tac
How to append on the same line the variable
You can place your variable directly into the substitution:
sed "s~\(Green:\)~\1$variable~" test.txt
This will match the string Green:
and capture it into group \1
, you can then append $variable
to the captured group in the substitution. Note I have used ~
as a separator as your substitution string contains multiple slashes /
which cause problems if you also use /
as the separator.
Related Topics
Nasm Gcc Command Error with Subprogram as Seperate File
Specify Git Tool to Use in Multi-Os Agent Jenkins Environment for Declarative Pipeline
Using Pipe in Linux Using Parent and Child Process
Preventing to Bash Script from Running in Parallel or Overlap Using Cron
Determine Os from a Single Command Line Operation
How to Merge Two Rows in a Same Row from a Text File in Linux Shell Script
Safely Remembering Ssh Credentials in Bash Script
Find the Average of Fields in the Columns
A Way to Prevent Bash from Parsing Command Line W/Out Using Escape Symbols
How to Mail Script Output in Table Format
Calling Printf from Assembly Language on 64Bit and 32Bit Architecture Using Nasm
When Installing Rust Toolchain in Docker, Bash 'Source' Command Doesn't Work
How to Escape the Bang (!) Character in Linux Bash Shell
Why Does This Shell Pipeline Exit