5

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

1
  • 1
    If perl is not mandatory I would suggest using sed: sed -i 's/^/"/' file Commented Jul 27, 2011 at 16:14

3 Answers 3

6

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
Sign up to request clarification or add additional context in comments.

3 Comments

Or perl -pe '$_ = qq{"$_}' myfile
Thanks, the /^/ indicate the begin of a line?
@Adrian: yes, ^ is the beginning of line, and $ then end.
2

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.