check if no line contains string in given field [closed]
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
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 command1nprepare command2nprepare command3n") ' file
prepare command1
prepare command2
prepare command3
$ awk -v s="z" '$2==s c++ END printf (!c ? NOP : "prepare command1nprepare command2nprepare command3n") ' file2
$
or
$ awk -v s="z" '$2==s c++ ENDif(c==0) printf "prepare command1nprepare command2nprepare command3n" ' file2
prepare command1
prepare command2
prepare command3
$ awk -v s="z" '$2==s c++ ENDif(c==0) printf "prepare command1nprepare command2nprepare command3n" ' file
$
text-processing awk gawk
closed as unclear what you're asking by glenn jackman, DarkHeart, ñÃÂsýù÷, G-Man, tripleee Mar 16 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.
add a comment |Â
up vote
0
down vote
favorite
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 command1nprepare command2nprepare command3n") ' file
prepare command1
prepare command2
prepare command3
$ awk -v s="z" '$2==s c++ END printf (!c ? NOP : "prepare command1nprepare command2nprepare command3n") ' file2
$
or
$ awk -v s="z" '$2==s c++ ENDif(c==0) printf "prepare command1nprepare command2nprepare command3n" ' file2
prepare command1
prepare command2
prepare command3
$ awk -v s="z" '$2==s c++ ENDif(c==0) printf "prepare command1nprepare command2nprepare command3n" ' file
$
text-processing awk gawk
closed as unclear what you're asking by glenn jackman, DarkHeart, ñÃÂsýù÷, G-Man, tripleee Mar 16 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 toc?"no":c
or!c?NOP:"no"
â Ã±ÃÂsýù÷
Mar 15 at 19:42
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 command1nprepare command2nprepare command3n") ' file
prepare command1
prepare command2
prepare command3
$ awk -v s="z" '$2==s c++ END printf (!c ? NOP : "prepare command1nprepare command2nprepare command3n") ' file2
$
or
$ awk -v s="z" '$2==s c++ ENDif(c==0) printf "prepare command1nprepare command2nprepare command3n" ' file2
prepare command1
prepare command2
prepare command3
$ awk -v s="z" '$2==s c++ ENDif(c==0) printf "prepare command1nprepare command2nprepare command3n" ' file
$
text-processing awk gawk
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 command1nprepare command2nprepare command3n") ' file
prepare command1
prepare command2
prepare command3
$ awk -v s="z" '$2==s c++ END printf (!c ? NOP : "prepare command1nprepare command2nprepare command3n") ' file2
$
or
$ awk -v s="z" '$2==s c++ ENDif(c==0) printf "prepare command1nprepare command2nprepare command3n" ' file2
prepare command1
prepare command2
prepare command3
$ awk -v s="z" '$2==s c++ ENDif(c==0) printf "prepare command1nprepare command2nprepare command3n" ' file
$
text-processing awk gawk
edited Mar 15 at 20:22
asked Mar 15 at 15:15
DonJ
768
768
closed as unclear what you're asking by glenn jackman, DarkHeart, ñÃÂsýù÷, G-Man, tripleee Mar 16 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.
closed as unclear what you're asking by glenn jackman, DarkHeart, ñÃÂsýù÷, G-Man, tripleee Mar 16 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 toc?"no":c
or!c?NOP:"no"
â Ã±ÃÂsýù÷
Mar 15 at 19:42
add a comment |Â
Use!c?
or change toc?"no":c
or!c?NOP:"no"
â Ã±ÃÂsýù÷
Mar 15 at 19:42
Use
!c?
or change to c?"no":c
or !c?NOP:"no"
â Ã±ÃÂsýù÷
Mar 15 at 19:42
Use
!c?
or change to c?"no":c
or !c?NOP:"no"
â Ã±ÃÂsýù÷
Mar 15 at 19:42
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
Via awk
dynamic variable/argument -v <var>=<value>
:
Test case for string z
:
awk -v s="z" '$2==s c++ END printf "%s 42%s42n", (c? c : "no"), s ' file
The output:
3 "z"
Test case for string w
:
awk -v s="w" '$2==s c++ END printf "%s 42%s42n", (c? c : "no"), s ' file
The output:
no "w"
Why not exit immediately after the the first time incrementingc
? The OP says he has thousands of stringss
to be checked so depending upon each file's size, significant savings could be had by replacingc++
withc=1; exit
, no?
â user1404316
Mar 15 at 19:56
add a comment |Â
up vote
0
down vote
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
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Via awk
dynamic variable/argument -v <var>=<value>
:
Test case for string z
:
awk -v s="z" '$2==s c++ END printf "%s 42%s42n", (c? c : "no"), s ' file
The output:
3 "z"
Test case for string w
:
awk -v s="w" '$2==s c++ END printf "%s 42%s42n", (c? c : "no"), s ' file
The output:
no "w"
Why not exit immediately after the the first time incrementingc
? The OP says he has thousands of stringss
to be checked so depending upon each file's size, significant savings could be had by replacingc++
withc=1; exit
, no?
â user1404316
Mar 15 at 19:56
add a comment |Â
up vote
1
down vote
Via awk
dynamic variable/argument -v <var>=<value>
:
Test case for string z
:
awk -v s="z" '$2==s c++ END printf "%s 42%s42n", (c? c : "no"), s ' file
The output:
3 "z"
Test case for string w
:
awk -v s="w" '$2==s c++ END printf "%s 42%s42n", (c? c : "no"), s ' file
The output:
no "w"
Why not exit immediately after the the first time incrementingc
? The OP says he has thousands of stringss
to be checked so depending upon each file's size, significant savings could be had by replacingc++
withc=1; exit
, no?
â user1404316
Mar 15 at 19:56
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Via awk
dynamic variable/argument -v <var>=<value>
:
Test case for string z
:
awk -v s="z" '$2==s c++ END printf "%s 42%s42n", (c? c : "no"), s ' file
The output:
3 "z"
Test case for string w
:
awk -v s="w" '$2==s c++ END printf "%s 42%s42n", (c? c : "no"), s ' file
The output:
no "w"
Via awk
dynamic variable/argument -v <var>=<value>
:
Test case for string z
:
awk -v s="z" '$2==s c++ END printf "%s 42%s42n", (c? c : "no"), s ' file
The output:
3 "z"
Test case for string w
:
awk -v s="w" '$2==s c++ END printf "%s 42%s42n", (c? c : "no"), s ' file
The output:
no "w"
answered Mar 15 at 15:22
RomanPerekhrest
22.4k12144
22.4k12144
Why not exit immediately after the the first time incrementingc
? The OP says he has thousands of stringss
to be checked so depending upon each file's size, significant savings could be had by replacingc++
withc=1; exit
, no?
â user1404316
Mar 15 at 19:56
add a comment |Â
Why not exit immediately after the the first time incrementingc
? The OP says he has thousands of stringss
to be checked so depending upon each file's size, significant savings could be had by replacingc++
withc=1; exit
, no?
â user1404316
Mar 15 at 19:56
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 at 19:56
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 at 19:56
add a comment |Â
up vote
0
down vote
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
add a comment |Â
up vote
0
down vote
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
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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
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
answered Mar 15 at 17:34
ñÃÂsýù÷
14.8k82462
14.8k82462
add a comment |Â
add a comment |Â
Use
!c?
or change toc?"no":c
or!c?NOP:"no"
â Ã±ÃÂsýù÷
Mar 15 at 19:42