954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Scripts and editing files

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?

Blackeagle
Light Poster
47 posts since Apr 2008
Reputation Points: 10
Solved Threads: 1
 

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

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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?

Blackeagle
Light Poster
47 posts since Apr 2008
Reputation Points: 10
Solved Threads: 1
 

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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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.

Blackeagle
Light Poster
47 posts since Apr 2008
Reputation Points: 10
Solved Threads: 1
 

i used
echo text >> file

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

Thx again

Blackeagle
Light Poster
47 posts since Apr 2008
Reputation Points: 10
Solved Threads: 1
 
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?

Blackeagle
Light Poster
47 posts since Apr 2008
Reputation Points: 10
Solved Threads: 1
 

Be mindful of how the various shell quoting systems work.

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

But beware of users typing in a /

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

[edited]

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

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. Youcan edit a file in-place.

Here's some semi-advanced shell scripting. It demonstratesreading 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: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

Fest3er
Posting Whiz in Training
242 posts since Aug 2007
Reputation Points: 51
Solved Threads: 35
 

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

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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.

Blackeagle
Light Poster
47 posts since Apr 2008
Reputation Points: 10
Solved Threads: 1
 

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

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You