LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-17-2012, 07:22 AM   #1
Kashif_Bash
LQ Newbie
 
Registered: Apr 2012
Posts: 20

Rep: Reputation: Disabled
delete all lines of a file except one line


i want to write a script to delete all lines of files except a line with specific word. like delete every line but not one with "media" word.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 04-17-2012, 07:26 AM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Hi and welcome to LQ.

Have a look at a sed tutorial. Some hints:
-n
-i
/<media/>
p
 
Old 04-17-2012, 07:43 AM   #3
Kashif_Bash
LQ Newbie
 
Registered: Apr 2012
Posts: 20

Original Poster
Rep: Reputation: Disabled
Yes, I have been using sed but wasn't getting to the point that how to delete everything except that line which contains media.
 
Old 04-17-2012, 07:45 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
It would be more helpful if you provided an actual example of the text, to help avoid any trouble spots in our answers. It's hard to give specific advice when we have to guess the requirements.

In any case, I'd now recommend using ed instead of sed for simple modifications like this, as it's an actual text editor, and should be fully portable across platforms.

Code:
printf '%s\n' 'v/pattern/d' ',p' 'w' | ed -s file.txt
First, I used printf pipe the commands into ed, separated by newlines.

'v' runs the command on all lines that don't contain '/pattern/', and 'd' deletes them.

The ',p' command prints it to the screen (for confirmation), and the 'w' command writes the changes back to the file (only use it when you're ready to commit).

How to use ed:
http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)

It's also possible to use vi/vim in a similar way.
 
2 members found this post helpful.
Old 04-17-2012, 08:01 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,923

Rep: Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319Reputation: 7319
Code:
grep "specific word" filename > newfilename
mv newfilename filename
will give you the result
 
1 members found this post helpful.
Old 04-17-2012, 08:43 AM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
When it comes to sed, it would be easier in your case to think: print only lines containing the pattern instead of delete everything apart.......
Code:
sed -n '/media/p' file
See also the -i flag for permanent chages.

Last edited by sycamorex; 04-17-2012 at 01:47 PM.
 
Old 04-17-2012, 01:40 PM   #7
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by sycamorex View Post
sed -n '/media/p' file
Or, if you are stuck with a sed that does not support the -n option,
Code:
sed -e '/media/ p; d' FILE(s)...
 
Old 04-25-2012, 04:37 AM   #8
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
Code:
echo "$(grep 'word' filename)" > filename
Will work also.
 
  


Reply



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
how to copy some lines in a file and delete these lines after gartura Linux - General 1 07-20-2010 08:55 AM
find a string in a file and delete that line as well as x lines after tdnnash25 Linux - Server 3 06-24-2010 02:13 PM
Delete Duplicate Lines in a file, leaving only the unique lines left xmrkite Linux - Software 6 01-14-2010 06:18 PM
How to delete lines and insert another line in some xml files using a shell script? luananeko Linux - Newbie 1 07-20-2009 09:53 PM
How to compare two lines and delete the duplicate line from a file? Shobhna Linux - Newbie 10 12-05-2008 01:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:44 PM.

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
Open Source Consulting | Domain Registration