Operating System - HP-UX
1833875 Members
1773 Online
110063 Solutions
New Discussion

Removing an name from a file!

 
SOLVED
Go to solution
Tony Walker
Frequent Advisor

Removing an name from a file!

Hi Guys,

Right, I have a permissions file some lines are like this:

jbloggs[2]:rnash[12]:jdoe[1]
others like this:

jbloggs:rnash:jdoe

I have (after hours of trial and error) managed to add a new name based on one that already exists (the problem was including the [2] if necessary. The problem I now face is removal. Lets say I want to remove rnash from the file - how do I do this bearing in mind that ht name may appear as :rnash: :rnash[1]: or :rnash[24]:?

Thanks in advance for your help.

Ton
10 REPLIES 10
steven Burgess_2
Honored Contributor

Re: Removing an name from a file!

Hi

Try this

cat perms | sed 's/rnash\(\[[0-9][0-9]\]\)/ /g'

HTH

Steve
take your time and think things through
john korterman
Honored Contributor

Re: Removing an name from a file!

Hi Tone,
the easiest way is to create a temporary file, containing everything but "rnash". You can do that by e.g.:
# grep -v rnash >
and then mv to

regards,
John K.
it would be nice if you always got a second chance
Sridhar Bhaskarla
Honored Contributor

Re: Removing an name from a file!

Hi Ton,

Try this

sed 's/rnash.*://g' data

This will preserve the : seperators between the other fields after removing rnash entries.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
steven Burgess_2
Honored Contributor

Re: Removing an name from a file!

sorry, that only catches rnash[24]

you can see the format though. Have a play. I'm finished for the weekend

enjoy everyone

Steve
take your time and think things through
Tony Walker
Frequent Advisor

Re: Removing an name from a file!

Thanks for the quick replies! I have attached the file as a couple of the examples here cause another problem. The field is contained within 1 continuous : separated line. So far the /Regexp//g command replaces rnash[]and the REST of the line as opposed to just the field.

Hope tha makes sense.

Thanks,

Tony
Rodney Hills
Honored Contributor

Re: Removing an name from a file!

Perl can do it simply enough-

perl -p -i -e 's/:rnash[^:]*//g' yourfile

Will remove string ":rnhash" followed by any character up until (but not including) the next ":".

HTH

-- Rod Hills
There be dragons...
Sridhar Bhaskarla
Honored Contributor
Solution

Re: Removing an name from a file!

Hi Ton,

sed 's/rnash\[[0-9]*\]://g' your_file

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Ramkumar Devanathan
Honored Contributor

Re: Removing an name from a file!

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

# p.awk - awk script

BEGIN{}
{
for (i=1;i<=NF;i++) {
if (substr($1,1,length(name)) != name ) {
printf ("%s:",$i);
}
}
}
END{}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

assuming abc.txt contains the following string -

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
jbloggs:ramd[3]:hello[245]:asfjdsf:sdflk:ramd[334]:end

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

the command line is:

awk -F: -v name=ramd -f p.awk abc.txt

the output is:
jbloggs:hello[245]:asfjdsf:sdflk:end


Just coz i like awk....

- ramd.
HPE Software Rocks!
john korterman
Honored Contributor

Re: Removing an name from a file!

Hi again,
A simple approach where infile contains your first attachment:

1)put everyone on a separate line:
# tr ":" "\012" infile_1

2) remove all occurrences you do not like:
# grep ???v rnash infile_1 >infile_2

3) put the remaining back on a single line:
# tr ???\012??? ???:??? infile_3

4) remove the extra colon at the end (ksh only):
# STRING=$( cat infile_3 )
# echo ${STRING%:} >outfile

now outfile should be without rnash ??? in any shape.

regards,
John K.
it would be nice if you always got a second chance
Tony Walker
Frequent Advisor

Re: Removing an name from a file!

Thanks again. Problem is now solved thanks to some simple regular expression usage.

Cheers,

Tony