How can I prepend a string to the beginning of each line in a file?
a one-line awk command should do the trick also:
awk '{print "prefix" $0}' file
Add a prefix string to beginning of each line
# If you want to edit the file in-place
sed -i -e 's/^/prefix/' file
# If you want to create a new file
sed -e 's/^/prefix/' file > file.new
If prefix
contains /
, you can use any other character not in prefix
, or
escape the /
, so the sed
command becomes
's#^#/opt/workdir#'
# or
's/^/\/opt\/workdir/'
How to append a string in beginning of each line in file
str.join
inserts a constant string between all elements of the sequence given as argument:
>>> '-'.join(['a', 'b', 'c'])
'a-b-c'
If you want to have it before the first element as well, you can simply use concatenation:
>>> '-' + '-'.join(['a', 'b', 'c'])
'-a-b-c'
... or use a little trick – insert an empty string in the beginning of the sequence:
>>> '-'.join(['', 'a', 'b', 'c'])
'-a-b-c'
In your example, you can use list unpacking to stick with the dense one-liner style:
file2 = (processed_dateTimeStr.join(["", *z.read(filename).decode("utf-8").splitlines(True)])).encode("utf-8")
How to add a string at the beginning of each line in a file
$ awk '{print "03/06/2012|" $0;}' input.txt > output.txt
Takes about 0.8 seconds for a file with 1.3M lines on some average 2010 hardware.
Append String to each line of .txt file in python?
You can read the lines and put them in a list. Then you open the same file with write mode and write each line with the string you want to append.
filepath = "hole.txt"
with open(filepath) as fp:
lines = fp.read().splitlines()
with open(filepath, "w") as fp:
for line in lines:
print(line + "#", file=fp)
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
How do I read a file line by line in VB Script?
When in doubt, read the documentation:
filename = "C:\Temp\vblist.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename)
Do Until f.AtEndOfStream
WScript.Echo f.ReadLine
Loop
f.Close
Insert string at the beginning of each line
Python comes with batteries included:
import fileinput
import sys
for line in fileinput.input(['./ampo.txt'], inplace=True):
sys.stdout.write('EDF {l}'.format(l=line))
Unlike the solutions already posted, this also preserves file permissions.
Related Topics
How to Attach a File Using Mail Command on Linux
Bash Capturing Output of Awk into Array
Why Do We Need a Bootloader in an Embedded Device
Append Date to Filename in Linux
Do I Need -D_Reentrant with -Pthreads
Linux Bash Script to Extract Ip Address
How to Program Linux .Dts Device Tree Files
User-Data Scripts Is Not Running on My Custom Ami, But Working in Standard Amazon Linux
How to Set a Custom Baud Rate on Linux
What Is Path on a MAC (Unix) System
Why Exit Code 141 with Grep -Q
Where Does Eclipse Look for Eclipse.Ini Under Linux
How I Could Add Dir to $Path in Makefile
How to Install Nuget from Command Line on Linux
Is There an Invalid Pthread_T Id