4

I have one file. I want to add ' at evry line beginning.also after first word complete I want to put ' again and then , after that I want repeat the same first word in 'same word'. How can I do this ?

e.g.

abc
xyz
pqr
mno

will be converted into

'abc','abc'
'xyz','xyz'
'pqr','pqr'
'mno','mno'

I have thousands of such lines.How can I achieve this ? I am using ubuntu 16.04.

I am getting output as shown below .

enter image description here

What I want is :

enter image description here

Regards.

3
  • Why do you write "first word"? Your lines contain only one word each. If you actual file has more than one word, please update your question to reflext the actual file format.
    – Philippos
    May 2, 2017 at 11:58
  • @Philippos yes, I have only one word in each line. but I want same word with ' ' , after that word. That's why I have added word "first" , next can be second with same pattern. May 2, 2017 at 11:59
  • Please don't post images of text. This makes it impossible to work with actual data May 2, 2017 at 14:47

2 Answers 2

8

Use sed:

sed "s/.*/'&','&'/" filename

s/foo/bar replaces foo by bar. .* matches the whole line, & in the replacement section inserts the whole match.

9
  • doing nothing ! My file is not going to take any single change. What to do ? May 2, 2017 at 12:40
  • You mean, the output of the command is just the file listed? Hard to believe. Or do you want to modify the file directly? In this case add option -i to your GNU sed to edit the file in place.
    – Philippos
    May 2, 2017 at 12:45
  • -i works like magic. Thank @philippos. May 2, 2017 at 12:52
  • also can u please suggest how to avoid new line ? I want both the word with special character in same line. May 2, 2017 at 13:04
  • WHich new line? I don't get any new lines.
    – Philippos
    May 2, 2017 at 13:07
2

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'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .