Hot answers tagged

150

If your input only contains ASCII characters, you could use tr like: tr A-Z a-z < input or (less easy to remember and type IMO; but not limited to ASCII latin letters, though in some implementations including GNU tr, still limited to single-byte characters, so in UTF-8 locales, still limited to ASCII letters): tr '[:upper:]' '[:lower:]' < input if ...


131

With tr, use the squeeze repeat option: $ tr -s " " < file ID Name 1 a 2 b 3 g 6 f Or you can use an awk solution: $ awk '{$2=$2};1' file ID Name 1 a 2 b 3 g 6 f When you change a field in record, awk rebuild $0, takes all field and concat them together, separated by OFS, which is a space by default. That will squeeze sequences of space and tabs (and ...


51

You can use paste -s -d ' ' file.txt: $ cat file.txt one line another line third line fourth line $ paste -s -d ' ' file.txt one line another line third line fourth line


47

In Bash, you can use Bash's built in string manipulation. In this case, you can do: > text="some text with spaces" > echo "${text// /}" sometextwithspaces For more on the string manipulation operators, see http://tldp.org/LDP/abs/html/string-manipulation.html However, your original strategy would also work, your syntax is just a bit off: > ...


39

grep is a text processing tool. It expects their input to be text files. It seems that the same goes for tr on macOS (even though tr is supposed to support binary files). Computers store data as sequences of bytes. A text is a sequence of characters. There are several ways to encode characters as bytes, called character encodings. The de facto standard ...


36

It's your locale and tr problem. Currently, GNU tr fully supports only single-byte characters. So in locales using multibyte encodings, the output can be weird: $ </dev/urandom LC_ALL=vi_VN.tcvn tr -dc '[:print:]' | head -c 64 `�pv���Z����c�ox"�O���%�YR��F�>��췔��ovȪ������^,<H ���> The shell will print multi-byte characters correctly, but GNU ...


35

That's a known (1, 2, 3, 4, 5, 6) limitation of the GNU implementation of tr. It's not as much that it doesn't support foreign, non-English or non-ASCII characters, but that it doesn't support multi-byte characters. Those Cyrillic characters would be treated OK, if written in the iso8859-5 (single-byte per character) character set (and your locale was ...


31

Using vim, it's super simple: $ vim filename gg0guGZZ Opens the file, gg goes to the first line, 0, first column. With guG, lowers the case of all the characters until the bottom of the file. ZZ saves and exits. It should handle just about anything you throw at it; it'll ignore numbers, it'll handle non ASCII. If you wanted to do the opposite, turn the ...


28

Just use column: column -t inputFile Output: ID Name 1 a 2 b 3 g 6 f


28

sed has the y command that works just like tr: $ echo 'foobartest' | sed 'y/ots/u.x/' fuubar.ex. The y command is part the POSIX sed specification, so it should work on just about any platform. And since it's sed, you can have it replace a file with its edited version, sparing you the bothersome temp file business (provided your implementation of sed ...


27

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 you input data has DOS-style line endings (as @phk suggested), you can modify the command as follows: sed 's/\r//;s/^\|$/"/g'|paste -sd, -


25

With sed, you can do: sed 's/\r$//' The same way can do with tr, you only have to remove \r: tr -d '\r' although this will remove all instances of \r, not necessary followed by \n.


25

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"|tr -d [a-z] abcdefghijklmnpqrstuvwxyz1234567890 shell will expand [a-z] string if a match is found. This is called pathname expansion, according to man bash ...


20

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 input into a single instance of the character. This occurs after all deletion and translation is completed. Your original problem could have ...


18

I suppose that your charmap from the locales is UTF-8, so that you'll have problems on binary files. Just switch to C locale: LC_ALL=C tr '\r' '\n' < target-file | LC_ALL=C grep search-string


18

I like dd for this, myself. <<\IN LC_ALL=C 2<>/dev/null \ dd conv=lcase hi Jigar GANDHI jiga IN ...gets... hi jigar ghandi jiga The LC_ALL=C is to protect any multibytes in input - though any multibyte capitals will not be converted. The same is true for (GNU) tr - both apps are prone to input mangling in any non-C locale. iconv can be ...


17

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 output into /etc/hosts, you could either work with a copy of the file (or move your output file into place afterwards), or use the sponge command from moreutils, ...


17

It works as follows: SET1-> .............ABCDEFGHIJKLMNOPQRSTUVWXYZ SET2-> ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLM So tr will translate SET1 to SET2. This is equivalent to first one because it is also shifting by 13 units as there 13 dots. To include the lower case letters, you'll have to arrange them in SET1 with a similar offset, i.e.: ............


17

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 want to replace backslashes. This might be done e.g. by char=...\\\\... in the shell, this part doesn't need further explanation since you understand correctly ...


15

All of: tr '[:lower:]' '[:upper:]' (don't forget the quotes, otherwise that won't work if there's a file called :, l, ... or r in the current directory) or: awk '{print toupper($0)}' or: dd conv=ucase are meant to convert characters to uppercase according to the rules defined in the current locale. However, even where locales use UTF-8 as the character ...


15

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, nslookup -- whatever suites you the best. Here's a dig output: % dig +short google.com 123.108.243.57 123.108.243.51 123.108.243.54 123.108.243.48 123.108.243....


14

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, the answer is that they all have features that require more than one input or input selection or otherwise require direct access to the files. As tr is not ...


14

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 typically takes two or more arguments, the first of which is a "format string" and the rest will be used to fill up placeholders in that format string. Once that ...


14

As @Prvt_Yadv says in their answer, it works because there are 13 dots. The sets are First set: .............ABCDEFGHIJKLMNOPQRSTUVWXYZ Second set: ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ The dot isn't a special character, so if you have a dot in your input, it will be translated too. In the version of tr that I have, it is the last ...


13

I would use translate command tr eg. tr ab ba < input_file


13

You can also use Perl 5: perl -pe '$_=lc' temp The option -p tells perl to run the specified expression once for each line of input, printing the result, i.e. the final value of $_. -e indicates that the program will be the next argument, as opposed to a file containing the script. lc converts to lowercase. Without an argument, it will operate on $_. And $...


13

Commands generally don't buffer their input. They would do a read() for a large chunk, but when reading from a pipe, if there aren't that many bytes in the pipe, the read() system call will return with as many characters there are and the application will generally work with that if it can. A notable exception to that is mawk which will keep re-read()ing ...


13

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 \n will replace all dots and spaces with newlines. Use (GNU) sed instead: $ echo 'I am happy. I am here. How are you, Meg?' | sed 's/\([!.?]\) /\1\n/g' I am ...


13

etopylight was almost right: tr -s ' \t' '\n' because the question asks to replace tabs, too.


12

If you only want to replace individual characters, not longer strings, use sed -e 'y/ab/ba/' or the equivalent tr command from X Tian's answer. For arbitrary strings, you have to work harder: If there is any character that does certainly not occur in the input, such as # (even a control character will do), you can use something like sed -e 's/a/#/g;s/b/a/...


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