Skip to main content
Share Your Experience: Take the 2024 Developer Survey
32 votes
Accepted

Turning separate lines into a comma separated list with quoted entries

You can add quotes with sed and then merge lines with paste, like that: sed 's/^\|$/"/g'|paste -sd, - If you are running a GNU coreutils based system (i.e. Linux), you can omit the trailing '-'. If ...
zeppelin's user avatar
  • 3,822
28 votes

Can I use `sed` to translate characters like with `tr`?

sed has the y command that works just like tr at least in most implementations: $ echo 'foobartest' | sed 'y/ots/u.x/' fuubar.ex. The y command is part the POSIX sed specification, so it should work ...
n.st's user avatar
  • 8,138
26 votes
Accepted

Strange behaviour of tr using ranges

you have a file named o in current directory foo> ls foo> echo "abcdefghijklmnopqrstuvwxyz1234567890"|tr -d [a-z] 1234567890 foo> touch o foo> echo "abcdefghijklmnopqrstuvwxyz1234567890"|...
Archemar's user avatar
  • 31.6k
20 votes

Is it possible to replace duplicates of a character with one character using tr

Using tr -s: $ echo 'ab ### cde fghi## jklm' | tr -s '#' ab # cde fghi# jklm -s Squeeze multiple occurrences of the characters listed in the last operand (either string1 or string2) in the ...
jesse_b's user avatar
  • 37.3k
18 votes
Accepted

Main difference between tr (translate) to sed and awk

Yes, tr is a "simple" tool compared to awk and sed, and both awk and sed can easily mimic most of its basic behaviour, but neither of sed or awk has "tr built in" in the sense that ...
Kusalananda's user avatar
  • 337k
17 votes
Accepted

Pulling IP address from ping command with sed?

ping is for checking whether a host is up or down based on ICMP response, it is never the right tool for only resolving IP address, there are dedicated tools for that. You should look at dig, host, ...
heemayl's user avatar
  • 56.5k
17 votes

tr a A < /etc/hosts | sort -r |pr -d > /etc/hosts

The expected outcome is a blank /etc/hosts file. The redirection > /etc/hosts occurs and truncates the file before the programs start running and tr begins to read from the file. To write the ...
Michael Homer's user avatar
17 votes
Accepted

What do the dots in this tr command do: tr .............A-Z A-ZA-Z <<< "JVPQBOV" (with 13 dots)

It works as follows: SET1-> .............ABCDEFGHIJKLMNOPQRSTUVWXYZ SET2-> ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLM So tr will translate SET1 to SET2. This is equivalent to first one because ...
Prvt_Yadav's user avatar
  • 5,872
17 votes

Why doesn't the command tr "\'\\\"\?\!" "01234" work?

Not just the shell, but tr itself also interprets backslash as a special escaping character, see its manual for details. So you need to make sure that tr receives literal \\ (two backslashes) when you ...
egmont's user avatar
  • 5,956
16 votes
Accepted

Why does the tr command not read from file?

The UNIX philosophy advocates for "small, sharp tools", so the answer is that reading from a file would be bloat contrary to the UNIX philosophy. As to why wc, grep, sed, awk, etc do read from files, ...
user1133275's user avatar
  • 5,562
15 votes
Accepted

tr analog for unicode characters?

GNU sed does work with multi-byte characters. So: $ echo é½Æ | sed 'y/é½Æ/ABŒ/' ABŒ It's not so much that GNU tr hasn't been internationalised but that it doesn't support multi-byte characters (like ...
Stéphane Chazelas's user avatar
14 votes

How do I use tr command to replace: full stops, exclamations and question marks followed by a space, with newlines?

You don't. tr is not designed for that. It's designed to transliterate a set of single characters into another set of single characters, e.g., A-Z into a-z. Using tr with .␣ (a dot and a space) and \...
Kusalananda's user avatar
  • 337k
14 votes

Replace any number of tabs and spaces with single new line in Linux?

etopylight was almost right: tr -s ' \t' '\n' because the question asks to replace tabs, too.
G-Man Says 'Reinstate Monica''s user avatar
14 votes
Accepted

Explanation needed on how I can repeat a character in POSIX shell

In short, printf %100s will print 100 spaces, and tr " " "=" will convert those spaces to equal signs, effectively printing 100 equal signs. Breaking it down: printf is a shell built-in. It ...
filbranden's user avatar
  • 21.8k
14 votes

What do the dots in this tr command do: tr .............A-Z A-ZA-Z <<< "JVPQBOV" (with 13 dots)

As @Prvt_Yadv says in their answer, it works because there are 13 dots. The sets are First set: .............ABCDEFGHIJKLMNOPQRSTUVWXYZ Second set: ...
user7761803's user avatar
14 votes
Accepted

tr command unable to process colour output piped from grep

To output matches in colour, grep writes colouring escape sequences before and after the match. Those are instructions to the terminals to change their background and/or foreground colour. It's ...
Stéphane Chazelas's user avatar
13 votes

Replace multiple spaces with one using 'tr' only

If you want to squeeze "white space" you will want to use tr's pre-defined character sets ":blank:" (horizontal whitespace tab and space) or ":space:" (verical whitespace) : /bin/echo -e "val1\t\...
user3183018's user avatar
12 votes

Turning separate lines into a comma separated list with quoted entries

Using awk: awk 'BEGIN { ORS="" } { print p"'"'"'"$0"'"'"'"; p=", " } END { print "\n" }' /path/to/list Alternative with less shell escaping and therefore more readable: awk 'BEGIN { ORS="" } { ...
phk's user avatar
  • 5,973
12 votes
Accepted

How to translate Unicode characters?

Both GNU and BSD sed are multibyte-aware in appropriate locales, and the y command is analogous to tr: $ echo hello | sed -e 'y/abcdefghijklmnopqrstuvwxyz/abcdefghijklmnopqrstuvwxyz/' hello This ...
Michael Homer's user avatar
12 votes

how to use tr to replace multiple sets?

With sed, you need to add g so that it will replace all occurrences on each line: sed 's/[0-4]/A/g;s/[5-9]/B/g' With tr, you can either chain two trs: tr '0-4' A | tr '6-9' B or provide the ...
Stephen Kitt's user avatar
12 votes

How can I transliterate some characters 1-to-1, leave some unchanged, and replace others with the same target character?

I think you could use the fact that for many practical implementations, if a character repeats in the first set to tr, the last instance takes effect. Combined with the repeat syntax, you could do it ...
ilkkachu's user avatar
  • 140k
12 votes
Accepted

Why does 'tr' with '-c' option and set 2 extension add an unwarranted character to the end?

That's because echo adds a newline to the end of what you tell it to print. The same is true if you use a here-string instead. So echo "hello" actually prints hello\n: $ echo hello | od -c ...
terdon's user avatar
  • 243k
11 votes

Explanation needed on how I can repeat a character in POSIX shell

The printf command uses its first argument as a format for printing its subsequent arguments. printf %100s prints out its arguments padded to 100 characters wide, using spaces (on the left). There is ...
Michael Homer's user avatar
11 votes
Accepted

If any first-in-line English letter is lowercase, uppercase it

Use the \U function in GNU sed. s/^\([a-z]\)/\U\1/ so this captures a single character at the start of the line if it is lowercase, and upper cases it. As the \U leaves other things alone, this ...
icarus's user avatar
  • 18.1k
10 votes

Replace any number of tabs and spaces with single new line in Linux?

Basically, you could do it in GNU sed: sed 's/\s\+/\n/g' There you go...
thecarpy's user avatar
  • 3,946
10 votes
Accepted

How to use the tr REPEAT modifier to replace a character with a repeated character?

I'm afraid you are misunderstanding the meaning of the "repeat" option. tr, as you correctly infer, is used to translate character sets. You call it with option tr set1 set2 The idea is ...
AdminBee's user avatar
  • 22.8k
9 votes

Can I use `sed` to translate characters like with `tr`?

If like in your case, you're transliterating characters without changing their size (anyway, some implementations like GNU tr only support single-byte characters), you can do: tr 'ots' 'u.x' < ...
Stéphane Chazelas's user avatar
9 votes

Main difference between tr (translate) to sed and awk

sed has a y command that is analogous to tr: y/string1/string2/ Replace all occurrences of characters in string1 with the corresponding characters in string2. $ echo 'a big cat' | sed -e 'y/...
Michael Homer's user avatar
9 votes

Strange behaviour of tr using ranges

What is happening The shell (bash) sees the argument [a-z]. That's a wildcard pattern (a glob), which matches any lowercase letter¹. Therefore the shell looks for a file name that matches this ...
Gilles 'SO- stop being evil''s user avatar
9 votes
Accepted

How to concatanate the result of two Linux commands

Or do it inside-out: printf "%s°C\n" "$(wget -q -O- "http://meteocentre.com/montreal/home_e.html" | grep -oP '(?<=Tn= ).*(?=&deg)')" So that the results from wget...|grep... are collected as ...
Jeff Schaller's user avatar
  • 67.5k

Only top scored, non community-wiki answers of a minimum length are eligible