Scan a gz file for non-printable characters and display each line number and line that contains them.

zcat a_big_file.gz | sed -ne "$(zcat a_big_file.gz | tr -d "[:print:]" | cat -n | grep -vP "^ *\d+\t$" | cut -f 1 | sed -e "s/\([0-9]\+\)/\1=;\1p;/" | xargs)" | tr -c "[:print:]\n" "?"
Scans the file once to build a list of line numbers that contain non-printable characters Scans the file again, passing those line numbers to sed as two commands to print the line number and the line itself. Also passes the output through a tr to replace the characters with a ?
Sample Output
Hi, I'm a line with ctrl?characters
I?m another line

2009-02-24 02:57:37

What Others Think

I feel like there should be an easier way to do this, but I'm definitely glad I found the command, it's quite useful for what I'm doing. I don't quite understand how the last sed command and the xargs are working with the sed -ne "$(), could anyone help explain this? BTW I find it nice to add the following to the end (assuming that you don't have lines with numbers only within file), just for formatting sake: | sed -e :a -e '/^[0-9]\+$/N; s/\([0-9]\+\)\n/\1: /; ta'
erranteyes · 674 weeks ago
BTW I find it more descriptive for finding what non-printable char exists to use cat -A rather than the ? tr complement
erranteyes · 674 weeks ago
I think this one will do the same: zcat a_big_file.gz | egrep -n ""
juto · 458 weeks and 4 days ago
@juto I'm not sure if the comment system ate something you had in the egrep argument, but that command would just output every single line with line numbers (note you could also use zgrep instead of zcat | ). This command that I listed ever so long ago was set up to display the line number and text for each line that contained non-printable characters and additionally translate them to question marks. @erranteyes I agree I think there must be a simpler way, but this isn't a problem I've had to solve again in the past several years.
DEinspanjer · 458 weeks and 4 days ago
seofox · 26 weeks ago ??? ? ????? ????
seofox · 15 weeks and 2 days ago
seofox · 14 weeks and 3 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: