LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-06-2009, 03:56 AM   #1
graziano1968
Member
 
Registered: Sep 2004
Posts: 223

Rep: Reputation: 30
how to remove a line from a txt file


Hello

how to remove (from console) a line from a txt file

suppose I have

housea
bhouse
house
bhousex



and I would remove the line house to have this

housea
bhouse
bhousex



I tried using

replace "house" "" -- file.txt

however it's not good because I receive this
a
b
bx



How to do ?

Thank you
 
Old 11-06-2009, 04:06 AM   #2
indiajoe
Member
 
Registered: Jan 2009
Location: India
Distribution: Porteus atma
Posts: 84

Rep: Reputation: 21
Use sed

Hi
Use sed as follows
Code:
sed '/^house$/d' file.txt
-Cheers
indiajoe

Last edited by indiajoe; 11-06-2009 at 04:07 AM.
 
Old 11-06-2009, 04:10 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk '$1!="house"' file
 
Old 11-06-2009, 04:32 AM   #4
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by ghostdog74 View Post
Code:
awk '$1!="house"' file
Could you please explain how this code works ? Thanks in adv.
 
Old 11-06-2009, 04:38 AM   #5
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Lightbulb

Even I struggles in these kinda problems in my starting days,

then I came across sed/awk. These two tools are God for shell scripting people.

For your case, if you want to delete any specific line (if you know the line no.)

Like in you case it is line 3
Code:
sed -i '3 d' file
Other than this you can also use grep command "-w" switch.
Code:
# cat file | grep -vw house
housea
bhouse
bhousex
Hope this helps.
 
Old 11-06-2009, 05:08 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by vikas027 View Post
Could you please explain how this code works ? Thanks in adv.
what is it that you don't understand? because its very simple. check field 1 against the whole word "house" using inequality. If field1 not equal "house", print.
 
Old 11-06-2009, 05:10 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by vikas027 View Post
Even I struggles in these kinda problems in my starting days,

then I came across sed/awk. These two tools are God for shell scripting people.
only 1 good tool is needed, awk. If you know awk, no need to use sed. their functions overlap. I don't really understand why people like to combine them together.


Quote:
Other than this you can also use grep command "-w" switch.
Code:
# cat file | grep -vw house
housea
bhouse
bhousex
Hope this helps.
no need cat.
Code:
grep -vw house file

Last edited by ghostdog74; 11-06-2009 at 05:12 AM.
 
1 members found this post helpful.
Old 11-06-2009, 08:19 AM   #8
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,091

Rep: Reputation: 41
Quote:
Originally Posted by ghostdog74 View Post
only 1 good tool is needed, awk. If you know awk, no need to use sed. their functions overlap. I don't really understand why people like to combine them together.



no need cat.
Code:
grep -vw house file
you can't make a direct edit to a file with just awk... That's where sed comes in.
 
Old 11-06-2009, 08:28 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by trey85stang View Post
you can't make a direct edit to a file with just awk... That's where sed comes in.
You mean, I assume: sed -i
Be aware of the risk in this if your sed regex is not correct

But you can of course redirect to a file from any utility--n'est-ce pas?
 
Old 11-06-2009, 09:04 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by trey85stang View Post
you can't make a direct edit to a file with just awk... That's where sed comes in.
that's only trivial. at the back end, sed is creating "temp file" as well, just that its not visible to you.
Code:
awk '{....}' file > temp;mv temp file
since you want to compare, i give you this: how about using sed to calculate sine of a number? or logarithm?
Code:
awk 'BEGIN{print sin(90)}' file
awk 'BEGIN{print log(10)}' file

Last edited by ghostdog74; 11-06-2009 at 09:07 AM.
 
Old 11-06-2009, 12:09 PM   #11
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by ghostdog74 View Post
what is it that you don't understand? because its very simple. check field 1 against the whole word "house" using inequality. If field1 not equal "house", print.
Got it
 
Old 11-06-2009, 06:39 PM   #12
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
For the record, I want to acknowledge that AWK is better than SED...........sometimes
 
Old 11-06-2009, 06:51 PM   #13
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by pixellany View Post
For the record, I want to acknowledge that AWK is better than SED...........sometimes
just curious, can you tell me when is the time that sed is better than awk, considering what they both can and cannot do?
 
Old 11-07-2009, 08:45 AM   #14
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by ghostdog74 View Post
just curious, can you tell me when is the time that sed is better than awk, considering what they both can and cannot do?
First, I was making a feeble attempt at humor....

You have me at a disadvantage--since I do not know AWK as well as I do SED. My impression is that SED will do certain kind of things faster and with less code.

Here's one thing I was just playing with (not sure if it is optimized for SED)
Code:
sed '=' filename | sed 'N;s/\n/\t/' > newfilename
Numbers the lines, with the number on the same line, with a tab.

What is the equivalent in AWK?

Last edited by pixellany; 11-07-2009 at 08:47 AM.
 
Old 11-07-2009, 10:06 AM   #15
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by pixellany View Post
Code:
sed '=' filename | sed 'N;s/\n/\t/' > newfilename
Numbers the lines, with the number on the same line, with a tab.

What is the equivalent in AWK?
The equivalent, with 1 invocation of awk process.
Code:
$ awk '{print NR"\t"$0}' file
Now let's see how they perform on big files
Code:
$ wc -l <file
3168000
$ head -5 file
this is line
this is line
this is line
this is line
this is line

$ time sed '=' file | sed 'N;s/\n/\t/' > /dev/null

real    0m10.224s
user    0m9.922s
sys     0m0.291s

$ time awk '{print NR"\t"$0}' file > /dev/null

real    0m7.997s
user    0m7.921s
sys     0m0.071s

$ time sed '=' file | sed 'N;s/\n/\t/' > /dev/null

real    0m10.233s
user    0m9.889s
sys     0m0.322s

$ time awk '{print NR"\t"$0}' file > /dev/null

real    0m7.963s
user    0m7.867s
sys     0m0.079s
awk runs faster than sed. I believe there should be a way to only invoke one sed process...maybe it could speed things up a bit, but overall in this test, awk=1,sed=0
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Strip / Remove attachments (Replace with TXT file if Possible) Vince0000 Linux - General 2 09-17-2010 06:46 PM
How can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 05:11 PM
Read a line in a txt file with bash orgazmo Programming 5 05-03-2005 07:10 AM
Read a line in a txt file with bash orgazmo Linux - Newbie 3 05-03-2005 04:16 AM
PERL:: trying to remove last line of file ocularbob Programming 16 09-01-2003 01:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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