0

I need to search for thousands of strings one by one if they exists on a second column and if not do action against each such string which was not found in second column of any line

file:

line a
line b
line c
line z
line d
line e
line z
line z

file2:

line a
line b
line c
line d
line e

What I tried:

$ awk '{if($2=="z") {c++} } END { if(c==0) {print "no \"z\""} else { print c" \"z\"" }}' file
3 "z"
$ awk '{if($2=="z") {c++} } END { if(c==0) {print "no \"z\""} else { print c" \"z\"" }}' file2
no "z"
$ awk '{if($2=="z") {c++} } END { if(c!=0) {print "no \"z, action will be done\""} }' file
no "z, action will be done"

Is my code ok, or could be simplified/optimized yet?

Update: @RomanPerekhrest referring to your code, how to process only "else" or add negation to "c?" and then ignore else?

I was trying with: (c? : "no"),

but it does not work.

@αғsнιη, thank you, finally I have working script

$ awk -v s="z" '$2==s{ c++ }END{ printf (!c ? NOP : "prepare command1\nprepare command2\nprepare command3\n") }' file
prepare command1
prepare command2
prepare command3
$ awk -v s="z" '$2==s{ c++ }END{ printf (!c ? NOP : "prepare command1\nprepare command2\nprepare command3\n") }' file2
$

or

$ awk -v s="z" '$2==s{ c++ }END{if(c==0) printf "prepare command1\nprepare command2\nprepare command3\n" }' file2
prepare command1
prepare command2
prepare command3
$ awk -v s="z" '$2==s{ c++ }END{if(c==0) printf "prepare command1\nprepare command2\nprepare command3\n" }' file
$

closed as unclear what you're asking by glenn jackman, DarkHeart, αғsнιη, G-Man, tripleee Mar 16 '18 at 11:10

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Use !c? or change to c?"no":c or !c?NOP:"no" – αғsнιη Mar 15 '18 at 19:42
1

Via awk dynamic variable/argument -v <var>=<value>:

Test case for string z:

awk -v s="z" '$2==s{ c++ }END{ printf "%s \042%s\042\n", (c? c : "no"), s }' file

The output:

3 "z"

Test case for string w:

awk -v s="w" '$2==s{ c++ }END{ printf "%s \042%s\042\n", (c? c : "no"), s }' file

The output:

no "w"
  • Why not exit immediately after the the first time incrementing c? The OP says he has thousands of strings s to be checked so depending upon each file's size, significant savings could be had by replacing {c++} with {c=1; exit}, no? – user1404316 Mar 15 '18 at 19:56
0

If I understood this correctly you want to check if second column has specific alphabet, if yes, so please try below awk script which will report which one is exist with corresponding count and which one doesn't with "NOT FOUND!" remark.

awk -v alphabet="$(printf "%s" {a..z})" 'BEGIN{ split(alphabet, arr, "") }
    { chrs[$2]++ } END{ for(y in chrs) for(x in arr)
        { if(arr[x] in chrs) {print y, chrs[y]; delete arr[x]; break }
            else{ print arr[x]" NOT FOUND!"; delete arr[x] }
        }
}' infile

Output:

w 1
z 3
a 1
b 1
c 1
f NOT FOUND!
g NOT FOUND!
h NOT FOUND!
i NOT FOUND!
j NOT FOUND!
k NOT FOUND!
l NOT FOUND!
m NOT FOUND!
n NOT FOUND!
o NOT FOUND!
p NOT FOUND!
q NOT FOUND!
r NOT FOUND!
s NOT FOUND!
t NOT FOUND!
u NOT FOUND!
v NOT FOUND!
d 1
x NOT FOUND!
y NOT FOUND!
e 1

Not the answer you're looking for? Browse other questions tagged or ask your own question.