Skip to content

Instantly share code, notes, and snippets.

@jnbdz
Created March 3, 2024 00:47
Show Gist options
  • Save jnbdz/e8d16a5b8f5b0e3f69ce98870a503303 to your computer and use it in GitHub Desktop.
Save jnbdz/e8d16a5b8f5b0e3f69ce98870a503303 to your computer and use it in GitHub Desktop.
awk '{if (!match($0, /^[[:print:]\t\n\r]+$/)) print NR, $0}' ~/.bash_history
@jnbdz
Copy link
Author

jnbdz commented Mar 3, 2024

Breaking Down the Command

awk: Invokes the AWK command-line tool for processing and analyzing text files.
'{if (!match($0, /^[[:print:]\t\n\r]+$/)) print NR, $0}': The AWK script being executed.
    !match($0, /^[[:print:]\t\n\r]+$/): This checks if the entire line ($0) does not match the regular expression. The regular expression ^[[:print:]\t\n\r]+$ is designed to match only lines that consist entirely of printable characters ([:print:]), tabs (\t), newlines (\n), and carriage returns (\r). If a line contains anything else (like binary data), it will not match.
    print NR, $0: If the line contains non-printable characters, this prints the line number (NR) and the line's content ($0), helping you identify exactly where the binary data is located.
~/.bash_history: Specifies the file to be processed by AWK, in this case, your bash history file.

This command helps you identify lines that might contain binary data by excluding those that consist only of characters safe for text files. Lines printed as a result of this command warrant closer inspection, as they contain characters outside the specified range, potentially indicating the presence of binary data.

@jnbdz
Copy link
Author

jnbdz commented Mar 3, 2024

cat ~/.bash_history | grep sublime                                                                                                                                                                                                                                                                                  
grep: (standard input): binary file matches

grep -a

-a, --text
              Process a binary file as if it were text; this is equivalent to the --binary-files=text option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment