You have already got a sed
answer. Here are a few alternatives:
awk
: Setting single quote as variable q
(for readability) and out
as the record surrounded by single quotes, then printing out
with desired formatting:
awk -v q="'" '{out=q$0q; print out","out}'
bash
: Reading each line, setting up variable out
to contain the line surrounded by single quotes, and finally printing in desired format:
while IFS= read -r line; do out="'$line'"; printf '%s,%s\n' "$out" "$out"; done
python
: Reading each line, and printing in desired format using print()
function:
#!/usr/bin/env python3
with open('file.txt') as f:
for line in f:
line = line.rstrip()
print("'{}','{}'".format(line, line))
perl
: Replacing each line (.*
) with proper formatting, $&
in the replacement stands for the matched portion, in this case the whole line:
perl -pe "s/.*/'$&','$&'/"
Example:
% cat file.txt
abc
xyz
pqr
mno
% awk -v q="'" '{out=q$0q; print out","out}' file.txt
'abc','abc'
'xyz','xyz'
'pqr','pqr'
'mno','mno'
% while IFS= read -r line; do out="'$line'"; printf '%s,%s\n' "$out" "$out"; done <file.txt
'abc','abc'
'xyz','xyz'
'pqr','pqr'
'mno','mno'
% perl -pe "s/.*/'$&','$&'/" file.txt
'abc','abc'
'xyz','xyz'
'pqr','pqr'
'mno','mno'