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
-2
votes
2 answers

bash : Remove the specific pattern of characters from a line

The content of the file a.txt is : 10.39.105.16tcp(1234) ABCDEF02 10.49.105.13tcp(521) ABCDEFV01 10.19.105.12tcp(1241) ABCDEFV01 10.29.105.20tcp(1222) ABCDEF03 10.39.104.9tcp(131) ABCDEF21 10.49.104.34tcp(1512) ABCDEF22 …
sr1
  • 251
  • 4
  • 11
-2
votes
2 answers

Convert a DNA sequence into Amino Acid

I am trying to write a bash script that would be able to read DNA sequences (each line in the file is a sequence) from a file, where sequences are separated by an empty line. I am then to find the amino acid that these DNA sequences encode per codon…
Faiz Lotfy
  • 121
  • 1
  • 11
-2
votes
1 answer

jquery find closest tr fails with "undefined"

I have the following code, I want to find the closest tr element but it didn't work.(returns undefined) But I can get closest 'ul', 'li' without problem . Any help is greatly appreciated. Thanks I have added the actual html code…
-2
votes
1 answer

How to use "tr" to replace a digit in a number

My input file contains some series of commands for a number for eg. 13287 13288 13289 I want the following output in all the file. 13281 13282 13283 I am using the following command saved in a script file as I pass the first four digits as…
Mohit Kumar
  • 46
  • 1
  • 2
  • 11
-3
votes
4 answers

If two patterns are found then convert in into single line from start to end pattern

If two patterns are found then convert in into single line from start to end pattern. Example- cat test.txt qwe rty uio {asd fgh jkl zxc} vbn mqw rty Now desired o/p should be qwe rty uio {asd fgh jkl zxc} vbn mqw rty Here First pattern is…
-3
votes
1 answer

Quantitavely replace digit (as counter) with string in sed

Let's say i have the following file: balloons: - 2 - 3 Each number above should represents how many times i want to print the string. So for example I would like to process this to output as following: balloons: - red - red - blue - blue -…
VMold
  • 37
  • 7
-3
votes
3 answers

Why doesn't my table row appear when the button is clicked?

Just trying to have a new drop down row to enter some data on a form. Should be simple, but the drop down shows in the first cell only, even when using colspan=5. Or is there a better way? Thx for any help.
Merle_the_Pearl
  • 1,391
  • 3
  • 18
  • 25
-3
votes
2 answers

breaklines and blank spaces

In file shell I have the following: Looks like: BCTS1 ,07/09/2021 , 09:06:26 ,09:09:26 , 0 horas con 3 minutos I would like it to look…
-3
votes
1 answer

Short way to convert a string or character to a new one in java

I am trying to convert characters in a string into a new character. Essentially it would convert ATCG into TAGC. Is there a shorter way of doing this than writing a for loop and checking each character such as the tr command in bash?
-3
votes
3 answers

Return Non-empty Output: grep httpd | head -1 | tail -c 9

I need some value returned. it can be anything. this code returns " 08:10" if the service is running. ps -eo comm,etime | grep httpd | head -1 | tail -c 9 but what if the service is not running ? service httpd stop and let's try again: ps -eo…
user7423861
-3
votes
2 answers

Exclude the last-but-one field

How to exclude or remove the word before the last word in line: Example: var=1 echo "list $var M" Expected: list 1M
Kalin Borisov
  • 1,091
  • 1
  • 12
  • 23
-4
votes
2 answers

What happens to characters not specified in the first part of Perl's tr/// operator?

I am using Perl for string manipulation and that involves use of the reverse function and of tr to translate my string. The script reads some strings and then performs following: $revread = reverse($newword); $revread =~ tr/TACGN/ATGCN/; So the…
Sudeep
  • 3,015
  • 3
  • 17
  • 8
-4
votes
3 answers

converting complex tr command to sed statement

What is the "best" way to clean up this unix command? (e.g. one clean sed command) cat file.txt | tr '\t' '|' | tr '|\n' '|' | tr -s '|' | tr '"' '\n' | sed "/^|/d" My workflow was something like the following: replace tabs with pipes replace pipe…
Roger
  • 365
  • 2
  • 18
-4
votes
1 answer

Change "" value with different string depends on last line

Change "" value with different string depends on last line. In that case when see in the last line "*" to replace "" with "ls -lrt" and to separate the "*" from the last line when see slash for anything else with "find". Text…
Kalin Borisov
  • 1,091
  • 1
  • 12
  • 23
-5
votes
2 answers

How To: Convert Text Following Title Case Rules in Bash

How to convert string to title case while following rules, not just simply capitalizing every first letter of the word? Sample rule: Capitalize all words, with exception to: Lowercase all articles (a, the), prepositions (to, at, in, with), and…
Majal
  • 1,635
  • 20
  • 31
1 2 3
44
45