12

I have a file like this:

AAAA  
BBBB  
CCCC   
DDDD   
etc  

Words in separate lines. I want to add them quotes using tr.
So I tried the following:

1) To add a quote in the start of each word i.e. 'AAAA

  • tr '^\w$' '\'\w$' <file.txt Does not work
  • tr '^\w$' ''\w$' <file.txt Does not work

Both of the above make the command line stuck.

2) To add quote to the end I tried:

tr '\n' ''\n' <file.txt But this does not work and the command line gets stuck.

Why don't these regex work with tr?

4 Answers 4

13

You can't replace multiple things like this with tr. I would use sed to do this instead.

Example

$ sed "s/^/'/" file.txt
'AAAA
'BBBB
'CCCC
'DDDD

Says to find the beginning of each line (^) and replace it with a single quote (').

If you want to wrap the "words" in single quotes you can use this form of the command:

$ sed "s/\(.*\)/'\1'/" file.txt
'AAAA'
'BBBB'
'CCCC'
'DDDD'

This time we're saving anything on the line in a temporary variable (\1). We then replace the line with this '\1'.

This command can be abbreviated to this, use GNU specific switches:

$ sed -r "s/^(.*)$/'&'/" file.txt
'AAAA'
'BBBB'
'CCCC'
'DDDD'
3
  • For adding the ending quote too, I'll modify it to sed -r "s/^(.*)$/'&'/" file.txt. Commented Aug 15, 2013 at 21:22
  • -r is GNU specific. sed "s/.*/'&'/". No need for ^ or $ or to capture the whole pattern. Commented Aug 16, 2013 at 5:44
  • @StephaneChazelas - thanks I've mentioned that in my answer (GNU) and removed the redundant anchor characters.
    – slm
    Commented Aug 16, 2013 at 12:58
5

tr is for transliteration. One popular use of tr is change the case of strings:

$ echo 'this is a test' | tr a-z A-Z
THIS IS A TEST

Some operating systems version of tr, need the a-z to be of the form [a-z], in which case it should also be quote:

$ echo 'this is a test' | tr '[a-z]' '[A-Z]'
THIS IS A TEST 

Info you have the info package installed on your system, the tr page has some very good examples and further explanation of the tr command:

info coreutils 'tr invocation'

For what you are trying to achieve you probably want to use sed, perl or awk.

Perl:

perl -pe "s/^/'/" file

for each line, Match the start of the line (^), replace with '

Sed:

sed "s/^/'/" file

Similar to the perl example

Awk:

awk -v x="'" '{ print x$0 }' foo

Assign the variable x to be ' and for each line print ' and then the whole line

3

No, tr is only for substituting characters one by one. You cannot add characters this way.

You can use something like perl to do so:

perl -pe "s/\w+/'$&'/"

With tr you can only for example substitute one kind of quote by another kind:

tr \" \'

Those backslashes are only to tell your shell to send the character as it is and not trying to interpret it itself.

Those will not work:

tr '"' '''
tr '"' '\''

The first argument ('"') is fine it is exactly the same than \". The second argument in both cases does not work: you cannot quote single quotes using single quotes as the second quote ends the quoting and the third quote opens a new one which is missing. (therefore your shell opens a new line to wait for the missing end quote.)

But all of this is just shell stuff und has nothing to do with tr.

3

The fastest solution is generally using the simplest one-task utility.

tr is a simple utility, but for something else: transliteration (turning given characters into other ones), removing given characters or squeezing characters.

More generic utilities like sed, awk or perl (by order of size) will do it, but here, you've got a one-task utility that would do it well as well: paste:

paste -d\' /dev/null file.txt /dev/null

Or:

:|paste -d\' - file.txt -

You must log in to answer this question.

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