|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hello!
I try to optimize some shell code and come across the following two questionmarks: 1) Is there any way of writing the following two commands in one single line? grep '{PATTERN}' $IN_FILE | sed '{ARG}' > $OUT_FILE_1 grep -v '{PATTERN}' $IN_FILE > $OUT_FILE_2 Without the -v option on the 2nd line I guess I could have written: grep '{PATTERN}' $IN_FILE > $OUT_FILE_2 | sed '{ARG}' > $OUT_FILE_1 ... which leads me to the question if I can actually output after each step of a filter cascade like on this line? 2) I would like to use the grep command on several conditions including boolean logic operators. The following line uses filter cascade but uses 4 grep processes at run time: grep -v '{PATTERN_1}' $FILE | grep -v '{PATTERN_2}' | grep -v '{PATTERN_3}' | grep '{PATTERN_4}' (note the last grep hasn't the -v option) Is there any possibility of using only one grep command combinating the -v and eventually the -e option? Thanks for your help! ![]() |
|
#2
|
|||
|
|||
|
I do not believe you can do part 1 in just the one command line, not with grep anyway. What about using awk to do it - simple if/else to direct output to one file or another, based on if the search value is found?
The 'cascade' you mention, depending on what you want, may be achieved with the tee command. The second part you can, depending on implementation shrink down into fewer commands: grep -v -e '{PATTERN_1}' -e '{PATTERN_2}' -e '{PATTERN_3}' | grep '{PATTERN_4} |
|
#3
|
|||
|
|||
|
Thank you for your reply, Simon!
Good idea about awk, however I don't see how the sed filter could fit into this solution. The command looks like this: sed -e 's/;//g' -e 's/(\([0-9]*\))//g' (removal of the ";" character and of all brackets and their eventual numeric contents). I don't know if awk can make it as quickly. Each of those filters are not that tough to implement within an awk command, however their combination can be pretty tough compared to the sed solution. |
|
#4
|
||||
|
||||
|
Use gsub in the awk script to do the substitution - for just the lines that match your criteria.
__________________
According to Sod's Law, buttered toast lands butter side down, when dropped. Per nature, cats always land on their feet. So, what happens when you strap buttered toast to the back of a cat and throw it out a window?. |
|
#5
|
|||
|
|||
|
Quote:
Try this: : awk ' { gsub(/;|\([0-9]*\)/,"") } ' <<! XXXX;;; adsf (123) 123 asdfasdf (034556) ;; AAAAAAAAA ! This awk script was tested on AIX. It is OK, but it does not mean that it is faster than sed solution. Generally, awk is slower than sed. Regards ![]() |
|
#6
|
|||
|
|||
|
Thank you both for your help
But I had already written the following awk line:awk '{if (match($0,{PATTERN})) {sub(/[(]*[0-9]*[)]*;/,"",$0); print substr($0,9) > "'$OUT_FILE1'"} else {print $0 > "'$OUT_FILE_2'"} }' $IN_FILE ...from the two grep commands below: grep '{PATTERN}' $IN_FILE | sed -e 's/;//g' -e 's/(\([0-9]*\))//g' > $OUT_FILE_1 grep -v '{PATTERN}' $IN_FILE > $OUT_FILE_2 I still think the two grep/sed commands were a bit more readable! ![]() Quote:
|
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Can I optimize these commands? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|