| | |
Scripts and editing files
Thread Solved |
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?
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?

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?
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?

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.
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.
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.
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.

•
•
Join Date: Aug 2007
Posts: 165
Reputation:
Solved Threads: 18
•
•
•
•
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?
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)
#! /bin/sh echo "Enter CMD COLOR, where CMD is either a (add) or d (delete), and COLOR is the alphanumeric name of a color." echo echo "Press <CTRL/D> when finished." while read cmd color; do # First, drop all non-alphanumeric characters safecolor=`echo $color|sed -e 's=[^a-zA-Z0-9]==g'` # Now take action case $cmd in a) echo $safecolor >> colors;; d) echo -e "/$safecolor/d\nw" | ex colors;; *) echo "Unknown command '$cmd' given!";; esac done
Two things to note here:
- Color names are limited to only alphanumerics
- In shells, stuff inside single quotes prevents expansion of shell variables, while stuff inside double quotes allows variable expansion
- 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.
![]() |
Similar Threads
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
- hey, any 1 interested in helping out for scripting (Game Development)
- Assigning IPs to one NIC (*nix Software)
- Please check this log to see if I'm clean (Viruses, Spyware and other Nasties)
- This Should be Easy for You Guys! (Linux Servers and Apache)
- Fake Microsoft Windows Security Warning (Viruses, Spyware and other Nasties)
Other Threads in the Shell Scripting Forum
- Previous Thread: Birthday Calculation
- Next Thread: redirect output of a CMD to file
| Thread Tools | Search this Thread |






