Friday, May 3, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 842  / 3 Years ago, mon, may 31, 2021, 2:11:44

I am trying to determine the Startsector of a .img file from within a bash script so it can be correctly mounted from within the script. I have been using the file command to easily find the Startsector - how ever I'm not sure how to extract that number from the output. I think the answer lies with using awk but I cannot for the life of me fathom out how to do it.



Here is the output of the file command for reference:



kemra102@kaon:~/Downloads$ file 2012-07-15-wheezy-raspbian.img
2012-07-15-wheezy-raspbian.img: x86 boot sector; partition 1: ID=0xc, starthead 130, startsector 8192, 114688 sectors; partition 2: ID=0x83, starthead 165, startsector 122880, 3665920 sectors, code offset 0xb8
kemra102@kaon:~/Downloads$

More From » 12.04

 Answers
6

It's a bit easier with sed or perl than with awk. With sed: match the whole line, looking for partition 1 followed by startsector 1234 (or any other sequence of digits) with no ; in between. Replace that whole line by just the digit, and print the result. You can change partition 1 to partition 2, of course.



sed -n 's/^.*partition 1:[^;]* startsector ([0-9][0-9]*).*$/1/p'


With perl: look for a substring consisting of partition 1 followed by startsector 1234 with no ; in between; save the digits after startsector in a group. If there's a match, print the digits.



perl -l -ne '/partition 1:[^;]* startsector ([0-9]+)/ and print $1'

[#36288] Tuesday, June 1, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
onomicsiberated

Total Points: 217
Total Questions: 98
Total Answers: 107

Location: Luxembourg
Member since Sun, May 28, 2023
1 Year ago
onomicsiberated questions
Wed, Mar 22, 23, 20:38, 1 Year ago
Thu, Oct 6, 22, 05:00, 2 Years ago
;