Я пишу программу awk для допустимых 3 файлов. здесь файл 1 будет иметь 5 полей, файл 2 будет иметь 4 поля, файл 3 будет иметь 2 поля
first coulm in first row matches with the condition, move to next record orelse exit on each file
for example if (NR == 1 && $1 == "NAME") {next}
else {exit 1}
for first file from second row validate the 2nd column should not be null
for second file from second row validate the 01st column should not be null, and 03rd column should not be null
for third file from second row validate the 01st column should not be null
файлы SAMPLE
file1: SUN_20200801.csv
NAME|AGE|JOIN|END|QUIT
AAA|23|Y|2021|N
BBB|25|N|2022|N
file2: MOON_20200801.csv
EMPNO|EMPNAME|EMPSAL|EMPDEP
01|XXXX|20000|SE
01|YYYY|30000|NSE
file3: STAR_20200801.csv
DEPNO|DEPNAME
01|XXXX
01|YYYY
CODE I TRIED
BEGIN { FS="|"
}
#MAIN PROGRAM
{
if (NR == 1 && $1 != "NAME") {
if (NR == 1 && $1 != "EMPNO") {
if (NR == 1 && $1 != "DEPNO") { print "HEADER NOT MATCHING"
exit 1}}}
if (NR == 1 && $1 == "NAME")
{ next
if (NR > 1)
{ if ( NF != 5 ) { print "Input record does not contain 5 fields" }
else { if ( NF = 5 ) { if ( $2 == "" ) { print "IS NULL" } }}
}
}
if (NR == 1 && $1 == "EMPNO")
{ next
if (NR > 1)
{ if ( NF != 4 ){ print "Input record does not contain 4 fields" }
else { if ( NF = 4 ) { if ( $1 == "" ) { print "IS NULL" }
if ( $1 == "" ) { print "IS NULL" }
}
}
}
}
if (NR == 1 && $1 == "DEPNO")
{next
if (NR > 1)
{ if ( NF != 2 ) { print "Input record does not contain 2 fields" }
else {if ( NF = 2 ) { if ( $1 == "" ) { print "IS NULL" } } }
}
}
}
Вот что "дальше" не работает. Может ли кто-нибудь из вас помочь мне написать правильный код?
РЕДАКТИРОВАТЬ Эдом Мортоном, чтобы показать приведенный выше код, пропущенный через gawk -o-
, чтобы упростить отслеживание структуры:
BEGIN {
FS = "|"
}
{
if (NR == 1 && $1 != "NAME") {
if (NR == 1 && $1 != "EMPNO") {
if (NR == 1 && $1 != "DEPNO") {
print "HEADER NOT MATCHING"
exit 1
}
}
}
if (NR == 1 && $1 == "NAME") {
next
if (NR > 1) {
if (NF != 5) {
print "Input record does not contain 5 fields"
} else if (NF = 5) {
if ($2 == "") {
print "IS NULL"
}
}
}
}
if (NR == 1 && $1 == "EMPNO") {
next
if (NR > 1) {
if (NF != 4) {
print "Input record does not contain 4 fields"
} else if (NF = 4) {
if ($1 == "") {
print "IS NULL"
}
if ($1 == "") {
print "IS NULL"
}
}
}
}
if (NR == 1 && $1 == "DEPNO") {
next
if (NR > 1) {
if (NF != 2) {
print "Input record does not contain 2 fields"
} else if (NF = 2) {
if ($1 == "") {
print "IS NULL"
}
}
}
}
}
#MAIN PROGRAM