Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

I need "read file until" tool

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
13 posts • Page 1 of 1
Author
Message
Zucca
Moderator
Moderator
User avatar
Posts: 4691
Joined: Thu Jun 14, 2007 10:31 pm
Location: Rasi, Finland
Contact:
Contact Zucca
Website

I need "read file until" tool

  • Quote

Post by Zucca » Tue Mar 02, 2021 10:00 pm

I have a specific need for a tool which can work in a pipe with other programs.

I need to read a (binary) file until a string (more preferably regex) of characters (in hex or ascii) are encountered.

I've used something like this before:

Code: Select all

((offset=$(grep --byte-offset --only-matching --text "<regex>" "<file>" | head -n 1 | grep -Eo '^[0-9]+')+1))
tail -c +"$offset" "<file>"
... although this example outputs the data after the regexp. Easily modified to output the data before. But that cannot work in a pipeline. Unless one could use "tee" with some strange magic. I don't think its possible.

Anyway. Does such tool exist? Or can it be done with some shell (bash) trickery? Maybe (g)awk?

I want this because to avoid using temporary files.
..: Zucca :..

Code: Select all

init=/sbin/openrc-init
-systemd -logind -elogind seatd
I am NaN! I am a man!
Top
mike155
Advocate
Advocate
Posts: 4438
Joined: Fri Sep 17, 2010 11:33 pm
Location: Frankfurt, Germany

  • Quote

Post by mike155 » Tue Mar 02, 2021 10:41 pm

You could use sed:

Code: Select all

sed '/PATTERN/q' FILE
See: https://unix.stackexchange.com/question ... -the-match

Sed works at least for text files. Maybe it can be tweaked to work with binary files.
Last edited by mike155 on Wed Mar 03, 2021 12:18 am, edited 1 time in total.
Top
mike155
Advocate
Advocate
Posts: 4438
Joined: Fri Sep 17, 2010 11:33 pm
Location: Frankfurt, Germany

  • Quote

Post by mike155 » Tue Mar 02, 2021 11:19 pm

dev-util/bbe is a sed for binary files.

You could try sometghing like:

Code: Select all

bbe -b '/STRING/:$' -e "D" 
Bbe doesn't seems to understand regular expressions though...
Top
Zucca
Moderator
Moderator
User avatar
Posts: 4691
Joined: Thu Jun 14, 2007 10:31 pm
Location: Rasi, Finland
Contact:
Contact Zucca
Website

  • Quote

Post by Zucca » Tue Mar 02, 2021 11:36 pm

mike155 wrote:dev-util/bbe is a sed for binary files
Thanks!
I might get away using it... It surely is the closest thing I'm looking for.
..: Zucca :..

Code: Select all

init=/sbin/openrc-init
-systemd -logind -elogind seatd
I am NaN! I am a man!
Top
Zucca
Moderator
Moderator
User avatar
Posts: 4691
Joined: Thu Jun 14, 2007 10:31 pm
Location: Rasi, Finland
Contact:
Contact Zucca
Website

  • Quote

Post by Zucca » Thu Mar 04, 2021 1:28 pm

This bbe looks like a wonderful tool.
Shame it does not support regex or even patterns (kinda).

The d command would for perfectly if it could take negative values as first parameter. I guess it would complicate the program by order of magnitude.

But that gave me a thought... What if I could give sed different record (block) splitter than a newline?
Then again I could do the same with awk. Problem is then if you give give it a huge binary file... It could eat up my RAM very quickly and with it goes down the performance.

Oh well. I'll take one more closer look at bbe, test it out, then decide if it is the real answer.
..: Zucca :..

Code: Select all

init=/sbin/openrc-init
-systemd -logind -elogind seatd
I am NaN! I am a man!
Top
ff11
l33t
l33t
User avatar
Posts: 665
Joined: Mon Mar 10, 2014 10:24 pm

  • Quote

Post by ff11 » Thu Mar 04, 2021 1:51 pm

Not every binary has strings encoded in the same way (some will need the search to be done relative to the position of each character on the string, others will need a special character table). Could you tell us what you need this for?

EDIT: Just for information, how a string can get a format that makes several of these tools infeasible on the binary: Hazards of Converting Binary Data To A String
| Proverbs 26:12 |
| There is more hope for a fool than for a wise man that are wise in his own eyes. |
* AlphaGo - The Movie - Full Documentary "I want to apologize for being so powerless" - Lee
Top
Zucca
Moderator
Moderator
User avatar
Posts: 4691
Joined: Thu Jun 14, 2007 10:31 pm
Location: Rasi, Finland
Contact:
Contact Zucca
Website

  • Quote

Post by Zucca » Thu Mar 04, 2021 7:27 pm

ff11: I'm trying to concatenate cpio archives on-the-fly (pipe). Normally you'd need to store the earlier archive on disk before you can concatenate another one. I've solved that problem already.

Now I'm just interested generally about the topic. bbe is quite a tool, but lacks regexp features.
String format therefore isn't an issue either. I think strings -tool is for that kind of usage.
..: Zucca :..

Code: Select all

init=/sbin/openrc-init
-systemd -logind -elogind seatd
I am NaN! I am a man!
Top
ff11
l33t
l33t
User avatar
Posts: 665
Joined: Mon Mar 10, 2014 10:24 pm

  • Quote

Post by ff11 » Fri Mar 05, 2021 12:07 am

Zucca wrote:ff11: I'm trying to concatenate cpio archives on-the-fly (pipe). Normally you'd need to store the earlier archive on disk before you can concatenate another one. I've solved that problem already.

Now I'm just interested generally about the topic. bbe is quite a tool, but lacks regexp features.
String format therefore isn't an issue either. I think strings -tool is for that kind of usage.
I'm sorry, but I didn't get it right. For some reason I think you are trying to recreate the app-arch/tar using strings and concatenation.
| Proverbs 26:12 |
| There is more hope for a fool than for a wise man that are wise in his own eyes. |
* AlphaGo - The Movie - Full Documentary "I want to apologize for being so powerless" - Lee
Top
Zucca
Moderator
Moderator
User avatar
Posts: 4691
Joined: Thu Jun 14, 2007 10:31 pm
Location: Rasi, Finland
Contact:
Contact Zucca
Website

  • Quote

Post by Zucca » Fri Mar 05, 2021 11:27 am

<OT>
I'm making a wrapper around cpio, which has few features to identify entries given to it (from stdin) so that it can be used also as a initramfs image creation tool (my primary goal).
It can be also dropped into /etc/kernel/postinst.d so it can auto create initramfs image when a new kernel is installed.

It however does not have any more automation than identifying the entries, therefore it can be used also as a general cpio tool. The automatic part of it is only to deal with library collecting and module dependencies (optionally including firmware files). Everything else is left to the user:
  • linuxrc/init script
  • listing of required
    • binaries
    • modules
    • ... and optionally firmware files
So basically it solves the dependencies for initramfs and packs them along other files specified by the user.
</OT>
..: Zucca :..

Code: Select all

init=/sbin/openrc-init
-systemd -logind -elogind seatd
I am NaN! I am a man!
Top
ff11
l33t
l33t
User avatar
Posts: 665
Joined: Mon Mar 10, 2014 10:24 pm

  • Quote

Post by ff11 » Fri Mar 05, 2021 2:03 pm

Zucca wrote:<OT>
I'm making a wrapper around cpio, which has few features to identify entries given to it (from stdin) so that it can be used also as a initramfs image creation tool (my primary goal).
It can be also dropped into /etc/kernel/postinst.d so it can auto create initramfs image when a new kernel is installed.

It however does not have any more automation than identifying the entries, therefore it can be used also as a general cpio tool. The automatic part of it is only to deal with library collecting and module dependencies (optionally including firmware files). Everything else is left to the user:
  • linuxrc/init script
  • listing of required
    • binaries
    • modules
    • ... and optionally firmware files
So basically it solves the dependencies for initramfs and packs them along other files specified by the user.
</OT>
Sorry, but i'm a little lost here.
Isn't cpio an archive that is a concatenation of files and directories preceded by a header giving the file name and other file system information (like the size)?
Isn't it enough to use the command normally?
If the issue is with stdin, wouldn't using named pipe be enough?
And if the cpio command is missing features, wouldn't it be better to discuss it with the GNU people? They probably have a much broader view on using cpio than the folks here.
( on the mailing list: https://www.gnu.org/software/cpio/cpio.html ).
| Proverbs 26:12 |
| There is more hope for a fool than for a wise man that are wise in his own eyes. |
* AlphaGo - The Movie - Full Documentary "I want to apologize for being so powerless" - Lee
Top
Zucca
Moderator
Moderator
User avatar
Posts: 4691
Joined: Thu Jun 14, 2007 10:31 pm
Location: Rasi, Finland
Contact:
Contact Zucca
Website

  • Quote

Post by Zucca » Fri Mar 05, 2021 7:48 pm

You misunderstood and I didn't explain very well.

Features of my little project which are out of cpio feature scope:
  • Handles dependencies of modules and their firmware files
  • Handles directories differently
    • cds into the directory and adds all the files and directories using realtive paths (add them into cpio root)
  • Can work as a drop-in file inside /etc/kernel/postinst.d
  • Compresses the archive
All the files with absolute paths are added as-is but binaries are inspected if they need any dynanic libraries. Those library files are automatically added to the cpio.
Modules listed with their name only (no path given) are inspected for dependencies also.
If --auto-firmware is being used then all the firmware files which are associated with listed modules are also added automatically.

All these features are not something cpio would need built-in.

The reason I want to concatenate cpio archives is to make it a little neat.
Normally there's an EOF marker inside every cpio archive: A pseudo file named 'TRAILER!!!' with zero size. I'm removing this marker. It's not really neccessary in case of initramfs images. But normally you'd need to run cpio inside a loop to be able to extract or list the files it contains.

So yes. cpio archives are files concatenated, but cpio still has the EOF marker.

man 5 cpio is really interesting read in its own right. I've been reading it quite many times now. :P

Anyway... Too much off-topic now.
..: Zucca :..

Code: Select all

init=/sbin/openrc-init
-systemd -logind -elogind seatd
I am NaN! I am a man!
Top
szatox
Advocate
Advocate
Posts: 3858
Joined: Tue Aug 27, 2013 12:35 pm

  • Quote

Post by szatox » Sun Mar 07, 2021 11:00 am

I haven't inspected the binary format of CPIO archive, but you might even get away with using head.
Like in head -c -10 (skip the last 10 bytes)
Top
Zucca
Moderator
Moderator
User avatar
Posts: 4691
Joined: Thu Jun 14, 2007 10:31 pm
Location: Rasi, Finland
Contact:
Contact Zucca
Website

  • Quote

Post by Zucca » Sun Mar 07, 2021 11:18 am

szatox wrote:I haven't inspected the binary format of CPIO archive, but you might even get away with using head.
Like in head -c -10 (skip the last 10 bytes)
I actually tried it. It didn't work properly, because cpio adds trailing padding depending of which cpio format is being used.
..: Zucca :..

Code: Select all

init=/sbin/openrc-init
-systemd -logind -elogind seatd
I am NaN! I am a man!
Top
Post Reply

13 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic