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?

Recommended Answers

All 12 Replies

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

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?

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.

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.

i used
echo text >> file

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

Thx again

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?

Be mindful of how the various shell quoting systems work.

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

But beware of users typing in a /

[edited]

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)
#! /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:

  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

> Not all previous answers were exactly correct. You can edit a file in-place.
OK, show me.

all what salem recommended to use, did work perfectly for me, and my script is running well.
i was also told by others that file cant be edited in place .. but if such way exists, i would like to learn it.

> You can pipe commands to ex(1) to edit a file 'in place'.
Like I said in my first post, the editors like ed, ex and vi merely hide the detail from you. There's still a write / delete / rename going on underneath.

commented: Abstraction layers are good thing. ;) +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.