Computing.Net > Forums > Unix > removing specific line from file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

removing specific line from file

Reply to Message Icon

Name: JelloPhreak
Date: May 18, 2003 at 04:45:20 Pacific
OS: Darwin
CPU/Ram: PPC603e
Comment:

I'm trying to remove a specific line from a file
based on a user defined input. I had found a
similar situation where sed was used, but when I
tried using it in my script, sed had trouble with the
variable input, but would work fine if I put in a
number within the text editor. Any suggestions on
how I might be able to get this to work?



Sponsored Link
Ads by Google

Response Number 1
Name: David Perry
Date: May 18, 2003 at 06:10:51 Pacific
Reply:

If you include a variable, the sed command needs to be surrounded with double, not single quotes

HOME=/usr/home/bob

sed -e "s/${HOME}/replacement_string/" filename

sed -e "#~^.*${HOME}.*#d" filename

The -v flag to grep says to print all lines except those contianing that string

grep -v $HOME filename

will print all lines except those containing /usr/home/bob

If this doesn't help, please post what you have and someone can help you fix it.


0

Response Number 2
Name: JelloPhreak
Date: May 19, 2003 at 04:13:05 Pacific
Reply:

Thanks for the help... I'm teaching myself shell
scripting, so I still had a little trouble with sed, but
I'm sure its because of my lack of expeirience. I
did mange to get my script to work exactly as I
wanted tho... heres how.

The script first displays several lines from a file
and numbers them, then asks which line should
be deleted....

read NUMBER
DELETE=`tail +$NUMBER file | head -1`
grep -v $DELETE file > file1
mv file1 file

I'm not sure if this is the most efficient way to get
the job done, but it works fine for my needs. I'm
always open to suggestions for better methods, as
I'm just doing this for the sake of learning.


0

Response Number 3
Name: JelloPhreak
Date: May 19, 2003 at 10:57:54 Pacific
Reply:

One more little speed bump...
this script worked fine in a test, but when I applied
it towards my actual script, I came across a
problem, which I'm sure is something simple, but
could not stumble across on my own.
This is for an invoice program for myself.... the file
contains lines that include...
MM/DD/YY Dollar amount Client
the spaces are tripping up my grep -v command.
The line is called as a variable...
grep -v $DELETE file > file1
I tried single and double quotes around the
$DELETE variable, but it still get...

grep: Money: No such file or directory
grep: Clent: No such file or directory

I just need to know how to get grep to see the
variable as a single statement


0

Response Number 4
Name: JelloPhreak
Date: May 19, 2003 at 15:14:57 Pacific
Reply:

Think I got it... I used...
grep -vE "$VARIABLE" which seems to be workin
fine


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: removing specific line from file

extract a specific line from file www.computing.net/answers/unix/extract-a-specific-line-from-file/5381.html

to remove duplicate lines from file www.computing.net/answers/unix/to-remove-duplicate-lines-from-file/7011.html

Remove blank lines from file www.computing.net/answers/unix/remove-blank-lines-from-file/3887.html