Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1095423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:07:18+00:00 2026-05-17T00:07:18+00:00

Standard grep / pcregrep etc. can conveniently be used with binary files for ASCII

  • 0

Standard grep/pcregrep etc. can conveniently be used with binary files for ASCII or UTF8 data – is there a simple way to make them try UTF16 too (preferably simultaneously, but instead will do)?

Data I’m trying to get is all ASCII anyway (references in libraries etc.), it just doesn’t get found as sometimes there’s 00 between any two characters, and sometimes there isn’t.

I don’t see any way to get it done semantically, but these 00s should do the trick, except I cannot easily use them on command line.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:07:19+00:00Added an answer on May 17, 2026 at 12:07 am

    The easiest way is to just convert the text file to utf-8 and pipe that to grep:

    iconv -f utf-16 -t utf-8 file.txt | grep query
    

    I tried to do the opposite (convert my query to utf-16) but it seems as though grep doesn’t like that. I think it might have to do with endianness, but I’m not sure.

    It seems as though grep will convert a query that is utf-16 to utf-8/ascii. Here is what I tried:

    grep `echo -n query | iconv -f utf-8 -t utf-16 | sed 's/..//'` test.txt
    

    If test.txt is a utf-16 file this won’t work, but it does work if test.txt is ascii. I can only conclude that grep is converting my query to ascii.

    EDIT: Here’s a really really crazy one that kind of works but doesn’t give you very much useful info:

    hexdump -e '/1 "%02x"' test.txt | grep -P `echo -n Test | iconv -f utf-8 -t utf-16 | sed 's/..//' | hexdump -e '/1 "%02x"'`
    

    How does it work? Well it converts your file to hex (without any extra formatting that hexdump usually applies). It pipes that into grep. Grep is using a query that is constructed by echoing your query (without a newline) into iconv which converts it to utf-16. This is then piped into sed to remove the BOM (the first two bytes of a utf-16 file used to determine endianness). This is then piped into hexdump so that the query and the input are the same.

    Unfortunately I think this will end up printing out the ENTIRE file if there is a single match. Also this won’t work if the utf-16 in your binary file is stored in a different endianness than your machine.

    EDIT2: Got it!!!!

    grep -P `echo -n "Test" | iconv -f utf-8 -t utf-16 | sed 's/..//' | hexdump -e '/1 "x%02x"' | sed 's/x/\\\\x/g'` test.txt
    

    This searches for the hex version of the string Test (in utf-16) in the file test.txt

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.