Page 1 of 1
bash grep help
Posted: 30 Nov 2015, 06:24
by Ed_P
If the command "cat /proc/cmdline" returns a "from=" operand how does one extract the file name that follows it?
FileName=`cat /proc/cmdline`
echo "Boot File: ${FileName:17:36}"
Works if the filename is 36 characters but that isn't always the case. I've looked into using grep but have failed so far.
Re: bash grep help
Posted: 30 Nov 2015, 06:57
by aus9
Ed_P
I am an intermediate coder so until you get a better reply I hope you don't mind me asking questions and making comments?
1) Can you show the actual output of these 2 commands please
The reason why I ask is your second line does not appear to be relative to your first line.
I was expecting your second line to read something like .....
echo $FileName | grep something or
VARIABLE2=`echo $FileName | grep something`
but maybe you are trying to write a script?
I don't know just guessing
good luck
Re: bash grep help
Posted: 30 Nov 2015, 08:21
by donald
not using grep but sed to get the Filename
Try:
Code: Select all
cat /proc/cmdline | sed -e 's/^.*from=//' -e 's/ .*$//'
The first expression removes from= and everything before.
The second one removes the next space and everything after.
more info about sed
http://linux.die.net/man/1/sed
Re: bash grep help
Posted: 30 Nov 2015, 12:26
by Bogomips
Bashing along:
Code: Select all
guest@porteus:~$ c=$(cat /proc/cmdline); f=${c#*from=}; f=${f%% *}
guest@porteus:~$ echo $f
/tmp/iso/Por-KDE4-v3.1-4.2.5-i486.iso
@donald
Nice link.
Re: bash grep help
Posted: 30 Nov 2015, 15:53
by Ed_P
Wow!!!
Thank you all.
Code: Select all
guest@porteus:~$ cat /proc/cmdline
quiet from=/ISOs/Porteus-RazorQT-v3.0.1-x86_64-nu.iso changes=EXIT:/porteus3.0/changes/porteussave.dat extramod=/porteus3.0/Modules volume=40 reboot=cold ramsize=80%
guest@porteus:~$ echo $MODDIR
guest@porteus:~$ cat /proc/cmdline | sed -e 's/^.*from=//' -e 's/ .*$//'
/ISOs/Porteus-RazorQT-v3.0.1-x86_64-nu.iso
guest@porteus:~$ c=$(cat /proc/cmdline); f=${c#*from=}; f=${f%% *}
guest@porteus:~$ echo $f
/ISOs/Porteus-RazorQT-v3.0.1-x86_64-nu.iso
guest@porteus:~$
But the part that really blows my mind are the special character strings. c#* %% * s/^. s/.*$// Where did these come from??? The closest I remember seeing something close is here:
http://forum.porteus.org/viewtopic.php? ... =30#p28468
-update-
And now I have:
Code: Select all
guest@porteus:~$ bootdev.sh
Boot device: /dev/sda5
Device format: "ntfs"
Boot ISO: /ISOs/Porteus-RazorQT-v3.0.1-x86_64-nu.iso
Changes are being saved to a save.dat file.
Save.dat file: /porteus3.0/changes/porteussave.dat
Cmdline: quiet from=/ISOs/Porteus-RazorQT-v3.0.1-x86_64-nu.iso changes=EXIT:/porteus3.0/changes/porteussave.dat extramod=/porteus3.0/Modules volume=40 reboot=cold ramsize=80%
guest@porteus:~$
Re: bash grep help
Posted: 30 Nov 2015, 18:15
by Bogomips
Ed_P wrote:But the part that really blows my mind are the special character strings. c#* %% * s/^. s/.*$// Where did these come from???
c#* %% *
http://man.cx/bashFind(^F): ##
s/^. s/.*$
http://manpages.ubuntu.com/manpages/pre ... gex.7.html
Re: bash grep help
Posted: 30 Nov 2015, 23:55
by Ed_P