| View previous topic :: View next topic |
| Author |
Message |
snuffsaid
Joined: Mon Feb 06, 2006 10:24 pm Posts: 6 Location: West Wittering
|
Posted: Mon Feb 06, 2006 10:48 pm Post subject: editing text files from within the shell |
|
|
evening all,
I have been trying to get a shell script working that will automatically edit a text file. The text file is set out using ':' as a delimiter between bits of info, a bit like the /etc/passwd file. I am trying to use only bash shell scripting for this, no PHP, Ruby, PERL, etc. for reasons of portability, not all the machines have such goodies.
I have managed to get the script to edit a line by using the output of sed and redirecting it to a new file, then copying the file back over the original. Though this seems to leave a blank line in place of the old line if I remove the files entry with the script, though editing the second field does work.
Does anyone know of a more elegant way of doing this? If not with sed then I think all of my machines do have awk if that is of any help to someone.
Cheers
Pete |
|
| Back to top |
|
 |
Nigel LXF regular

Joined: Fri Apr 08, 2005 9:03 pm Posts: 1141 Location: Gloucestershire, UK
|
Posted: Mon Feb 06, 2006 11:53 pm Post subject: RE: editing text files from within the shell |
|
|
| What exactly are you trying to do to the file ? There's a good chance that both sed and awk can do what you want, but one may be easier than the other for the task you have in mind. |
|
| Back to top |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 7995 Location: Warrington, UK
|
Posted: Tue Feb 07, 2006 12:46 am Post subject: RE: editing text files from within the shell |
|
|
If using sed, the s command only replaces content within a line, you need d to delete a complete line or block of lines.
Also, use the -i option to modify a file in place instead of sending pout put to a temporary file, deleting and renaming.
As Nigel says, a clearer idea of what you are doing will result in a clearer recommendation. _________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
snuffsaid
Joined: Mon Feb 06, 2006 10:24 pm Posts: 6 Location: West Wittering
|
Posted: Tue Feb 07, 2006 9:43 am Post subject: RE: editing text files from within the shell |
|
|
Sorry for not providing more info, I didn't want to waffle on too much. I have a thrid party application that limits the amount of logins using the application, but does not limit the number of times a user can log in. I have found a way of limiting the users to be only able to login a certain number of times by editing the apps startup script to refer to another file to see how many times that user is allowed to log in. Now I am trying to create a script that can be run from the command line so the person in charge doesn't have to use vi. All he would need to do is <command> -a (add), -r (remove) -m(modify) and so on. This would edit the file, whichis only about 20 lines long and is set out like "username:number of logins". The : can be replaced by anything really.
Hope I haven't waffled on too much and it's made things a little clearer
In the mean time I'll go and try the -i option
Many Thanks
Pete |
|
| Back to top |
|
 |
jjmac LXF regular
Joined: Fri Apr 08, 2005 2:32 am Posts: 1996 Location: Sydney, Australia
|
Posted: Wed Feb 08, 2006 8:21 am Post subject: RE: editing text files from within the shell |
|
|
Information isn't waffle .
Sounds like a job for "sed" though. A google on sed will come up with lots of diverse sources of information. Including histories, "examples" etc. Unfortuately the man page is quite lacking. I think that because sed has been around for such a long time, it isn't really centralised to the dgree where a single site will provide all the required documentation. So, as a result, it is scattered. Certainly, imo, one of the great unix tools.
jm |
|
| Back to top |
|
 |
nelz Moderator

Joined: Mon Apr 04, 2005 12:52 pm Posts: 7995 Location: Warrington, UK
|
Posted: Wed Feb 08, 2006 9:33 am Post subject: RE: editing text files from within the shell |
|
|
The sed manpage is limited, but the last paragraph tells you all you need to know, full documentation is in the info page.
You can do what you want with
| Code: |
#add
echo "${NAME}:${LIMIT}" >>configfile
#remove
sed -i "/^${NAME}:/d" configfile
#modify
sed -i "s/^${NAME}:.*/${NAME}:${LIMIT}/" configfile
|
You'll have to add the usual checks, like making sure you don't add a user twice or try to modify a non-existent user. _________________ Unix is user-friendly. It's just very selective about who it's friends are. |
|
| Back to top |
|
 |
jjmac LXF regular
Joined: Fri Apr 08, 2005 2:32 am Posts: 1996 Location: Sydney, Australia
|
Posted: Thu Feb 09, 2006 8:52 am Post subject: RE: editing text files from within the shell |
|
|
, wish i had looked at the info page at the time (good_grief.png)  _________________ http://counter.li.org
#313537
The FVWM wm -=- www.fvwm.org -=-
Somebody stole my air guitar, It happened just the other day,
But it's ok, 'cause i've got a spare ... |
|
| Back to top |
|
 |
alloydog LXF regular

Joined: Thu Apr 07, 2005 8:32 pm Posts: 600
|
Posted: Thu Feb 09, 2006 1:03 pm Post subject: Re: RE: editing text files from within the shell |
|
|
| jjmac wrote: | , wish i had looked at the info page at the time (good_grief.png)  |
You mean:
 |
|
| Back to top |
|
 |
snuffsaid
Joined: Mon Feb 06, 2006 10:24 pm Posts: 6 Location: West Wittering
|
Posted: Thu Feb 09, 2006 2:36 pm Post subject: RE: Re: RE: editing text files from within the shell |
|
|
a big thank you to you all. I was staring at the man page and even the "classic shell scripting" book late at night but just could not see for looking, as they say.
Cheers
Pete |
|
| Back to top |
|
 |
jjmac LXF regular
Joined: Fri Apr 08, 2005 2:32 am Posts: 1996 Location: Sydney, Australia
|
Posted: Sun Feb 12, 2006 2:20 pm Post subject: RE: Re: RE: editing text files from within the shell |
|
|
alloydog wrote:
>>
You mean:
>>
In a nut shell (hehehe) (um ,,, that is a jpg you know ! )
snuffsaid:
>>
I was staring at the man page and even the "classic shell scripting" book late at night but just could not see for looking,
>>
Tell me about it (grin), mind you, some of the google feed back on sed is worth the squize
jm _________________ http://counter.li.org
#313537
The FVWM wm -=- www.fvwm.org -=-
Somebody stole my air guitar, It happened just the other day,
But it's ok, 'cause i've got a spare ... |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|