The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Delete first line from file and more.... dfb500 Shell Programming and Scripting 6 07-02-2009 11:04 AM
Delete a line from a file rohini UNIX for Dummies Questions & Answers 8 01-07-2009 06:34 AM
How to delete line from a file ramezernest UNIX for Dummies Questions & Answers 0 11-01-2007 11:56 AM
delete line in file with sed forever_49ers Shell Programming and Scripting 8 09-12-2007 09:39 AM
Delete line(s) from file elchalateco UNIX for Dummies Questions & Answers 4 10-11-2002 02:25 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-17-2005
dta4316 dta4316 is offline
Registered User
  
 

Join Date: May 2005
Posts: 3
delete line in a file

Hi all,
I would like to delete a line in file.txt which has a list of filename.
example:
file_a
file_b
file_c

lets say i have file_b in my directory, then i should delete file_b in file.txt and get output file.txt with only file_a and file_b.

Below is my script, my result is still
file_a
file_b
file_c
Anyone can help???

#!/bin/bash
while read line
do
if [[ -r $line ]]
then
sed -e '/$line/d' $1 > $1.tmp
mv $1.tmp $1
else
echo "file not readable"
fi
done < $1
  #2 (permalink)  
Old 05-17-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
while read line

waits for your input each time. Is that what you need ? Or do you need a script which will check your file contents against the directory listing and then carry out your requirement.

vino
  #3 (permalink)  
Old 05-17-2005
RishiPahuja's Avatar
RishiPahuja RishiPahuja is offline
Registered User
  
 

Join Date: Apr 2005
Location: Bangalore, India
Posts: 203
Thumbs up Try this

sed '/file_b/d' file.txt > new_file.txt
  #4 (permalink)  
Old 05-17-2005
strike strike is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 32
Quote:
Originally Posted by RishiPahuja
sed '/file_b/d' file.txt > new_file.txt
just wandering, is there a way to delete the line out of "file.txt" without having to redirect it to a "new_file.txt" , or do we have flush "file.txt" and copy "new_file.txt" into it?
  #5 (permalink)  
Old 05-17-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
Quote:
Originally Posted by strike
just wandering, is there a way to delete the line out of "file.txt" without having to redirect it to a "new_file.txt" , or do we have flush "file.txt" and copy "new_file.txt" into it?
/bin/echo "/$line/d\nwq!" | ex -s file.txt
  #6 (permalink)  
Old 05-17-2005
strike strike is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 32
yep that works. i was just curious
why does this work
/bin/echo "/$line/d\nwq!" | ex -s file.txt
but not this?
echo "/$line/d\nwq!" | ex -s file.txt
  #7 (permalink)  
Old 05-17-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,122
Quote:
Originally Posted by strike
yep that works. i was just curious
why does this work
/bin/echo "/$line/d\nwq!" | ex -s file.txt
but not this?
echo "/$line/d\nwq!" | ex -s file.txt
'echo' is a built-in in bash/ksh which has a different syntax than the /bin/echo
Closed Thread

Bookmarks

Tags
sed delete line

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:39 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0