1753500 Members
3571 Online
108794 Solutions
New Discussion юеВ

Re: SED help

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

SED help

Hello,

I want to remove a "." from the end of the path from a users profile and want to use sed.

I use this string to determine to determine the period "." in an if statement:

echo $PATH |awk -F":" '/\.$/ {print $NF}'

if true what is the best way to use sed to remove the ".":

my method:

if [[ $(echo $PATH |awk '/\./ {print NF}') -gt "0" ]] ; then

cp /home//.profile /home//.profile.bck

then I have difficulty replacing the . from the profile and recreate the .profile ....

can anyone please help?

thanks

Chris.
hello
7 REPLIES 7
OFC_EDM
Respected Contributor

Re: SED help

Why echo the entire path?

Why don't you use the form of the ls command to list just the file name.

ls - /.profile

So it shows up as
.profile

Then pipe that to tr and do a tr "." ""

I don't have access to a system today so I didnt' test the tr syntax.


Or if you want to just replace the . at the beginning of the
.profile

Pipe it to sed -e 's/^.//g'
that should work. May have to \ out the . though.
The Devil is in the detail.
lawrenzo_1
Super Advisor

Re: SED help

I dont think your answer will work ....

I only echo the path of the user in this example however I will check the .profile of the user I need to change.

I cannot use

# sed 's/\.//' as this will remove any other "." from the profile so all I am intereted in is the ^PATH=* then recreate the file without the "." in the path.

thanks anyway.
hello
OFC_EDM
Respected Contributor

Re: SED help

Instead of changing the sed statement change the path you provide to ls.

Then my solution may work.

And put a ^ in the sed statement ^\. to indicate to find only the . at the beginning.

Cheers
The Devil is in the detail.
lawrenzo_1
Super Advisor

Re: SED help

ok well thanks but maybe I didnt explain very well however came up with this solution:

if [[ $(awk '/^PATH.+\.$/ {print NF}' /home/ca/.profile) -gt "0" ]] ; then

cp /home/ca/.profile /home/ca/.profile.bck

cat /home/ca/.profile.bck |sed -e '/^PATH/s/\.//' > /home/ca/.profile

fi

is this a logical method?
hello
Dennis Handly
Acclaimed Contributor
Solution

Re: SED help

What do you mean "." from the end of a path?

If you mean the "." at the very end you can use: -e 's/\.$//'
If you mean remove a "." component from a PATH component you could use:
sed -e 's!:.:!:g' -e 's/:\.$//'
lawrenzo_1
Super Advisor

Re: SED help

ok so the system is AIX and sometimes the systems are configured so the path has PATH:*:. at the end in .profile ....

if I just run s/\.$//' then this will remove any "." from the profile.

I will take a look at the component method so thanks ...
hello
lawrenzo_1
Super Advisor

Re: SED help

thanks both
hello