You are not logged in.

#1 2009-10-24 06:41:12

brando56894
Member
From: NYC
Registered: 2008-08-03
Posts: 681

Simple Sed expression

I got the idea to create a bash alias to specify the number of concurrent gcc threads to run and  that will take input along with the alias from the command line (ex. jobs 32), but im not really sure on how to do it. I have a slight grasp of sed but its pretty confusing to me, one of the main things I would like to know is how to edit a file in place (the inplace option in manual confuses me) because the only way I know how to do it now is like this:

echo day>day;sed 's/day/night/' <day >day1;mv day1 day

I think the command string I want for the alias to be something like this but im sure theres a cleaner/easier way to do it, and im not sure on the variable parts. I would like '5' and '15' to be replaced by variables, 5 being the current value of MAKEFLAGS=" -j" that is in /etc/makepkg.conf and with 15 being the input value, which IIRC would be $1

cd /etc; sed 's/MAKEFLAGS="  -j5"/MAKEFLAGS=" -j15"/" <makepkg.conf >makepkg.conf1; sudo mv makepkg.conf1 makepkg.conf

Last edited by brando56894 (2009-10-24 06:43:09)

Offline

#2 2009-10-24 06:56:41

thepizzaking
Member
From: Melbourne, Victoria, Australia
Registered: 2006-03-13
Posts: 46

Re: Simple Sed expression

I can help you with the first part:

echo day>day; sed -i -e 's/day/night/' day

Unfortunately that's about as far as my sed knowledge goes, someone else will surely know more.

Offline

#3 2009-10-24 07:13:03

brando56894
Member
From: NYC
Registered: 2008-08-03
Posts: 681

Re: Simple Sed expression

cool, thanks.

Offline

#4 2009-10-24 07:40:50

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Simple Sed expression

You don't need the value of what it was, sed uses regexes. You can match whatever value was there with simply =.*
To get a variable in, you need to close sed's single quotes and then without spaces add double quotes so even if there are spaces in the variable, the variable is glued to the sed statement, and then resume the sed quote '. In this case you want to surround it inside the sed statement with extra double quotes so you get in the output.

sed -i '/^MAKEFLAGS/s/=.*/="'"$1"'"/' /etc/makepkg.conf

Offline

#5 2009-10-24 09:54:47

Gen2ly
Member
From: Sevierville, TN
Registered: 2009-03-06
Posts: 1,529
Website

Re: Simple Sed expression

For a good guide, I like Gentoos' stream editor guide:

http://devmanual.gentoo.org/tools-refer … index.html


Setting Up a Scripting Environment | Proud donor to wikipedia - link

Offline

#6 2009-10-24 13:34:45

tlvb
Member
From: Sweden
Registered: 2008-10-06
Posts: 297
Website

Re: Simple Sed expression

Just replacing the -j flag value:

#!/bin/bash
# send file to work on as first parameter and
# new value for -j as second parameter.
# example: thisscript file 22
sed -i "s/\(MAKEFLAGS=\"[^\"]*\-j\)[0-9]\+/\1$2/" $1

Last edited by tlvb (2009-10-24 13:42:02)


I need a sorted list of all random numbers, so that I can retrieve a suitable one later with a binary search instead of having to iterate through the generation process every time.

Offline

#7 2009-10-24 16:58:13

brando56894
Member
From: NYC
Registered: 2008-08-03
Posts: 681

Re: Simple Sed expression

Thanks for the help, this is still extremely foreign to me though lol hopefully after I read that guide I'll understand it a little better.

Procyon wrote:

You don't need the value of what it was, sed uses regexes. You can match whatever value was there with simply =.*
To get a variable in, you need to close sed's single quotes and then without spaces add double quotes so even if there are spaces in the variable, the variable is glued to the sed statement, and then resume the sed quote '. In this case you want to surround it inside the sed statement with extra double quotes so you get in the output.

sed -i '/^MAKEFLAGS/s/=.*/="'"$1"'"/' /etc/makepkg.conf

using this I get the error that it cant write to a temp file:

sed: couldn't open temporary file /etc/sed1i1bQo: Permission denied

even though I added sudo to the alias sting:

alias jobs='sudo sed -i /^MAKEFLAGS/s/=.*/='\'''\''/ /etc/makepkg.conf'

Offline

#8 2009-10-24 17:20:33

tlvb
Member
From: Sweden
Registered: 2008-10-06
Posts: 297
Website

Re: Simple Sed expression

Hmm, I think you should call the script something else than "jobs" though, as it is a bash alias showing what processes you have suspended and/or running in the background.
E.g:

$ vim foo
vim window opens, editing the file foo
ctrl+z
back at bash prompt, vim foo stopped

[1]+  Stopped                 vim foo
$ vim bar
vim window opens, editing the file bar
ctrl+z
back at bash prompt, vim bar stopped

[2]+  Stopped                 vim bar
$ jobs
[1]-  Stopped                 vim foo
[2]+  Stopped                 vim bar
$ fg 2
the vim process editing the file bar is started again and brought forward

(bold stuff=user input, regular=appears as output, italics=describing the situation)

Last edited by tlvb (2009-10-24 17:31:08)


I need a sorted list of all random numbers, so that I can retrieve a suitable one later with a binary search instead of having to iterate through the generation process every time.

Offline

#9 2009-10-24 17:22:13

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Simple Sed expression

alias does not have $1 and such, use a function.

Offline

#10 2009-10-24 17:33:46

tlvb
Member
From: Sweden
Registered: 2008-10-06
Posts: 297
Website

Re: Simple Sed expression

...Or make it a script with the appropirate hashbang and put it in a directory listed in $PATH
(I usually use /usr/bin)


I need a sorted list of all random numbers, so that I can retrieve a suitable one later with a binary search instead of having to iterate through the generation process every time.

Offline

#11 2009-10-26 23:57:35

brando56894
Member
From: NYC
Registered: 2008-08-03
Posts: 681

Re: Simple Sed expression

I changed the name of the script to threads and its located in my ~/scripts directory. it works perfectly but I just needed to add in -j to the sed expression before the $1 variable or else it would just change MAKEFLAGS=-j[nunmber] to MAKEFLAGS=[number].

Thanks for the help and heres the whole script incase someone stumbles upon this thread and wants it.

#!/bin/bash

sudo sed -i '/^MAKEFLAGS/s/=.*/="'-j"$1"'"/' /etc/makepkg.conf

Offline

Board footer

Powered by FluxBB