editing text files from within the shell

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

editing text files from within the shell

Postby snuffsaid » Mon Feb 06, 2006 10:48 pm

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
snuffsaid
 
Posts: 6
Joined: Mon Feb 06, 2006 10:24 pm
Location: West Wittering

RE: editing text files from within the shell

Postby Nigel » Mon Feb 06, 2006 11:53 pm

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.
User avatar
Nigel
LXF regular
 
Posts: 1141
Joined: Fri Apr 08, 2005 8:03 pm
Location: Gloucestershire, UK

RE: editing text files from within the shell

Postby nelz » Tue Feb 07, 2006 12:46 am

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.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8579
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

RE: editing text files from within the shell

Postby snuffsaid » Tue Feb 07, 2006 9:43 am

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
snuffsaid
 
Posts: 6
Joined: Mon Feb 06, 2006 10:24 pm
Location: West Wittering

RE: editing text files from within the shell

Postby jjmac » Wed Feb 08, 2006 8:21 am

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
jjmac
LXF regular
 
Posts: 1996
Joined: Fri Apr 08, 2005 1:32 am
Location: Sydney, Australia

RE: editing text files from within the shell

Postby nelz » Wed Feb 08, 2006 9:33 am

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: Select all
#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.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8579
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

RE: editing text files from within the shell

Postby jjmac » Thu Feb 09, 2006 8:52 am

:roll: , wish i had looked at the info page at the time (good_grief.png) :roll:
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 ...
jjmac
LXF regular
 
Posts: 1996
Joined: Fri Apr 08, 2005 1:32 am
Location: Sydney, Australia

Re: RE: editing text files from within the shell

Postby alloydog » Thu Feb 09, 2006 1:03 pm

jjmac wrote::roll: , wish i had looked at the info page at the time (good_grief.png) :roll:

You mean:

Image
User avatar
alloydog
LXF regular
 
Posts: 600
Joined: Thu Apr 07, 2005 7:32 pm

RE: Re: RE: editing text files from within the shell

Postby snuffsaid » Thu Feb 09, 2006 2:36 pm

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
snuffsaid
 
Posts: 6
Joined: Mon Feb 06, 2006 10:24 pm
Location: West Wittering

RE: Re: RE: editing text files from within the shell

Postby jjmac » Sun Feb 12, 2006 2:20 pm

alloydog wrote:

>>
You mean:
>>

In a nut shell :) (hehehe) (um ,,, that is a jpg you know ! :roll: )


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 :wink:

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 ...
jjmac
LXF regular
 
Posts: 1996
Joined: Fri Apr 08, 2005 1:32 am
Location: Sydney, Australia


Return to Programming

Who is online

Users browsing this forum: No registered users and 2 guests