I want to check columns which don't have certain values and redirect them to another file. Why is my code not working??
my code
awk -F '|' 'BEGIN{OFS=FS} {
if ( ($2 ~ /FOP/ || $2 !~ /FOP/) && $4 !~/ABC/ && $5 !~ /^YX.*$/ && $6 !~ /Clone/) print}' file
file:
|FOP|12345|ABC|YX0937|Clone|
AED|FOP|12345|ABC|aacc|Clone|
AUS|FOP|3446|XXW|tred|Clone|
|POP|3452|ABC|aacc|Clone|
|TOP|1234|KBJ|YX|Client|
expected output:
AED|FOP|12345|ABC|aacc|Clone|
AUS|FOP|3446|XXW|tred|Clone|
|POP|3452|ABC|aacc|Clone|
Basically I want to exclude those records which has specific pattern like :-
$2=FOP
$4=ABC
$5=starts with YX
$6=Clone
CodePudding user response:
These requirements:
exclude those records which has specific pattern like:
$2=FOP
$4=ABC
$5=starts with YX
$6=Clone
translate into this awk:
$ awk 'BEGIN {
OFS=FS="|"
}
!($2=="FOP" || $4=="ABC" || $5~/^XY/ || $6=="Clone")' file
Explained:
!( ... || ... )
exclude record if any rule matches$2=="FOP"
second field equalsFOP
$5~/^XY/
fifth field starts withXY
Now you just got to feed it proper data for testing. Your sample data won't produce a single line since every record has Clone
in $6
.
CodePudding user response:
It's hard to understand what you really want to do here, but it seems that you need to work by line.
If so, read your file by line to an array :
mapfile -t array_of_your_file < "file"
Then work with lines :
for line in "${array_of_your_file[@]}" ; do
if [[ line == *string_to_match* ]]; then
# do something with this line
fi
done