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 

Edit a file via shell script

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



Joined: Thu Apr 27, 2006 4:05 pm
Posts: 2

PostPosted: Thu Apr 27, 2006 4:12 pm    Post subject: Edit a file via shell script Reply with quote

I'm trying to edit a file via a shell script... I have 1 script and one file. The text file is made up of 5 lines e.g.
james 1111 98987678
John 2222 848474838

the 1111 and 2222 are ID's.
I want to run my script and delete the line with the ID i specify. e.g.
./script textfile -d 2222 - this will delete line with id 2222?

How do i do this?
Back to top
View user's profile Send private message
crispibits
LXF regular


Joined: Thu Jun 30, 2005 1:33 pm
Posts: 201
Location: Bath

PostPosted: Thu Apr 27, 2006 7:45 pm    Post subject: RE: Edit a file via shell script Reply with quote

I don't want to ruin the joy of working out the whole script, but the following will delete a line where it contains 222. See if this makes sense, and take it from there...

Code:
sed -e '/222/d' textfile

_________________
I'm not a spammer - honest!
Back to top
View user's profile Send private message
SSL



Joined: Thu Apr 27, 2006 4:05 pm
Posts: 2

PostPosted: Wed May 03, 2006 11:08 am    Post subject: Reply with quote

Hi,

Thanks for that... I now have it working as well as other options but i'm now stuck on the final part...

I'm using sed to replace the phone number in a file.e.g.

./script 1 2222 999 (This would replace the phone number for ID 2222 to 999)? I can get it to work if i specify the new and old number in the syntax when i run the script but not when i use just the ID and new number?

Is there a command to replace just the third word?

Any ideas?

James.
Back to top
View user's profile Send private message
crispibits
LXF regular


Joined: Thu Jun 30, 2005 1:33 pm
Posts: 201
Location: Bath

PostPosted: Wed May 03, 2006 1:18 pm    Post subject: Reply with quote

For this you'd use awk:
Code:
awk '/^1 / {$3 = "999"} {print $0}' testfile

This means 'if the line starts with 1 followed by a space, set the third field to be 999. Regardless of what came before, print the whole line.
Awk assumes the field separator to be a space, but you can alter that using the -F flag.
_________________
I'm not a spammer - honest!
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