bash programming using sed

General programming chat and advice for beginners

6 posts
by JMK8 » 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
Posts: 22
Joined: Tue Apr 28, 2015 10:00 am
by B.Goode » Wed May 17, 2017 12:27 pm
Maybe awk or perl would provide an alternative way of doing the substitution?
Posts: 3474
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK
by Paeryn » 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.
User avatar
Posts: 1381
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England
by JMK8 » Fri May 19, 2017 5:26 pm
Thanks guys, I will try this when I get home next week.

-JohnK
Posts: 22
Joined: Tue Apr 28, 2015 10:00 am
by DougieLawson » 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).
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.
User avatar
Posts: 27872
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
by JMK8 » 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
Posts: 22
Joined: Tue Apr 28, 2015 10:00 am