LinuxQuestions.org
Social Bookmarking all things Linux and Open Source
Go Back   LinuxQuestions.org > Forums > Linux > Linux - General
User Name
Password
Linux - General This 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
 
Thread Tools
Old 11-06-2009, 04:56 AM   #1
graziano1968
Member
 
Registered: Sep 2004
Posts: 172
Thanked: 0
how to remove a line from a txt file


[Log in to get rid of this advertisement]
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
windows_xp_2003 graziano1968 is offline     Reply With Quote
Old 11-06-2009, 05:06 AM   #2
indiajoe
Member
 
Registered: Jan 2009
Location: India
Distribution: Slax atma
Posts: 48
Thanked: 9
Use sed

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

Last edited by indiajoe; 11-06-2009 at 05:07 AM..
linux indiajoe is offline     Reply With Quote
Old 11-06-2009, 05:10 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 1,815
Blog Entries: 5
Thanked: 116
Code:
awk '$1!="house"' file
linuxfedora ghostdog74 is offline     Reply With Quote
Old 11-06-2009, 05:32 AM   #4
vikas027
Member
 
Registered: May 2007
Distribution: Linux RHEL 5.2
Posts: 498
Thanked: 8
Quote:
Originally Posted by ghostdog74 View Post
Code:
awk '$1!="house"' file
Could you please explain how this code works ? Thanks in adv.
vikas027 is offline     Reply With Quote
Old 11-06-2009, 05:38 AM   #5
vikas027
Member
 
Registered: May 2007
Distribution: Linux RHEL 5.2
Posts: 498
Thanked: 8
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.
vikas027 is offline     Reply With Quote
Old 11-06-2009, 06:08 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 1,815
Blog Entries: 5
Thanked: 116
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.
linuxfedora ghostdog74 is offline     Reply With Quote
Thanked by:
Old 11-06-2009, 06:10 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 1,815
Blog Entries: 5
Thanked: 116
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 06:12 AM..
linuxfedora ghostdog74 is offline     Reply With Quote
Old 11-06-2009, 09:19 AM   #8
trey85stang
Member
 
Registered: Sep 2003
Location: Ft. Worth, TX
Distribution: Always.. Slackware 9.1
Posts: 945
Thanked: 0
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.
windows_xp_2003 trey85stang is offline     Reply With Quote
Old 11-06-2009, 09:28 AM   #9
pixellany
Moderator
 
Registered: Nov 2005
Location: Pasadena, CA
Distribution: Arch
Posts: 13,173
Thanked: 285
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?
linux pixellany is offline     Reply With Quote
Old 11-06-2009, 10:04 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 1,815
Blog Entries: 5
Thanked: 116
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 10:07 AM..
linuxfedora ghostdog74 is offline     Reply With Quote
Old 11-06-2009, 01:09 PM   #11
vikas027
Member
 
Registered: May 2007
Distribution: Linux RHEL 5.2
Posts: 498
Thanked: 8
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
vikas027 is offline     Reply With Quote
Old 11-06-2009, 07:39 PM   #12
pixellany
Moderator
 
Registered: Nov 2005
Location: Pasadena, CA
Distribution: Arch
Posts: 13,173
Thanked: 285
For the record, I want to acknowledge that AWK is better than SED...........sometimes
linux pixellany is offline     Reply With Quote
Old 11-06-2009, 07:51 PM   #13
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 1,815
Blog Entries: 5
Thanked: 116
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?
linuxfedora ghostdog74 is offline     Reply With Quote
Old 11-07-2009, 09:45 AM   #14
pixellany
Moderator
 
Registered: Nov 2005
Location: Pasadena, CA
Distribution: Arch
Posts: 13,173
Thanked: 285
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 09:47 AM..
linux pixellany is offline     Reply With Quote
Old 11-07-2009, 11:06 AM   #15
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 1,815
Blog Entries: 5
Thanked: 116
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
linuxfedora ghostdog74 is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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 can read from file.txt C++ where can save this file(file.txt) to start reading sam_22 Programming 1 01-11-2007 06:11 PM
Read a line in a txt file with bash orgazmo Programming 5 05-03-2005 08:10 AM
Read a line in a txt file with bash orgazmo Linux - Newbie 3 05-03-2005 05:16 AM
Help Strip / Remove attachments (Replace with TXT file if Possible) Vince0000 Linux - General 0 07-29-2004 01:17 PM
PERL:: trying to remove last line of file ocularbob Programming 16 09-01-2003 02:07 PM


All times are GMT -5. The time now is 11:21 PM.

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration