Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [tr]

translate or delete characters

1
vote
2answers
44 views

find n most frequent words in txt file using the terminal but with the cat, “grep .” and tr -d “/r” commands

I would like to find n most common words in a text file but with the following conditions: cat to get the file. grep . to get the words. tr -d '/r' to get rid of new lines. I've managed to get ...
0
votes
1answer
45 views

Remove punctuation except a particular symbol in a particular position

I'm using tr to parse text and do several tasks like lower-case all words, remove punctuation and multiple spaces. The end result should be clean lower-case text with only alpha-numeric characters, ...
18
votes
3answers
1k views

What do the dots in this tr command do: tr …A-Z A-ZA-Z <<< “JVPQBOV” (with 13 dots)

I want to use tr to do some rot13 transformation. I can beautifully understand this command: tr A-Za-z N-ZA-Mn-za-m <<< "URYC ZR CYRNFR" which output is HELP ME PLEASE, but I can't figure ...
7
votes
1answer
287 views

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

I'm trying to take all occurrences of # and if they are consecutive replace them with just one #. e.g. ab ### cde fghi## jklm would become: ab # cde, fghi# jklm My initial goal was to replace ...
0
votes
1answer
153 views

How to replace double new line with single using tr?

Given a text, $ cat a.txt Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam at magna sed libero accumsan ultrices. Proin varius tortor risus, at pulvinar quam auctor vitae. Etiam iaculis ...
8
votes
2answers
438 views

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

The following answer on Stack Overflow, How can I repeat a character in bash? imposes one plausible way of POSIX-ly repeating a single character, as follows. In this example let's use the equal sign ...
4
votes
3answers
93 views

Replacing all instances of string containing hyphen

Running the following command: echo "hello-world" | tr "hello-world" "Hello World" Returns Heddd-ddddd. I want it to return Hello World. I believe this is because tr is interpreting the SET1 ...
0
votes
1answer
305 views

Removing unseen junk characters from the file in unix

I am trying to remove unseen junk characters from the file which can be seen using cat -v . I got the script below in google but i am curious to know how the below command works . tr -cd '\11\12\40-\...
-1
votes
2answers
45 views

Subtract string value in col1 from col2 and populate col2

I have a data file that looks like this: MASTER APP SET STATUS max_mstr_ref max_mstr_ref_horizon_biz pp_script true max_mstr_ref ...
0
votes
1answer
34 views

replace first semicolon occurence

Hi I have a logfile and I want to replace the first semicolon occurence, like: head1;head2;head3;head4;head5 blabb;blabl;;blab;blabl;;bla Desired output: head1;head2;head3;head4;head5 blabb;...
2
votes
4answers
1k views

How to concatanate the result of two Linux commands

Is there an easy way to concatenate the result of two Linux commands, in one line (i.e., without using variables)? I pull the local outdoor temperature from a nearby weather station. The result ...
0
votes
3answers
74 views

adding a new line character to a row

I'd like to extract the 2nd field and transpose it to become a row cat > temp1 1 73 2 74 3 75 My approach : cut -f2 temp1 | tr '\n' '\t' > temp2 The result : temp2 73 74 75 ...
0
votes
2answers
428 views

sed: remove extra whitespace to single whitespace between strings while leaving leading tabs intact

I have a code: 1 /** 2 a b c 3 **/ 4 int main() { 5 int x; 6 if ( condition) { 7 return x; 8 } 9 } I need to change multiple whitespaces between ...
-2
votes
3answers
303 views

Change all but first ',' to “<COMMA>” for each row in file (bash) [duplicate]

I am using bash and have a csv file (dat.csv) that needs to be only two columns (App, Blurb) of data but due to many ',' on each row it is becoming MANY columns. EXAMPLE OF PROBLEM csv.dat: App , ...
0
votes
1answer
42 views

How to delete whitespaces in columns?

I want to filter some columns of a file, it has 46 in total. I only want to filter from the 25 until the 46, by the next way: Desired Output: Column25 Column26 ... Column47 Column25 Column26 ... ...
2
votes
4answers
41 views

How to reformat named.conf's zone blocks into single lines?

For reasons I won't go into here, I need to make a copy of an old CentOS 5.10 server's named.conf file and perform some text manipulation on the copy. The final result should consolidate each zone's ...
0
votes
4answers
72 views

make row to column n insert newline in every last row

i have file.txt john, 5901, open lina, 2401, open jody, 5401, open I want to insert a new line every last row, The result should be : john,5901,open lina,2401,open jody,5401,open what i try ...
3
votes
1answer
276 views

Remove multiple strings from file on command line, high performance [closed]

Is there an elegant, high-performance one-liner way to remove multiple complete strings from an input? I process large text files, e.g., 1 million lines in inputfile, and 100k matching strings in ...
10
votes
2answers
926 views

Strange behaviour of tr using ranges

I have one particular server that is exhibiting strange behaviour when using tr. Here is an example from a working server: -bash-3.2$ echo "abcdefghijklmnopqrstuvwxyz1234567890"|tr -d [a-z] ...
1
vote
2answers
50 views

Compile list of words from list of files

How would one take on this task? I've tried to do it with tr, and come up with this (with help of other similar question): tr -c '[:alnum:]' '[\n*]' < long_concatenated_file but.. Its a bit of a ...
3
votes
5answers
209 views

Split text in specific order using common string “Message”

I have a text file with following contents $ cat foo.txt some text email@id.com 8903457923 2018-02-09 07:12 (Asia/Kolkata) again some text over here some more text again Message some text email@id....
2
votes
1answer
203 views

I cannot understand what -c parameter does in tr command in Ubuntu GNU/Linux even though I read the manual

I try to understand what -c option does in tr command according to man tr command it says: -c, -C, --complement use the complement of SET1 But I cannot understad what the doc mean ...
1
vote
1answer
2k views

Using tr to replace text with repeated characters

I tried using tr to replace a variable name in a file, and I noticed some behavior I can't figure out if the variable has a repeated character. For example: echo "aab" | tr 'aab' 'xyz' gives the ...
1
vote
1answer
166 views

How does the “1<>file” shell input/output redirection work?

I saw this piece of code on another question, but I wasn‘t able to comment on the answer so I‘m asking here. tr '\n' '\0' < file 1<> file I‘m interested in the 1<> part, never seen ...
2
votes
1answer
680 views

How to translate Unicode characters? [duplicate]

I'm trying to convert some characters to the fullwidth form like this tr 'abcdefghijklmnopqrstuvwxyz' 'abcdefghijklmnopqrstuvwxyz' However, it doesn't work. I did a search and it turns out tr doesn'...
2
votes
3answers
2k views

Main difference between tr (translate) to sed and awk

AFAIC both sed and awk are general purpose text processing utilities with whom a user can get quite similar results, in a slightly different syntax: With both, a user could add, replace/translate and ...
9
votes
1answer
731 views

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

In this line: tr a A < /etc/hosts | sort -r |pr -d > /etc/hosts what would be the expected outcome? I know lowercase 'a' would change to uppercase 'A' but what's next? Would the original file ...
0
votes
3answers
84 views

Can anyone explain to me how this script works?

I was looking for a script that changes the filename and directory name to lowercase and replaces any white spaces with "-". I managed to find the script below inside another script but I do not fully ...
1
vote
1answer
112 views

invalid source string in KSH but not in BASH

I try to generate a random string using this command: tr -dc 'A-Za-z0-9!#$%&()*+,-./:;<=>?@[\]^_`{|}~' </dev/urandom | head -c16; echo; I need to put that command into a function ...
6
votes
5answers
3k views

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

I have a very large word list that has been damaged, maybe by improper vim exit or memory exhaustion? Everything in my word list should be left-justified. But now it is like this: Cat ...
13
votes
3answers
3k views

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

I would like to replace a set of characters with corresponding characters from another set, something like this: original set: ots "target" set: u.x foobartest → fuubar.ex. Translations/...
7
votes
3answers
1k views

tr analog for unicode characters?

I need internationalized utility that does the same thing as tr: gets character from stream and substitutes it with a corresponding character. Not a particular case solution like lower-to-upper, but a ...
1
vote
1answer
384 views

Why does tr -sc 'A-Za-z' '[\12*]' includes empty line?

I'm working through Unix For Poets, and trying make a file containing all words/tokens in the Bible. However, when using tr, as suggested, this includes the empty string. See example below: > tr -...
0
votes
3answers
2k views

Remove spaces between two words ONLY

I have the following "/x/x y/asfas/g.pdf " "fdfdf "/x/y/yy y/d.doc " "fdfdf etc.. I want to remove the spaces between the extension and the " ONLY when I used tr to remove trailing ...
3
votes
1answer
50 views

How to prefix backslash to output [duplicate]

This is my input file. 3400,2 3400,27 3400,DC 3400,TF 5600,KA 5600,KN 5600,TF 0313,EX 0313,EY 0313,EZ 0313,FD 0313,FE 0313,JB 0313,JC 0313,KG 0313,T8 I want my output to be 400,2\|3400,27\|3400,DC\|...
1
vote
2answers
886 views

how to use tr to delete a octal characters sequence?

I'm trying to delete this: --More-- from a .dat file, this is a fragment of the file to give an example: Device ID: N7K-LAN(JAF1651ANDL) IP address: 148.228.4.192 Interface: GigabitEthernet0/1,...
2
votes
2answers
322 views

Is there any library or tool to “translate” string from one keyboard layout to another?

I want to write simple bash script which translates input between different keyboard layout. For example, if my system has Russian and English layouts, and I'm passing 'Руддщ' string to this script, ...
0
votes
1answer
270 views

Replace content that have newlines using sed

I have markdown README file that have html markers like this: <!-- CONTRIBUTORS-START --> <!-- CONTRIBUTORS-END --> and I want to replace the text inside (it may already have stuff) with ...
1
vote
3answers
282 views

Make statistic of bigrams [closed]

I have a text file, which has one word in each line and I would like to do bigrams and count repetitions (statistics) of each bigram. My approach: cat TEXTEN1.txt | tr '*\n' '*? *\n' I would like ...
-1
votes
2answers
455 views

How to run `tr` (translate command) and mute its output?

I'm using tr and sed command to replace text in my file like this tr '\n' ' ' < afile.txt | sed '$s/ $/\n/' and as discussed here. Though running that on a big file will get my console spammed with ...
0
votes
1answer
509 views

SED to find and replace exact word beginning with $

My php file has some defined variables with $downtime_hosts, what i need is a single command to just find and replace entire $downtime_hosts = 8; into $downtime_hosts = 15; and without affecting the ...
3
votes
2answers
2k views

Convert file contents to lowercase and store result in same file [duplicate]

I use the following command to convert my input file contents to lowercase tr A-Z a-z < input > output This command works fine. But when I try to store the output in input file itself, it is ...
13
votes
8answers
7k views

Turning separate lines into a comma separated list with quoted entries

I have the following data (a list of R packages parsed from a Rmarkdown file), that I want to turn into a list I can pass to R to install: d3heatmap data.table ggplot2 htmltools htmlwidgets ...
6
votes
3answers
919 views

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

I have text that goes like this: I am happy. I am here. How are you, Meg? I want this to be: I am happy. I am here. How are you, Meg? For full stops I tried tr -s '. ' '\n' <file.txt >out....
1
vote
1answer
414 views

Escaping the dot character in tr -dc

I'm trying to rename files using tr. The following command nearly works: for file in ./*; do mv -v "$file" $(echo "$file" | tr ' []' '-' | tr -dc 'A-Za-z0-9_-' | tr '[:upper:]' '[:lower:]'); done ...
0
votes
8answers
4k views

Replace comma with newline following with just a command and space

I have a list like so. blue-image1.jpg, blue-image2.jpg, blue-image3.jpg red-image1.jpg, red-image2.jpg, red-image3.jpg It's was originally a csv format but I added newlines because they needed a ...
4
votes
1answer
1k views

How do I turn accented lowercase letters to uppercase? - Using the 'tr' command [duplicate]

I'm tryed with: $ echo 'Ciência' | tr 'a-z' 'A-Z' CIêNCIA But the 'ê' character does not change, obviously because it is accentuated. Then I tried with POSIX classes: echo "Ciência" | tr '[:lower:]'...
10
votes
1answer
1k views

Why does the tr command not read from file?

I looked up a Unix book, the man and the wikipedia page for tr but could not find a reason why it was designed/implemented in such way that it does not read from file but strictly only from the ...
1
vote
1answer
2k views

Iterate over files in a folder

I have a folder containing files, and I need to iterate over them using unix command "tr" having as output the same name as before (if is possible) (the code below doesn't work): FILES=/root/Desktop/...
1
vote
2answers
2k views

tr command has no effect when used in $() and saved in a variable

If I run in the CLI: curl time.com | sed -n 's/.*href="\([^"]*\).*/\1/p' | tr " " "\n" then, as expected, I get a list of sanitized links from the page to STDOUT, each on new line. However, when I ...