Hot answers tagged

78

Define these two functions (usually available in other languages): chr() { [ "$1" -lt 256 ] || return 1 printf "\\$(printf '%03o' "$1")" } ord() { LC_CTYPE=C printf '%d' "'$1" } Usage: chr 65 A ord A 65


67

I'm not sure about this but I think all you need is tree | sed 's/├/\+/g; s/─/-/g; s/└/\\/g' For example: $ tree . ├── file0 └── foo ├── bar │   └── file2 └── file1 2 directories, 3 files $ tree | sed 's/├/\+/g; s/─/-/g; s/└/\\/g' . +-- file0 \-- foo +-- bar │   \-- file2 \-- file1 2 directories, 3 files Alternatively, you can ...


40

$ echo AB | perl -lpe '$_=unpack"B*"' 0100000101000010 $ echo 0100000101000010 | perl -lpe '$_=pack"B*",$_' AB -e expression evaluate the given expression as perl code -p: sed mode. The expression is evaluated for each line of input, with the content of the line stored in the $_ variable and printed after the evaluation of the expression. -l: even more like ...


30

Essentially "because it's been done that way since manual typewriters". Really. A manual typewriter had a carriage on which the paper was fed, and it moved forward as you typed (loading a spring), and had a lever or key which would release the carriage, letting the spring return the carriage to the left-margin. As electronic data entry (teletype, etc) ...


28

ASCII characters are characters in the range from 0 to 177 (octal) inclusively. To delete characters outside of this range in a file, use LC_ALL=C tr -dc '\0-\177' <file >newfile The tr command is a utility that works on single characters, either substituting them with other single characters (transliteration), deleting them, or compressing runs of ...


26

You can use xxd to convert from ASCII and binary. $ echo -n "A" | xxd -b 0000000: 01000001 A $ echo -n "A" | xxd -b | awk '{print $2}' 01000001 Converting bases If you're looking to do just base conversions between Hex, Octal, & Dec I usually use the basic calculator command line tool (bc) to do such ...


21

You can see the entire set with: $ man ascii You'll get tables in octal, hex, and decimal.


20

When using vim -b, this displays all high characters as <xx>: set encoding=latin1 set isprint= set display+=uhex Any single-byte encoding will work, vim uses ASCII for all lower chars and has them hard-coded as printable. Setting isprint to empty will mark everything else as non-printable. Setting uhex will display them as hexadecimal. Here is how ...


20

The unit separator (US) character, also known as IS1, is in the cntrl character class and is not in the print character class. It is a control character that is intended for organizing text into groups, for programs that are designed to make use of that information. In general, non-printable characters are probably going to be interpreted and rendered ...


14

This works well, echo "A" | tr -d "\n" | od -An -t uC echo "A" ### Emit a character. | tr -d "\n" ### Remove the "newline" character. | od -An -t uC ### Use od (octal dump) to print: ### -An means Address none ...


14

You can't directly print the ascii codes by using the printf "%c" $i like in C. You have to first convert the decimal value of i into its octal value and then you have to print it using using printf and putting \ in front of their respective octal values. To print A, you have to convert the decimal 65 into octal, i.e. 101, and then you have to print that ...


13

If you want to extend it to UTF-8 characters (assuming you're in a UTF-8 locale): $ perl -CA -le 'print ord shift' 😈 128520 $ perl -CS -le 'print chr shift' 128520 😈 With bash, ksh or zsh builtins: $ printf "\U$(printf %08x 128520)\n" 😈


13

Hex: printf '\x4a' Dec: printf "\\$(printf %o 74)" Alternative for hex :-) xxd -r <<<'0 4a'


12

Try doing this in a shell : To test on STDOUT : column -t file.txt To modify the file : column -t file.txt > new_file.txt && mv new_file.txt file.txt As you can see, that's all you need. It saves you a lot of time playing with complicated printf tricks.


11

While Thomas Dickey's answer is quite correct, Stéphane Chazelas correctly mentioned in a comment to Dickey's answer that the conversion is not set in stone; it is part of the line discipline. In fact, the translation is completely programmable. The man 3 termios man page contains basically all the pertinent information. (The link takes to Linux man-pages ...


10

Using bc and bash: #!/bin/bash chrbin() { echo $(printf \\$(echo "ibase=2; obase=8; $1" | bc)) } ordbin() { a=$(printf '%d' "'$1") echo "obase=2; $a" | bc } ascii2bin() { echo -n $* | while IFS= read -r -n1 char do ordbin $char | tr -d '\n' echo -n " " done } bin2ascii() { for bin in $* do chrbin $...


10

The unit separator is in the ASCII range of Control Characters, and therefore does not (or should not usually) have a visual representation. Vim and some other editors display them, so you can edit them. As you noticed, cat -v displays it too. The man page shows, that -v is the short form of --show-nonprinting, which causes it to replace the non-printing ...


9

The pairings are "the Latin alphabet", 1 through 26 (and then the relevant rest of ASCII too). Ctrl-C gives ETX, byte value 3 (0x03, 000000011); C is ASCII 67 (0x43, 010000011). Flip bit 7 (add/subtract 64) to get from one to the other. SUB is byte value 26, and so on, as listed on the Wikipedia page you mention in order from 1-26 and A-Z. The other C0 ...


9

If all you need is a regex: [\x00-\x7F] that you could apply to several utilities: <file LC_ALL=C sed 's/[^\o0-\o177]//g' # GNU sed without POSIXLY_CORRECT <file LC_ALL=C awk '{gsub(/[^\0-\177]/,"");print}' <file perl -pe 's/[^[:ascii:]]//g;' <file LC_ALL=C tr -dc '\0-\177' Understand that sed, awk, and perl expect ...


9

printf '\101' where 101 is a an octal number outputs the byte with that value. When sent to an ASCII terminal, that will be rendered as A as A is character 65 (octal 101) in ASCII and all ASCII-compatible character sets (which includes most modern charsets with the exception of the EBCDIC ones still used on some IBM systems). In printf \\$(printf '%03o' $...


7

ASCII numbers between 0 and 9 < /dev/urandom tr -dc '[:digit:]' | head -c 10000000 > 10mb.txt ASCII 1s and 0s < /dev/urandom tr -dc 01 | head -c 10000000 > 10mb.txt


7

I think you're confusing "encoding" and "character sets". In the first case, the file contains only characters found in US-ASCII. This means that the file will look the same no matter what language settings you're using to display it. In the second case, the file now contains characters belonging to the UTF8 character set, because that's what you put into ...


7

In general, the shell could understand hex, oct and decimal numbers in variables, provided they have been defined as integers: $ declare -i v1 v2 v3 v4 v5 v6 v7 $ v1=0112 $ v2=74 $ v3=0x4a $ v4=8#112 $ v5=10#74 $ v6=16#4a $ v7=18#gg echo "$v1 $v2 $v3 $v4 $v5 $v6 $v7" 74 74 74 74 74 74 304 Or they are the result of an "Arithmetic Expansion": $ : $(( v1=...


7

Variables in the shell can contain any character, except for the NUL character, just like filenames in the filesystem. You should therefore not have any problem storing the filenames in variables, unless you read the mangled output of ls, which will possibly be modified for display purposes (ls output is strictly for looking at). In the edited question, ...


6

This is the hexundump script from my personal collection: #!/usr/bin/env perl $^W = 1; $c = undef; while (<>) { tr/0-9A-Fa-f//cd; if (defined $c) { warn "Consuming $c"; $_ = $c . $_; $c = undef; } if (length($_) & 1) { s/(.)$//; $c = $1; } print pack "H*", $_; } if (!eof) { die "$!"; } if (defined $c) { warn "Odd number of ...


6

You can use echo -e: echo -e "\x66\x6f\x6f" Do note that hexdump -C is what you want to dump the contents of the file in byte order instead of being interpreted as 4-byte words in network byte order.


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