LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 09-28-2011, 07:24 AM   #1
ameylimaye
LQ Newbie
 
Registered: Aug 2011
Posts: 25

Rep: Reputation: Disabled
how do you delete unwanted rows from a file?


i want to remove unwanted rows...for example.
a file has few rows like these..

AAFD
ADDJK
SDFJK
KLAS
=text

AFkls
lksejf

what do i do to remove rows like "=text" and the blank row?
can anyone help me!?
 
Old 09-28-2011, 07:29 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Use sed. If the blank line is always immediately after =text:
Code:
sed -i.bck '/=text/,/^$/d' file
otherwise use two different expressions:
Code:
sed -i.bck -e '/=text/d' -e '/^$/d' file
 
Old 09-28-2011, 07:29 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Here's a sed solution:
Code:
sed -r '/(=text|^$)/d' infile
This looks for (all between / and /) =text or ^$ (empty line) and removes it (the d).

Hope this helps.
 
1 members found this post helpful.
Old 09-28-2011, 07:30 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
There's a tutorial for sed here.
 
Old 09-28-2011, 07:31 AM   #5
ameylimaye
LQ Newbie
 
Registered: Aug 2011
Posts: 25

Original Poster
Rep: Reputation: Disabled
Thank you
 
Old 09-28-2011, 07:34 AM   #6
snooly
Member
 
Registered: Sep 2011
Posts: 124

Rep: Reputation: Disabled
The grep command is what you're looking for. Grep searches for lines that match a pattern, and prints those lines.

So for example, you don't like "=text" lines. Suppose your file is called xyz.txt, try this:

grep -v '=text' xyz.txt

As you will learn when you read the fabulous grep manpage with "man grep", the -v option tells grep to print lines that don't match the pattern. So in this case, it will print lines that don't have "=text" anywhere in them. Maybe you only want to match lines which have "=text" at the start, in which case we get a bit deeper into "regular expressions", by putting a caret to anchor the match to the start of the line:

grep -v '^=text' xyz.txt

To remove blank lines, you need to know that just like caret ^ matches the start of a line, dollar $ matches the end of a line. A blank line is a line where the start is right next to the end. So you can get rid of blank lines like this:

grep -v '^$' xyz.txt

Suppose you wanted to remove both the =text lines and blank lines, and put the result in a file called "answer.txt", you can use pipes and redirects and use grep twice, like this:

grep -v '^=text' xyz.txt | grep -v '^$' > answer.txt

See? Easy when you know how!
 
Old 09-28-2011, 07:38 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
@snooly: Yep, grep can also be used.

Code:
grep -v '^=text' xyz.txt | grep -v '^$' > answer.txt
If you use egrep you can make that shorter/more elegant:
Code:
egrep -v "=text|^$" infile > answer.txt
It is indeed easy if you know how
 
Old 09-28-2011, 07:40 AM   #8
snooly
Member
 
Registered: Sep 2011
Posts: 124

Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
If you use egrep you can make that shorter/more elegant:
I just like pipes! Also I like unnecessary use of cat.

cat xyz.txt | grep -v '^=text' | cat | cat | cat | grep -v '^$' > answer.txt
 
  


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
[SOLVED] Delete rows based on values in a column using sed captainentropy Linux - Newbie 6 01-19-2011 09:59 AM
How do I delete unwanted folders gael33 Linux - Newbie 4 07-23-2010 02:36 AM
Delete Selected Rows in Perl/CGI ashok.g Programming 5 04-06-2010 11:30 AM
Best way to delete old unwanted log files?? xmdms Linux - Newbie 6 10-27-2008 10:36 AM
How to delete unwanted kernels? ludwig W Linux - Software 4 04-14-2003 08:00 AM


All times are GMT -5. The time now is 08:45 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