Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl: add character to begin of a line

Tags:

regex

bash

perl

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

like image 362
Adrian Avatar asked Jul 27 '11 15:07

Adrian


2 Answers

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
like image 102
Aif Avatar answered Oct 01 '22 23:10

Aif


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
like image 40
glenn jackman Avatar answered Oct 01 '22 23:10

glenn jackman