JMK8
Posts: 42
Joined: Tue Apr 28, 2015 10:00 am

bash programming using sed

Wed May 17, 2017 12:12 pm

I have a relatively straight-forward task to do in a bash script using sed. I'm not familiar with bash scripting and even less so with sed. I have a text file like this:

Code: Select all

 random text PARTUUID=a5d5af73-01 random text
Obviously I have no control over the hex number. What I want to do is substitute PARTUUID=a5d5af73-01 with /dev/mmcblk0p1 I have gone around in circles trying to do this with sed but no joy -maybe I'm too thick to get my head around it :?

Can anyone help or give me an alternative method?

-JohnK

User avatar
B.Goode
Posts: 10191
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: bash programming using sed

Wed May 17, 2017 12:27 pm

Maybe awk or perl would provide an alternative way of doing the substitution?

User avatar
Paeryn
Posts: 2952
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: bash programming using sed

Wed May 17, 2017 1:36 pm

JMK8 wrote:I have a relatively straight-forward task to do in a bash script using sed. I'm not familiar with bash scripting and even less so with sed. I have a text file like this:

Code: Select all

 random text PARTUUID=a5d5af73-01 random text
Obviously I have no control over the hex number. What I want to do is substitute PARTUUID=a5d5af73-01 with /dev/mmcblk0p1 I have gone around in circles trying to do this with sed but no joy -maybe I'm too thick to get my head around it :?

Can anyone help or give me an alternative method?

-JohnK
This should change PARTUUID=... (including all characters from the = up to the first space) into /dev/mmcblk0p1 in the file filename, and backs up the original to filename.bak If you don't want the original file backing up don't pass a string to --in-place. And if you don't want to actually alter the file don't pass --in-place, then the output is sent to stdout rather than rewriting the file.

Code: Select all

sed --in-place=".bak" "s,PARTUUID=[^ ]*,/dev/mmcblk0p1," filename
Edit: Just in case there are tabs as well as spaces in there you might want to add \t between the square brackets, and if the PARTUUID= string appears more than once on a line add a g to the end of the search/replace string (otherwise it only alters the first occurrence on a line).

Code: Select all

sed --in-place=".bak" "s,PARTUUID=[^ \t]*,/dev/mmcblk0p1,g" filename
She who travels light — forgot something.

JMK8
Posts: 42
Joined: Tue Apr 28, 2015 10:00 am

Re: bash programming using sed

Fri May 19, 2017 5:26 pm

Thanks guys, I will try this when I get home next week.

-JohnK

User avatar
DougieLawson
Posts: 38883
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: bash programming using sed

Sun May 21, 2017 1:53 pm

JMK8 wrote:. What I want to do is substitute PARTUUID=a5d5af73-01 with /dev/mmcblk0p1 I have gone around in circles trying to do this with sed but no joy -maybe I'm too thick to get my head around it :?
Why? PARTUUID works and is agnostic to the device you're booting. That's why it's in Raspbian Jessie so that the same image works on an SDCard or a bootable USB device (on RPi3).
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

JMK8
Posts: 42
Joined: Tue Apr 28, 2015 10:00 am

Re: bash programming using sed

Mon May 22, 2017 8:22 am

Dougie - OK here's the long story.

About two years ago I wrote a bash script to clone the Raspberry Pi SD card on a running system. As I said previously I am not much of bash scripter but my script does what I want it to. Except -- when I try to boot the cloned SD card I found it would hang. I found that if edit /boot/cmdline.txt and /etc/fstab on the cloned SD card to replace PARTUUID=a5d.....entries with /dev/mmcblk0pX as appropriate everything works fine when booting the cloned SD card. This is a minor inconvenience since I clone SD cards quite infrequently.

I never bothered to try and fix this until a few days ago when I had an hour or two to spare and I decided to go for the blunt instrument fix of automating the editing of the two files. Hence the request in this forum :)

-JohnK

User avatar
DougieLawson
Posts: 38883
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: bash programming using sed

Mon May 22, 2017 8:57 pm

Use blkid to get the PARTUUID and run with them, it will be 10,000,000 times easier in future.

You can stick PARTUUIDs in /boot/cmdline.txt and /etc/fstab and they "just work".
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Return to “General programming discussion”