Scripts and editing files

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2008
Posts: 47
Reputation: Blackeagle is an unknown quantity at this point 
Solved Threads: 1
Blackeagle's Avatar
Blackeagle Blackeagle is offline Offline
Light Poster

Scripts and editing files

 
0
  #1
Dec 25th, 2008
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?
Black Eagle
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Scripts and editing files

 
0
  #2
Dec 25th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 47
Reputation: Blackeagle is an unknown quantity at this point 
Solved Threads: 1
Blackeagle's Avatar
Blackeagle Blackeagle is offline Offline
Light Poster

Re: Scripts and editing files

 
0
  #3
Dec 25th, 2008
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?
Black Eagle
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Scripts and editing files

 
0
  #4
Dec 25th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 47
Reputation: Blackeagle is an unknown quantity at this point 
Solved Threads: 1
Blackeagle's Avatar
Blackeagle Blackeagle is offline Offline
Light Poster

Re: Scripts and editing files

 
0
  #5
Dec 25th, 2008
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.
Black Eagle
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 47
Reputation: Blackeagle is an unknown quantity at this point 
Solved Threads: 1
Blackeagle's Avatar
Blackeagle Blackeagle is offline Offline
Light Poster

Re: Scripts and editing files

 
0
  #6
Dec 25th, 2008
i used
echo text >> file

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

Thx again
Black Eagle
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 47
Reputation: Blackeagle is an unknown quantity at this point 
Solved Threads: 1
Blackeagle's Avatar
Blackeagle Blackeagle is offline Offline
Light Poster

Re: Scripts and editing files

 
0
  #7
Dec 25th, 2008
Originally Posted by Salem View Post
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?
Black Eagle
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Scripts and editing files

 
0
  #8
Dec 25th, 2008
Be mindful of how the various shell quoting systems work.

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

But beware of users typing in a /
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Scripts and editing files

 
0
  #9
Dec 25th, 2008
[edited]
Last edited by Aia; Dec 25th, 2008 at 6:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 165
Reputation: Fest3er is an unknown quantity at this point 
Solved Threads: 18
Fest3er Fest3er is offline Offline
Junior Poster

Re: Scripts and editing files

 
0
  #10
Dec 30th, 2008
Originally Posted by Blackeagle View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC