Bash: add string to the end of the file without line break
sed '$s/$/yourText2/' list.txt > _list.txt_ && mv -- _list.txt_ list.txt
If your sed implementation supports the -i option, you could use:
sed -i.bck '$s/$/yourText2/' list.txt
With the second solution you'll have a backup too (with first you'll need to do it manually).
Alternatively:
ex -sc 's/$/yourText2/|w|q' list.txt
or
perl -i.bck -pe's/$/yourText2/ if eof' list.txt
How to append string at the end of text file without leading line break?
You can use sed:
sed -i~ '$ s/$/ bar/' test.txt
$
is an address that means "the last line". It applies to the following command.s/$/ bar/
replaces$
, i.e. the end of line, bybar
.-i
(if supported) will change the file in place, leaving the original as a backup (renamed totest.txt~
). If your sed doesn't support it, redirect the output to a new file and move it over the old one.
add a string to a file without line break
The -n
option controls whether a newline is added at the end of the echo. That very first echo "hello"
writes hello\n
to the file; that's where the newline is coming from.
To add a kernel option try one of these. Each of them searches for the kernel
line in grub.conf and appends " elevator=noop" to the end.
sed '/kernel/ s/$/ elevator=noop/' /etc/grub.conf
awk '$1 == "kernel" { $0 = $0 " elevator=noop" } { print } ' /etc/grub.conf
Append to protected file without creating a new line
You can't. The newline is already there, you can't remove things by appending.
Instead, you would have to replace the content of the file with the changed line.
The easiest way to do so would likely be sed
:
sudo sed -i -e 's/$/ fastboot noswap ro/' /boot/cmdline.txt
or paste
:
paste /boot/cmdline.txt <(echo fastboot noswap ro) | sudo tee /boot/cmdline.txt
Bash shell script: appending text to a specific line of an existing file without line break
awk '/Options=/ && ! /nodev/ {print $0 ",nodev"; next};1' file
no need to get the line number, just append the ",nodev" to the corresponding line
Is there a way to add text to the end of a string in a conf file in linux?
You can use sed for this purpose.
sed 's/random/& Hello World/' file
to append text to the matched string.
You can use ^random$
to make sure the entire line is matched, before appending.
If you need to modify the file directly, you can use the -i
flag, which facilitates in-place editing. Further, using -i.bak
creates a backup of the original file first before modifying it, as in
sed -i.bak 's/random/& Hello World/' file
The original copy of the file can be found in file.bak
More about sed
: https://www.gnu.org/software/sed/manual/sed.html
How to add newline character at the end of many files
Short and quick tee
But adding a newline at end of each files. For strictly adding a newline at end of files where there are not, please go to second part of this answer!
tee
is the tool you're searching for:
Simply:
tee -a <<<'' file1 file2 ...
or
find /path -type f ! -empty -name '*.php' -exec tee -a <<<'' {} +
Warning: Don't miss -a
option!
It's very quick, but add a newline on each files.
(You could whipe in a second command like sed '${/^$/d}' -i file1 file2 ...
all empty last lines in all files. ;)
(Warning again: I insist: if you miss -a
flag for tee
command, this will shortly and quickly replace the content of each file found by a newline!! So all your files will become empty!)
Some explanation:
from man tee
:
NAME
tee - read from standard input and write to standard output and files
SYNOPSIS
tee [OPTION]... [FILE]...
DESCRIPTION
Copy standard input to each FILE, and also to standard output.
-a, --append
append to the given FILEs, do not overwrite
- So
tee
will reproduce, appending (because of optiona
), to each file submited as argument, what become on standard input. - bash feature: "
here strings
" (seeman -Pless\ +/Here.Strings bash
), you could usecommand <<<"here string""
in replacement ofecho "here string"| command
. For this, bash add a newline to submited string (even empty string:<<<''
).
Slower, but stronger
fix newline character after the last line (if not present)
Stay very quick because of limited forks, but one fork to tail -c1
have to be done for each files anyway!
while IFS= read -d '' -r file
do
IFS= read -d "" chr < <(
exec tail -c1 "$file"
)
if [[ $chr != $'\n' ]]
then
echo >> "$file"
fi
done < <(
find . -type f ! -empty -name '*.php' -print0
)
Could by written more compact:
while IFS= read -d '' -r file; do
IFS= read -d "" chr < <( exec tail -c1 "$file" )
[[ $chr != $'\n' ]] && echo >> "$file"
done < <( find . -type f ! -empty -name '*.php' -print0 )
find -print0
print each filename separated by a null character\0
.IFS= read -d '' -r file
don't consider special characters nor spaces, so$file
could hold any kind of filename, event containing spaces or accented characters.exec
tell bash to executetail
as subprocess, avoid default second fork runningtail
in asubsubprocess
.tail -c1
read last caracter of$file
IFS= read -d "" chr
store last caracter into$chr
variable.
In Bash, how do I add a string after each line in a file?
If your sed
allows in place editing via the -i
parameter:
sed -e 's/$/string after each line/' -i filename
If not, you have to make a temporary file:
typeset TMP_FILE=$( mktemp )
touch "${TMP_FILE}"
cp -p filename "${TMP_FILE}"
sed -e 's/$/string after each line/' "${TMP_FILE}" > filename
Related Topics
Awk One Liner Select Only Rows Based on Value of a Column
Importing Shapefiles in Postgresql in Linux Using Pgadmin 4
How to Read a .Properties File Which Contains Keys That Have a Period Character Using Shell Script
How to Check the Version of Openmp on Linux
Shell Script to Get the Process Id on Linux
Add Text Between Two Patterns in File Using Sed Command
Getting Disconnection Notification Using Tcp Keep-Alive on Write Blocked Socket
Help with Understanding a Very Basic Main() Disassembly in Gdb
Archiving Hidden Directories with Tar
How to Sort Files Numerically from Linux Command Line
How to Exclude Absolute Paths for Tar
Moving Multiple Files Having Spaces in Name (Linux)
How to Find Out What Group a Given User Has