LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-11-2010, 06:04 AM   #1
mkohler
LQ Newbie
 
Registered: Nov 2010
Posts: 17

Rep: Reputation: 0
Delete Line from File


Hi,

Just new to this and I was creating a few scripts, I am trying to create a restore script to restore files that have previously been moved to a folder I selected. When moved I stored the original path for these files all in one text file.

I want to know if anyone can tell me how to delete the line from the file after using it restore the file. I have used the grep command to search through the file "pathName" to find where the file was stored but now want to delete that same line. Any help would be really appreciated?

Thanks
 
Old 11-11-2010, 06:31 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Hi and welcome to LinuxQuestions!

You can edit files from the command line using sed. For example, suppose you want to delete a line containing a certain pattern (string), you can try something like:
Code:
sed -i.bck '/pattern/d' file
where 'd' is the delete command in sed. The -i option means that the file is actually changed, otherwise the result would be sent to the standard output and the original file left untouched. The -i.bck specification forces sed to do a backup copy of the original file (safe for testing purposes). The copy will have the name of the file with the specified suffix (.bck) appended.

To learn about sed secrets, a good tutorial is http://www.grymoire.com/Unix/Sed.html. Hope this helps.
 
Old 11-11-2010, 07:06 AM   #3
mkohler
LQ Newbie
 
Registered: Nov 2010
Posts: 17

Original Poster
Rep: Reputation: 0
Thanks for that this works how do I change it so that it uses $1 which is what the user inputs, I tried just putting $1 instead of pattern and also tried '$1' any ideas?

Thanks again really appreciated.
 
Old 11-11-2010, 07:23 AM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,825
Blog Entries: 1

Rep: Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221
Try double quotes "/$1/d"
 
Old 11-11-2010, 07:24 AM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
You probably want to put the expression in double quotes, e.g. "/$1/d".
 
Old 11-11-2010, 08:33 AM   #6
mkohler
LQ Newbie
 
Registered: Nov 2010
Posts: 17

Original Poster
Rep: Reputation: 0
that's perfect! works now, still trying to get used to the syntax takes a lot of time to get used to
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete a line from a file pinga123 Linux - Newbie 7 11-05-2009 08:06 AM
Perl question: delete line from text file with duplicate match at beginning of line mrealty Programming 7 04-01-2009 07:46 PM
Delete a line in a file by script hraposo Linux - General 2 08-16-2007 03:23 PM
delete a line containing a pattern and the next line of a text file powah Programming 3 01-31-2007 06:34 PM
Delete line from file nebbus Linux - Newbie 3 06-03-2006 01:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration