Need help stripping statement from text file, ksh: sed awk?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Need help stripping statement from text file, ksh: sed awk?
This is my first time to this site, so go easy on me.
I'm writing a Korn Script that clears out the NO_HW entries from an ioscan. The script takes the output of the ioscan and places into a text file. The text file is reduced to only the lines with "NO_HW" on them.
Example:
Code:
ctl 54 0/3/0/0/0/0.0.21.11.0 sctl NO_HW DEVICE HP 260 SAS AJ940A
ctl 58 0/3/0/0/0/0.0.27.11.0 sctl NO_HW DEVICE HP 270 SAS AJ941A
ctl 59 0/3/0/0/0/0.0.28.9.0 sctl NO_HW DEVICE HP 270 SAS AJ941A
ctl 60 0/3/0/0/0/0.0.29.11.0 sctl NO_HW DEVICE HP 270 SAS AJ941A
ctl 61 0/3/0/0/0/0.0.30.12.0 sctl NO_HW DEVICE HP 270 SAS AJ941A
ctl 62 0/3/0/0/0/0.0.31.12.0 sctl NO_HW DEVICE HP 270 SAS AJ941A
ctl 63 0/3/0/0/0/0.0.32.12.0 sctl NO_HW DEVICE HP 270 SAS AJ941A
I am trying to strip out EVERYTHING except the H/W path.
This is assuming the fields are separated by tabs. If they're spaces you'll have to add '-d " "' and change the -f number to match the column you want.
Last edited by David the H.; 07-10-2009 at 10:35 AM.
*habit*, *not readable*, *makes me sick* - strange. I'd think, that efficiency in scripting should be a part of one's judgement too. Your awk solution is o.k., your sed / cut not, it's a waste of resources. Your *habit* to use redirect makes things more difficult to understand (unnecessary redirects and cat's are like an epidemic in my opinion!).
I only wanted to show, how sed can be used to do the whole work in the second example - if one doesn't understand the line, he should be able to ask. It's a simple basic regular expression, nothing high sophisticated.
I'd think, that efficiency in scripting should be a part of one's judgement too.
The overhead of reading a file from stdin versus opening the file directly is negligible (long.txt is 70007 lines):
Code:
[user@machine:~]:time awk '{ print $3 }' < long.txt >> /dev/null
real 0m0.076s
user 0m0.072s
sys 0m0.004s
[user@machine:~]:time awk '{ print $3 }' long.txt >> /dev/null
real 0m0.077s
user 0m0.060s
sys 0m0.016s
It actually took longer for the version that specified the file name, but I would have to run each command at least 100 times and average the results for us to do a fair comparison.
Quote:
Originally Posted by jan61
Your awk solution is o.k., your sed / cut not, it's a waste of resources.
Let us see how long the sed-plus-cut solution takes
Code:
[user@machine:~]:time sed -e 's/ */,/g' < long.txt | cut -d ',' -f 3 >> /dev/null
real 0m1.329s
user 0m1.304s
sys 0m0.032s
Much less efficient. So why would I suggest it? If the comma-separated data is saved, then cut can be reused. Let us see how well that works
Code:
[user@machine:~]:time cut -d ',' -f 3 < longc.txt >> /dev/null
real 0m0.042s
user 0m0.040s
sys 0m0.000s
So cut is much more efficient than awk when given well formatted input.
Quote:
Originally Posted by jan61
Your *habit* to use redirect makes things more difficult to understand (unnecessary redirects and cat's are like an epidemic in my opinion!).
Many would disagree with you. It really comes down to a religious debate.
Quote:
Originally Posted by jan61
I only wanted to show, how sed can be used to do the whole work in the second example - if one doesn't understand the line, he should be able to ask. It's a simple basic regular expression, nothing high sophisticated.
The problem with those regular expressions you posted is that they are hard to decipher. Someone who writes a script using them may come back in a week and forget what they do. Sometimes script writers like to sacrifice efficiency for clarity.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.