943,728 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Dec 25th, 2008
0

Scripts and editing files

Expand Post »
hello,
i'm new with scripts and linux and need a little help.

i made a text file called colors using a vi editor and contains as follows:
red
green
blue
gold


now i want to delete the 2nd line with the word green using a script file. well i know how to do it while being in the vi editor of that text, simply use the dd on the 2nd line .. but how can i do that using a script ? what commands to write there?
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
Blackeagle is offline Offline
47 posts
since Apr 2008
Dec 25th, 2008
0

Re: Scripts and editing files

Do you want to delete it because it's line 2, or because it's green?

Read up on the sed command.

For example
sed '2d' file > newfile
or
sed '/green/d' file > newfile
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 25th, 2008
0

Re: Scripts and editing files

yeah i want to delete the line with word green.

your command did work well but it doesnt change the original text file, it rather sends the new edited text to the newfile.
is there any way i can change the same file i'm using and save the cahnges?

and may i ask something else? suppose i want to add a new color to the end of the file, how is that possible?
Reputation Points: 10
Solved Threads: 1
Light Poster
Blackeagle is offline Offline
47 posts
since Apr 2008
Dec 25th, 2008
0

Re: Scripts and editing files

Nothing works by editing the file 'in place', even your vi editor.

It's all write the new file, delete the old file, then rename. It's just vi hides those steps from you. On very rare occasions when something crashes, you'll see (and be glad of it) that the old file was preserved and the new file was broken.

Do the same thing in any script you write.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 25th, 2008
0

Re: Scripts and editing files

aha .. i see .. i understand and it work fine with me.

just one more and last question, how can i add to the end of the file a certain row? can i use the sed command as before?
i tried to write: sed 'Gi someText' file > newfile
but it didnt work at all.
Last edited by Blackeagle; Dec 25th, 2008 at 9:36 am.
Reputation Points: 10
Solved Threads: 1
Light Poster
Blackeagle is offline Offline
47 posts
since Apr 2008
Dec 25th, 2008
0

Re: Scripts and editing files

i used
echo text >> file

and it adds to the end of file .. Thank you for helping Salem. this thread is solved.

Thx again
Reputation Points: 10
Solved Threads: 1
Light Poster
Blackeagle is offline Offline
47 posts
since Apr 2008
Dec 25th, 2008
0

Re: Scripts and editing files

Click to Expand / Collapse  Quote originally posted by Salem ...
sed '/green/d' file > newfile
Salem, what if the user enters a certain color like this:
echo enter color
read choice

and if i write the following:
sed '/$choice/d' file > newfile

it doesnt really work!
how can i put the value of choice in that command?
Reputation Points: 10
Solved Threads: 1
Light Poster
Blackeagle is offline Offline
47 posts
since Apr 2008
Dec 25th, 2008
0

Re: Scripts and editing files

Be mindful of how the various shell quoting systems work.

Eg, try
sed '/'$choice'/d' file > newfile

But beware of users typing in a /
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Dec 25th, 2008
0

Re: Scripts and editing files

[edited]
Last edited by Aia; Dec 25th, 2008 at 6:46 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Dec 30th, 2008
0

Re: Scripts and editing files

Click to Expand / Collapse  Quote originally posted by Blackeagle ...
yeah i want to delete the line with word green.

your command did work well but it doesnt change the original text file, it rather sends the new edited text to the newfile.
is there any way i can change the same file i'm using and save the cahnges?

and may i ask something else? suppose i want to add a new color to the end of the file, how is that possible?
Not all previous answers were exactly correct. You can edit a file in-place.

Here's some semi-advanced shell scripting. It demonstrates
  • reading from the terminal in a loop
  • deleting 'dangerous' characters from entered data
  • the case statement
  • piping commands to ex(1)
  • appending to a file
  • and the use of control-D as end of input (EOF)
Shell Scripting Syntax (Toggle Plain Text)
  1. #! /bin/sh
  2.  
  3. echo "Enter CMD COLOR, where CMD is either a (add) or d (delete), and COLOR is the alphanumeric name of a color."
  4. echo
  5. echo "Press <CTRL/D> when finished."
  6. while read cmd color; do
  7. # First, drop all non-alphanumeric characters
  8. safecolor=`echo $color|sed -e 's=[^a-zA-Z0-9]==g'`
  9. # Now take action
  10. case $cmd in
  11. a) echo $safecolor >> colors;;
  12. d) echo -e "/$safecolor/d\nw" | ex colors;;
  13. *) echo "Unknown command '$cmd' given!";;
  14. esac
  15. done

Two things to note here:
  1. Color names are limited to only alphanumerics
  2. In shells, stuff inside single quotes prevents expansion of shell variables, while stuff inside double quotes allows variable expansion
  3. You can pipe commands to ex(1) to edit a file 'in place'.

N
Last edited by Fest3er; Dec 30th, 2008 at 10:06 pm.
Reputation Points: 51
Solved Threads: 35
Posting Whiz in Training
Fest3er is offline Offline
238 posts
since Aug 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Birthday Calculation
Next Thread in Shell Scripting Forum Timeline: redirect output of a CMD to file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC