| View previous topic :: View next topic |
| Author |
Message |
SSL
Joined: Thu Apr 27, 2006 4:05 pm Posts: 2
|
Posted: Thu Apr 27, 2006 4:12 pm Post subject: Edit a file via shell script |
|
|
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 |
|
 |
crispibits LXF regular

Joined: Thu Jun 30, 2005 1:33 pm Posts: 201 Location: Bath
|
Posted: Thu Apr 27, 2006 7:45 pm Post subject: RE: Edit a file via shell script |
|
|
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 |
|
 |
SSL
Joined: Thu Apr 27, 2006 4:05 pm Posts: 2
|
Posted: Wed May 03, 2006 11:08 am Post subject: |
|
|
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 |
|
 |
crispibits LXF regular

Joined: Thu Jun 30, 2005 1:33 pm Posts: 201 Location: Bath
|
Posted: Wed May 03, 2006 1:18 pm Post subject: |
|
|
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 previous topic :: View next topic |
|