Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

editing text files from within the shell

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
snuffsaid



Joined: Mon Feb 06, 2006 10:24 pm
Posts: 6
Location: West Wittering

PostPosted: Mon Feb 06, 2006 10:48 pm    Post subject: editing text files from within the shell Reply with quote

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
View user's profile Send private message
Nigel
LXF regular


Joined: Fri Apr 08, 2005 9:03 pm
Posts: 1141
Location: Gloucestershire, UK

PostPosted: Mon Feb 06, 2006 11:53 pm    Post subject: RE: editing text files from within the shell Reply with quote

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
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8467
Location: Warrington, UK

PostPosted: Tue Feb 07, 2006 12:46 am    Post subject: RE: editing text files from within the shell Reply with quote

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)
Back to top
View user's profile Send private message
snuffsaid



Joined: Mon Feb 06, 2006 10:24 pm
Posts: 6
Location: West Wittering

PostPosted: Tue Feb 07, 2006 9:43 am    Post subject: RE: editing text files from within the shell Reply with quote

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
View user's profile Send private message
jjmac
LXF regular


Joined: Fri Apr 08, 2005 2:32 am
Posts: 1996
Location: Sydney, Australia

PostPosted: Wed Feb 08, 2006 8:21 am    Post subject: RE: editing text files from within the shell Reply with quote

Information isn't waffle Smile.

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
View user's profile Send private message
nelz
Site admin


Joined: Mon Apr 04, 2005 12:52 pm
Posts: 8467
Location: Warrington, UK

PostPosted: Wed Feb 08, 2006 9:33 am    Post subject: RE: editing text files from within the shell Reply with quote

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.
_________________
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
Back to top
View user's profile Send private message
jjmac
LXF regular


Joined: Fri Apr 08, 2005 2:32 am
Posts: 1996
Location: Sydney, Australia

PostPosted: Thu Feb 09, 2006 8:52 am    Post subject: RE: editing text files from within the shell Reply with quote

Rolling Eyes , wish i had looked at the info page at the time (good_grief.png) Rolling Eyes
_________________
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 user's profile Send private message
alloydog
LXF regular


Joined: Thu Apr 07, 2005 8:32 pm
Posts: 600

PostPosted: Thu Feb 09, 2006 1:03 pm    Post subject: Re: RE: editing text files from within the shell Reply with quote

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

You mean:

Back to top
View user's profile Send private message
snuffsaid



Joined: Mon Feb 06, 2006 10:24 pm
Posts: 6
Location: West Wittering

PostPosted: Thu Feb 09, 2006 2:36 pm    Post subject: RE: Re: RE: editing text files from within the shell Reply with quote

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
View user's profile Send private message
jjmac
LXF regular


Joined: Fri Apr 08, 2005 2:32 am
Posts: 1996
Location: Sydney, Australia

PostPosted: Sun Feb 12, 2006 2:20 pm    Post subject: RE: Re: RE: editing text files from within the shell Reply with quote

alloydog wrote:

>>
You mean:
>>

In a nut shell Smile (hehehe) (um ,,, that is a jpg you know ! Rolling Eyes )


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 ...
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast