Bash script: Appending text at the last character of specific line of a file
Try this:
sed -i "/^MYVERSION=/ s/\$/$VERSION/" myfile.txt
The idea is that it finds a line that starts with MYVERSION= and then replaces the end of that line with the contents of the $VERSION environment variable.
Edit: originally I wasn't sure if the first $
needed to be escaped, but @sehe's comment and its upvoters convinced me that it does.
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
bash append text with special character and newline to file at specific line
The problem you are having is the text contained in print_config
has unescaped newlines in the file. Specifically you are attempting multi-line insert of:
@ReactMethod
public void printBarCode(String str, int nType, int nWidthX, int nHeight,
int nHriFontType, int nHriFontPosition) {
byte[] command = PrinterCommand.getBarCodeCommand(str, nType, nWidthX, nHeight, nHriFontType, nHriFontPosition);
sendDataByte(command);
}
You cannot do that without preceding each newline with a '\'
character. From man sed
you have "i \ text Insert text, which has each embedded newline preceded by a backslash."
Instead, you may be better served by saving the contents above to a temporary file and then using the r filename
command to "Append text read from filename"
. In your case you would have:
sed -i '436r tempfile' file_to_modify
That would read the contents of tempfile
into file_to_modify
at line 436
without having to modify the text to escape each newline with a backslash.
Append text to the last line of a file in unix
You could go with dd
and notrunc
(tested on Linux 4.12):
printf ":" | dd of=file conv=notrunc bs=1 seek=$(( $(stat -c "%s" file) - 1))
How to append a character at the end of a specific line in a loop?
TL;DR:
$: c='!'
$: sed "s#\$# s/\$/ $c/#" fibs >script
$: sed -i "$(<script)" infile
Broken out -
A file of line numbers:
$: cat fibs
1
2
3
5
8
13
21
a file to be edited:
$: cat infile
1 a
2 b
3 c
4 d
5 e
6 f
7 g
8 h
9 i
10 j
11 k
12 l
13 m
14 n
15 o
16 p
17 q
18 r
19 s
20 t
21 u
22 v
23 q
24 x
25 y
26 z
3 steps -- first set your c
haracter variable if you're using one.
$: c='!'
Then make a script from the line number file -
$: sed "s#\$# s/\$/ $c/#" fibs >script
which creates:
$: cat script
1 s/$/ !/
2 s/$/ !/
3 s/$/ !/
5 s/$/ !/
8 s/$/ !/
13 s/$/ !/
21 s/$/ !/
It's a simple sed
to add a sed
substitution command for each line number, and sends the resulting script to a file. A few tricks here include using double-quotes to allow the character embedding, and #'s to allow the replacement text to include /'s without creating leaning-toothpick syndrome from all the backslash quoting.
Then run it against your input -
$: sed -i "$(<script)" infile
Which does the work. That pulls the script file contents in for sed
to use, generating:
1 a !
2 b !
3 c !
4 d
5 e !
6 f
7 g
8 h !
9 i
10 j
11 k
12 l
13 m !
14 n
15 o
16 p
17 q
18 r
19 s
20 t
21 u !
22 v
23 q
24 x
25 y
26 z
Let me know if you want to tweak it.
Bash: append text to last line of file
sed '${s/$/%/}' file
Appending a new line to a file, checking whether last character is a newline character or not in Bash
You can use:
x=$(tail -c 1 aFile.txt)
if [ "$x" != "" ]
then echo >> aFile.txt
fi
echo "some line of text" >> aFile.txt
The $(...)
operator removes the trailing newline from the output of the command embedded in it, and the tail -c 1
command prints the last character of the file. If the last character is not a newline, then the string "$x"
won't be empty, so append a newline to the file before appending the new line of text.
Related Topics
Restoring Stdout and Stderr to Default Value
Having Trouble Finding the Method _Kernel_Vsyscall Within the Linux Kernel
How to Join Multiple PDF Pages to a Single Page
Set Environment Variables for Non-Interactive Shell
.Zshrc Config File Syntax Error
Sudoers Nopasswd: Sudo: No Tty Present and No Askpass Program Specified
How to Print a Number in Arm Assembly
Chef Chef-Validator.Pem Security
Linux Get Process Name from Pid Within Kernel
Xclip Does Not Terminate When Tracing It
Xargs Sh -C Skipping the First Argument
How to Export a Modified Kernel Header
How to Use a Seq_File in Linux Kernel Modules