Hot answers tagged

47

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


25

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) ...


24

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 ...


17

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 ...


13

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


9

What about tree --charset unicode ? |-- boot_print | |-- config-2.6.32-5-amd64 | |-- grub | | |-- 915resolution.mod | | |-- acpi.mod | | |-- affs.mod | | |-- afs_be.mod | | |-- afs.mod | | |-- aout.mod | | |-- ata.mod | | |-- ata_pthru.mod | | |-- at_keyboard.mod | | |-- befs_be.mod | | |-- befs.mod | | |--...


8

If you want to extend it to UTF-8 characters: $ 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" 😈


8

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 ...


8

For hysterical historical reasons, od prints two-byte words¹ by default. The number 020061 (octal) corresponds to the two-byte sequence 1␣ (␣ is a space character). Why? It's clearer if you use hexadecimal: 0o20061 = 0x2031, and ␣ is 0x20 (32) in ASCII and 1 is 0x31 (49). Notice that the lower-order bits (0x31) correspond to the first character and the ...


8

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 ...


8

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 ...


8

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 ...


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 ...


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.


5

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.


5

find . -print0 | xargs -0 grep -FH text Runs faster than either -exec or grep -r on my machine.


5

Bash has a printf builtin, which can around the same as we could learn in C. The syntax a little bit differs. printf '\x2f' If you don't need to worry about higher-level data consistency problems, you can simply convert an url by this function: function deUrl() { printf "${1//%/\\x}" } (It converts every % to a \x, then prints it with printf.)


4

grep is the standard tool for searching for a pattern inside of files. It has an option for searching recursively, which means if it finds a directory, it goes inside and searches in everything in there. The command would simply be grep -r shirt /path/to/girl/ I also like to add two options that skip files that I know I'm not interested in: --binary-files=...


4

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 ...


4

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


4

If you consider that the actual value for each byte you receive from </dev/urandom is only significant in that it represents a successful chance occurrence of that byte's value as determined by the PRNG, then you'll realize that whether or not an input byte matches the value for the ones you're looking for is not nearly as important as how often it does. ...


4

Please note, that on the file system level, there is no difference between ascii and binary files. An ascii or text file is just a binary file containing bytes that are human readable (or control commands like LF=new line). To display the stored bytes in binary form, you can use xxd (part of vim): xxd -b INPUTFILE | cut -d" " -f 2-7 | tr "\n" " " To ...


4

The file command just guesses what is in the files you have it analyse. It does the analysis by reading a certain amount of bytes from the header of a file, sometimes in a multiple step process (if it find some clear marker at the beginning). In a non structured text file it will certainly read more than the number of characters than are in your extended ./...


4

head or tail will not fix/change the character. What probably happens is that gedit tries to guess the encoding of the file based on the first few bytes. When that 0xD4 is far within the file, gedit guesses the file is in ASCII or UTF-8 and complains when it sees that 0xD4 byte that is invalid in either ASCII or UTF-8. While for the second shorter file, ...


3

I'm going for the simple (and elegant?) Bash solution: for i in {a..z}; do echo $(printf "%s %d" "$i" "'$i"); done For in a script you can use the following: CharValue="A" AscValue=`printf "%d" "'$CharValue" Notice the single quote before the CharValue. It is obligated...


3

If your desired result is just the man pages in PDF format, man itself can do this for you. You will need the ghostscript suite of tools and you can do the following, for example, to get the bash(1) page in PDF format: man -T ps bash|ps2pdf - bash.pdf If you were intending to use LaTeX to produce DVI man pages, you won't need ghostscript as man can do ...


3

Have you considered figlet? It writes "large letters" in ASCII art.


3

Simulate a byte train: echo 41 42 43 44 | Change spaces into newlines so the while/read can easily parse them on by one tr ' ' '\n' | Parse byte by byte while read hex; do Convert hex to ascii: printf \\x$hex until end of input done If the files to parse are seriously big, you probably don't want to use bash because it is slow. PERL for ...


3

%c Interprets the associated argument as char: only the first character of a given argument is printed You seem to already have a way to print them, but here is one variant. for i in `seq 32 127`; do printf "\x$(printf "%x" $i) $i"; done



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