Hi Gurus,
I am SQL developer and new unix user.
I need to create some file and file content based on information in two files.
I have one file contains basic information below file1 and another exception file file2. the rule is if "zone' and "cd" in file1 exists in file2, then file name is FILENAME in file2, file content is CONTENT in file2. if "zone" and "cd" doesn't exist in file2, then file name is ZONE_CD.file, file content is DT_dat. below are sample files and expected result.
I want to use while loop read file1, then somehow check file2 the zone and Cd, but I don't know how to use two items check. I want to use "grep", but I need to compare two items.
Why would you want to use a shell while loop and grep (multiple times) when the REs needed to isolate fields in a file are much more complicated that using awk to perform field splitting and record joining operations?
Is this a homework assignment? Homework and coursework questions can only be posted in the Homework & Coursework forum under special homework rules. If this is not homework, please explain the company you work for and the nature of the problem you are working on.
the requirement changed a little, we will read from file1, and use field1 and field2 find record in file2 and get filename and file content from file2.
I tried to write with while loop, it works. just wondering if the code can be improved.
I saw all answers use awk.
I know a little bit of awk. I will check the doc to fully understand the syntax, otherwise I will have problem if I just copy and paste.
#!/bin/ksh
IFS=","
error_cnt=0
while read zone feed date
do
echo $zone, $feed, $date
exist_flag=0
while read zone1 feed1 fname fct
do
if [ $zone == $zone1 ] && [ $feed == $feed1 ]; then
echo "export ${fct}=${date}" > ${fname}
exist_flag=1
break
fi
done<metapull.txt
if [ exist_flag -eq 0 ]; then
echo $zone, $feed " doesn't exisit in meta file" >> miss_records.txt
error_cnt=1
fi
done<sample.txt
if [ error_cnt -ne 0 ]; then
echo "Some feeds missing detail information, please check file " miss_records.txt
exit 1
fi
both input files being read via what seems to be the same file descriptor - might work by accident but is sloppy programming; use different fd explicitly
if it would work, for every line in file1 the entire file2 is being read - highly inefficient
every time fname is found in the input files, its hitherto existing contents will be overwritten - mayhap undesired
Weren't it time to become familiar? With three to four line files you won't notice a difference between messing around with shell scripts and tools designed for text processing ( awk is not the only example, look at perl , or sed , or other). Have a look at post1 and post8: 45 min to process a huge file in shell compared to less than 1 min using awk ! And that's not the only thread recently citing tremendous performance improvements.
I modified the code you provided, it works fine
I tried to add one more logic, if zone and cd doesn't exist in file2, create a error file. I got error. would you please take a look. thanks.
my file is
FILE1
AAA FIRST 20170203
BBB SECOND 20170204
CCC THIRD 20170205
FILE2
AAA FIRST file1.txt content1=
BBB SECOND file2.txt content2=
In awk (and nawk ) abc is a variable name and since you haven't assigned anything to that variables, its contents is an empty string (if a string is expected) or zero (if a number is expected). Assuming that you want the error log file to be named abc , try changing:
print "error" > abc
to:
print "error" > "abc"
And, to cover the exit status issue:
nawk '
{ IX = $1 "," $2
}
NR == FNR {
FN[IX] = $3
CT[IX] = $4
next
}
{ if(IX in FN)
print CT[IX]$3 > FN[IX]
else { print "error" > "abc"
ev = 1
}
}
END { exit ev
}' FILE2 FILE1
Or, if you want to exit immediately if an error is found, change the:
You fell into the same trap as you did in post#9. In programming, you need to differentiate between strings, and variables and operators. And, you need to know how e.g. awk deals with variables' types, converting strings into numerics and vice versa if need be. Alphabetic only strings result in a numeric value of 0.
When you're writing tdir/FN[IX] in line 10 (as indicated in the error message), awk tries to divide tdir (a string, i.e. 0 which doesn't hurt) by FN[IX] (another string, i.e. 0, but hurts in this case).
Try, as you've beed told before, to enclose the / in double quotes: "/" .
nawk: syntax error at source line 10
context is
print CT[IDX]$3 > >>> tdir"/" <<<
nawk: illegal statement at source line 10
nawk: syntax error at source line 11