Tag Info

Hot answers tagged

28

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


11

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


9

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


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


7

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


5

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


5

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


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


4

You can't directly print the ascii codes by using the printf "%c" $i like in C language. 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. Eg. To print A, you have to convert the decimal 65 into octal i.e 101 and then you have to ...


4

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" 😈


4

This works well, echo "A" | tr -d "\n" | od -An -t uC


3

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


3

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.


3

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


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

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


3

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.


3

What you want is called a virus scanner. Eg. ClamAV


3

A little bit at the margin of the other (very good) answers, if you want to alter only the control character ^_ when displaying the file content, you might want to transliterate it using the tr utility (and a little bit of bash-compatible syntax): # Replace the control character US (^_) by *one* other character $ cat my.file | tr $'\c_' ':' If you need to ...


3

Can you do this only using POSIX sed? Yes: sed -e 's/.^H//g' < data where ^H is just a literal backspace character. POSIX sed uses POSIX basic regular expressions, which are defined over bytes - printing characters or not, they don't care, so this behaves the same as if ^H were a letter. There are no extensions involved here. Note that all you really ...


3

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


3

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


2

As stated by Shawn, using grep -r might be enough for your needs. Otherwise a combination of find and grep can do the trick: find . -exec grep -H text '{}' ';' This allows to refine the search using specific find predicates. (And -H option is there so that filenames are printed along with the matched content.)


2

Beside OCR-Software, which was my first idea too, I think of libaa1 (ascii-art) and related programs. There is even a program to watch TV in ascii-art, ttv, so there will be an image transforming program, I'm pretty sure. And I remember a program in the ImageMagick package, where you can take a photograph of a flipchart, and it performs ocr on it. ...


2

Interesting question. After browsing the man page, I found that -o prints octal output (od == octal dump), the c you appended only prints the associated characters as well. You get the same numbers with -o alone. Looking at the output it appears that od is reading data two bytes at a time. Take the first two characters for instance: CHAR - OCTAL - BINARY 1 ...


2

You could use aha (also packaged in some Linux distributions), to convert the ANSI colouring sequences to HTML: #! /bin/sh - sendmail -t -oi << EOF To: myaddy@domain.com Subject: Weekly Disk Report MIME-Version: 1.0 Content-Type: text/html; charset="us-ascii" Content-Disposition: inline $(discus | aha) EOF


2

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


2

%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


2

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


1

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 the display changes ...



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