Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.