Adrian
Adrian

Reputation: 75

Perl: add character to begin of a line

I want to add a " character to the begin of every line in a text file. Is there any simple solution?

Upvotes: 5

Views: 6467

Answers (3)

glenn jackman
glenn jackman

Reputation: 247210

Another couple of suggestions:

just in the shell:

tmp=$(mktemp)
while read -r line; do printf '"%s\n' "$line"; done < filename > "$tmp" &&
mv "$tmp" filename

ed:

ed describes.sql.bak <<'END'
1,$s/^/"/
w
q
END

Upvotes: 2

TLP
TLP

Reputation: 67920

I would consider one of these ways:

perl -pi.bak -e 's/^/"/' inputfile.txt

Edit file in place, saves a backup in "inputfile.txt.bak".

perl -pe 's/^/"/' inputfile.txt > outputfile.txt

Use shell redirection to print the output to a new file.

Upvotes: 1

Aif
Aif

Reputation: 11220

perl -p -e 's/^/"/' myfile should do it!

$ cat myfile 
0
1
2
3
4
5
6
7
8
9
10
$ perl -p -e 's/^/"/' myfile
"0
"1
"2
"3
"4
"5
"6
"7
"8
"9
"10

Upvotes: 6

Related Questions