awk script to search output for a value and print


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script to search output for a value and print
# 1  
Old 09-01-2014
awk script to search output for a value and print

Code:
GOODNUMBERS="1 2 3 4 5 6 3 3 34 34 5 66 12"
BADNUMBERS="7 3 12 5 66"

for eachnum in `echo ${GOODNUMBERS}`
do
        echo ${BADNUMBERS} | gawk -v threshold=${eachnum} '$1 != threshold'
done

what im trying to do with the above is, i want to print numbers that are in the GOODNUMBERS variable IF AND ONLY IF they are not in the BADNUMBERS variable.

how can i do this in awk?
# 2  
Old 09-01-2014
Why awk? Try:

Code:
for i in $GOODNUMBERS
do
  for j in $BADNUMBERS
  do
    if [ $i = $j ]; then
      continue 2
    fi
  done
  echo $i
done

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 09-01-2014
Quote:
Originally Posted by Scrutinizer
Why awk? Try:

Code:
for i in $GOODNUMBERS
do
  for j in $BADNUMBERS
  do
    if [ $i = $j ]; then
      continue 2
    fi
  done
  echo $i
done


the list of numbers in the GOODNUMBERS variable have the potential to be very big.. so i was worried using a for loop can be very slow.
# 4  
Old 09-01-2014
How big could that list in the GOODNUMBERS variable become then?
# 5  
Old 09-01-2014
Code:
awk -vg="${GOODNUMBERS}" -vb="${BADNUMBERS}" 'BEGIN{n=split(g, a); for(i=1; i<=n; i++) {if(b !~ "(^| )" a[i] "($| )") print a[i]}}'

---------- Post updated at 04:28 AM ---------- Previous update was at 04:08 AM ----------

I dont know about the variable limitation in awk, but if it cannot hold as much data as shell does, you could use below
Code:
awk -vb="${BADNUMBERS}" '{n=split($0, a); for(i=1; i<=n; i++) {if(b !~ "(^| )" a[i] "($| )") print a[i]}}' <<<${GOODNUMBERS}

This User Gave Thanks to SriniShoo For This Post:
# 6  
Old 09-01-2014
This will reduce the number of elements to be searched by every hit:
Code:
G=($GOODNUMBERS)
for i in $BADNUMBERS
   do for j in ${!G[@]}
      do [ $i -eq 0"${G[j]}" ] && unset G[j]
      done
   done
echo ${G[@]}
1 2 4 6 34 34

This User Gave Thanks to RudiC For This Post:
# 7  
Old 09-01-2014
@Srinishoo:
The variable limitation is not in awk, nor is it a limitation of the shell, but rather an OS limitation determined by the configuration variable ARG_MAX ( getconf ARG_MAX ) .
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

(n)awk: print regex search output lines in one line

Hello. I have been looking high and low for the solution for this. I seems there should be a simple answer, but alas. I have a big xml file, and I need to extract certain information from specific items. The information I need can be found between a specific set of tags. let's call them... (2 Replies)
Discussion started by: Tobias-Reiper
2 Replies

2. Shell Programming and Scripting

awk command for complex search and print

Input: 9999~cbc~100~209~ozzxczcz~10001001001~100~abc10 123~a~sdklfjl~sldoi~~~ksjdfnkjsdf~123456 125~g~sdklfjl~slsdfsdfsdfsdfdoi~~~~~~~~ksjdfnkjsdf~345 127~wera~sdklfjl~sldoi~~~ksjdfnkjsdf~123 128~awer~swerwerdklfjl~sldoi~~~ksjdfnkjsf~1da2345... (7 Replies)
Discussion started by: onesuri
7 Replies

3. Shell Programming and Scripting

Awk script to run a sql and print the output to an output file

Hi All, I have around 900 Select Sql's which I would like to run in an awk script and print the output of those sql's in an txt file. Can you anyone pls let me know how do I do it and execute the awk script? Thanks. (4 Replies)
Discussion started by: adept
4 Replies

4. Shell Programming and Scripting

Help with sed/awk for reverse search and print

I have a file which is DFDG START DSFDS DSDS XXX END (VIO) AADD START SDSD FGFG END and I have to print the lines between START and END (VIO). In the files there are multiple places where START would be followed by END with few lines in between but I need to print only if START is... (18 Replies)
Discussion started by: pgbuddy
18 Replies

5. Shell Programming and Scripting

compare two files and search keyword and print output

You have two files to compare by searching keyword from one file into another file File A 23 >pp_ANSWER 24 >aa hello 25 >jau head wear 66 >jss oops 872 >aqq olps ploww oww sss 722 >GG_KILLER ..... large files File B Beta done KILLER John Mayor calix meyers ... (5 Replies)
Discussion started by: cdfd123
5 Replies

6. Shell Programming and Scripting

awk search and print

I have a script where I need to use awk, go through some output, which is stored in a variable, and find a string Xms and Xmx and print the results, including the rest of that string. Example of string: ... (3 Replies)
Discussion started by: cbo0485
3 Replies

7. Shell Programming and Scripting

awk script to search an html file and output links

hello. i want to make an awk script to search an html file and output all the links (e.g .html, .htm, .jpg, .doc, .pdf, etc..) inside it. also, i want the links that will be output to be split into 3 groups (separated by an empty line), the first group with links to other webpages (.html .htm etc),... (8 Replies)
Discussion started by: kyris
8 Replies

8. Shell Programming and Scripting

To parse through the file and print output using awk or sed script

suppose if u have a file like that Hen ABCCSGSGSGJJJJK 15 Cock ABCCSGGGSGIJJJL 15 * * * * * * : * * * . * * * : Hen CFCDFCSDFCDERTF 30 Cock CHCDFCSDHCDEGFI 30 * . * * * * * * * : * * :* : : . The output shud be where there is : and . It shud... (4 Replies)
Discussion started by: cdfd123
4 Replies

9. Shell Programming and Scripting

Shell script to search through numbers and print the output

Suppose u have a file like 1 30 ABCSAAHSNJQJALBALMKAANKAMLAMALK 4562676268836826826868268468368282972982 2863923792102370179372012792701739729291 31 60... (8 Replies)
Discussion started by: cdfd123
8 Replies

10. Shell Programming and Scripting

using awk to search and print output

suppose i have one file file A 18 24 30 35 38 45 55 Another file file B 08_46 A 16 V -0.36 0.23 E : 1.41 08_46 A 17 D -1.04 0.22 E : 0.84 08_46 A 18 Q -0.49 0.12 E : 0.06 08_46 A 19 G 0.50 0.14 E : 0.05 08_46 A 20 V ... (5 Replies)
Discussion started by: cdfd123
5 Replies
Login or Register to Ask a Question