Questions tagged [tr]

tr is a *nix utility for character-level alterations to a stream. tr/// is a Perl operator named after this utility. For the tag used to build HTML tables, please use [html-table]. Use this tag only if your question relates to programming using tr. Questions relating to using or troubleshooting tr command-line options itself are off-topic.

tr (for "translate") is a standard utility in Unix, GNU/Linux, and other systems. It reads from standard input and writes to standard output, making specified character-level alterations.

One alteration it can make is to substitute individual characters, or groups of characters, with specified replacements; for example, tr a-z A-Z "translates" every occurrence of a lowercase ASCII letter to its uppercase counterpart.

Another alteration is to remove characters; for example, tr -d % "deletes" every instance of a percent sign, while tr -s % "squeezes" sequences of repeated percent signs (so that %% or %%% or %%%% or whatnot will become simply %). tr can also complement characters; for example, tr -cd '[:alnum:]' removes all non-alphanumeric characters.

tr/// is a Perl operator named for this utility. (It can also be written y///, after the same operator in sed.) It substitutes individual characters; for example, tr/a-z/A-Z/ translates every occurrence of a lowercase ASCII letter to its uppercase counterpart.

Beginners often try to use tr for replacements which are not character-level alterations. If you don't want to replace every instance of an individual character with something else, this is the wrong tool; perhaps look at sed, or the s/regex/string/ facility in Perl for regular expression (text pattern) replacement.

Links

675 questions
-1
votes
2 answers

Remove row which contains specific delimiter with different number of columns

Want to remove row which contains specific delimiter with different number of columns CPU Load for sdp4 7c:e5:3b:6e:2e:5f:d9:4d:68:4d:d5:57:3a:cb:4d:45. 02:30PM up 1 day, 9:20, 2 users, load average:…
Kalin Borisov
  • 1,091
  • 1
  • 12
  • 23
-1
votes
1 answer

How can "squeeze-repeated" words?

How can "squeeze-repeated" words? similar to "squeeze repeated characters" with tr -s '' I would like to change for example: hello.hello.hello.hello to hello
user2783132
  • 225
  • 1
  • 3
  • 16
-2
votes
1 answer

HTML table each row(tr) is having different column(td) numbers

I am creating one table with pure HTML code (i.e. without any 3rd party library or bootstrap) Here is my code…
-2
votes
1 answer

replacing string with '~'

I am wondering if there is a simple way using sed or awk to replace a string with a '~'. For example, I am hoping to replace any character that is not a letter with '~', so that…
kwaldner
  • 95
  • 1
  • 6
-2
votes
5 answers

From linux command line, how can I remove \n from a particular line to merge two lines together?

Using the command line, how can I transform something like: 1 first line 2 second line 3 third line 4 fourth line extra bit 5 fifth line 6 sixth line into, say: 1 first line 2 second line 3 third line 4 fourth line; extra bit 5 fifth line 6 sixth…
mherzl
  • 5,624
  • 6
  • 34
  • 75
-2
votes
2 answers

How to remove single quotes while running in the shell script

i'm trying to insert commit message for modified files as well no-modified files but commit failes. git commit -m "${commit_mmes}" "${fil}" $(git ls-files | grep "\.txt" | tr '\n' ' ' | awk '{$1=$1};1') git commit -m 'Master Merge-Commit message'…
Vinoth
  • 189
  • 1
  • 1
  • 9
-2
votes
3 answers

replace strings in file1 with empty space if strings not found in file2

This question: https://unix.stackexchange.com/questions/20322/replace-string-with-contents-of-a-file-using-sed replaces a fixed string in file1 with the contents of file 2. I want to do this the other way around plus an inversion. If I have…
brucezepplin
  • 9,202
  • 26
  • 76
  • 129
-2
votes
2 answers

Translate/Convert control characters from 0-1f (hex) to unicode escaping (\u0000 - \u0037) (BASH)

We have a bash script running on prod. Occasionally we receive control characters inside the bash script as output which is sent somewhere else to be rendered. Is there any way using tr/awk/sed or anything else to translate/convert control…
Div
  • 1
  • 1
-2
votes
3 answers

Bash / Shell / Perl - How to replace variable amount of white space with a single space?

Apologies if this has been asked before; I could not find any shell-related answers. I have a text file with white space that has varying amounts of white space in between the strings. Example: chafa    libgusb                      libvirt-glib …
-2
votes
1 answer

Bash Split a String with a Path in it

I have the follow Problem, my file '2018_08_18__Lysto BackUp.plist' looks like…
Kalysto
  • 19
  • 6
-2
votes
3 answers

How can I replace a the pattern ",," with in awk?

I am doing a ldapsearch query which returns the results as follow John Joe jjoe@company.com +1 916 662-4727 Ann Tylor Atylor@company.com (987) 654-3210 Steve Harvey sharvey@company.com 4567893210 (321) 956-3344 ... As you can see between each…
Asghar
  • 7
  • 5
-2
votes
1 answer

Remove newline characters in file

I have a text file with comma seperated values which has newline characters in the column values. So it makes the column data split to next line causing data issues. Sample…
lfc_07
  • 37
  • 5
-2
votes
2 answers

bash shell tr -d -c options combination usage

echo $PATH | tr -d -c : this output is: :::: The value of $PATH is: /import/adams/2/z1/bin-pc.i86.linux:/import/adams/2/z1/bin:/usr/local/bin:/usr/bin:/bin why I get such an output ;;;;? I cannot understand -d -c :. -c option needs two sets, but…
Allen
  • 145
  • 3
  • 10
-2
votes
3 answers

Replacing a specific field using awk, sed or any POSIX tool

I have a huge file1, which has values as follows: a 1 b 2 c 3 d 4 e 5 I have another huge file2, which is colon delimited with seven fields as follows: a:2543:2524:2542:252:536365:54654 c:5454:5454:654:54:87:54 d:87:65:1:98:32:87 I want to search…
1 2 3
44
45