1819805 Members
3166 Online
109607 Solutions
New Discussion юеВ

Re: sed brainstorming

 
SOLVED
Go to solution
Alexandre Arents
Occasional Advisor

sed brainstorming


Hi all,

I need to add in a lots of .profile one line which is:

export ECRAN=`who am I -R | sed 's/.*(\([a-zA-Z0-9]*\)\..*/\1/'`

(the line must be insert before a match)
I search a good script using sed or awk which looks like that:

for i in [ .. . profile] ; do
sed 's/match/iexport ECRAN=`....' $i
done

this one didn't work because there are many special char in the string :
Maybe someone can help me.
Thanks
6 REPLIES 6
Donny Jekels
Respected Contributor

Re: sed brainstorming

why don't you use Perl, it has a better reg ex engine than sed.

you will need to spool the .profile files either in memory or to a temp file.

send what you want done than I'll code it for you.

life is good here
Donny
"Vision, is the art of seeing the invisible"
Jean-Luc Oudart
Honored Contributor
Solution

Re: sed brainstorming

Well you can use vi for this can of job
keep your loop
add a match command for these .profile
and

vi .profile /dev/null

with mycom :
/
:r tpl1
:wq

and tpl1 is :
export ECRAN=`who am I -R | sed 's/.*(\([a-zA-Z0-9]*\)\..*/\1/'`

Of course if you navigate in subdir you would have to give full path for these files.

this will add the comand lie after the 1st match.
If none it would add the line on top of your .profile so better if you check beforehand with say "grep"

It's all yours

Rgds.
JL
fiat lux
Rodney Hills
Honored Contributor

Re: sed brainstorming

If I had to do mass changes on all user profiles, I would do the following-

for i in /home/*/.profile ; do
perl -p -i -e 'print "export ECRAN=\`who am I -R | sed \'s/.*(\\([a-zA-Z0-9]*\)\..*/\1/\'\`\n" if /match/'
done

This will insert the "export" line prior to any line that contains the text "match". I had to add extra "\" to the export so that perl would pass the characters asis. They of course will disappear in the resultant file.

The a nifty option to perl is called in-place changes. The -i option automatically creates a temp file, and sends the output there. At the end of the process, perl will make the temp file the "new" file. You also have the option to rename the original, ie -i.bu will rename original by appending ".bu".

HTH

-- Rod Hills
There be dragons...
Rodney Hills
Honored Contributor

Re: sed brainstorming

Whoops, I forgot to add the filename as part of the command.

for i in /home/*/.profile ; do
perl -p -i -e 'print "export ECRAN=\`who am I -R | sed \'s/.*(\\([a-zA-Z0-9]*\)\..*/\1/\'\`\n" if /match/' $i
done
There be dragons...
James R. Ferguson
Acclaimed Contributor

Re: sed brainstorming

Hi:

Here's one possible way with 'awk' and a shell wrapper:

#!/usr/bin/sh
EXP="export ECRAN=\`who am I -R | sed 's/.*(\([a-zA-Z0-9]*\)\..*/\1/'\`"
awk -v EXP="${EXP}" '
BEGIN{ B=0 }
/PATH=/ {
if (B==0) { B=1;print EXP }
}
{print $0}
' $1 > $1.new
exit 0

NOTE that the "ticks" are escaped with the backslash in the 'EXP' variable assignment. Pass the name of the user's profile to the shell script. The modified file is titled the same with a ".new" extension.

I chose to insert the export before the first PATH declaration. You could do it anywhere.

Regards!

...JRF...
Martin Robinson
Frequent Advisor

Re: sed brainstorming

If you need this variable to be defined in lots of different .profiles, why not just put the line once in /etc/profile, which is read by every .profile.

This makes maintenance a little easier, in case you need to make a change in the future.