Check file from multiple files is empty using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check file from multiple files is empty using awk
# 1  
Old 07-30-2015
Check file from multiple files is empty using awk

I am passing multiple files in awk & since one of the file is empty(say file3) so the same gets skipped & logic goes for toss. Need suggestion/help in checking and putting additional checks for the same


Code:
awk -F,  'FNR==1 {++filecounter}
filecounter==1 {KRL[$1]=$2;next}
filecounter==2 {GUJ[$1]=$2;next}
filecounter==3 {DEL[$1]=$2;next}
filecounter==4 {UPW[$1]=$2;next}
{
if($1 in KRL)
print "FOUND IN KRL"
else if($1 in GUJ)
print "FOUND IN GUJ"
else if($1 in DEL)
print "FOUND IN DEL"
else if($1 in UPW)
print "FOUND IN UPW"
else
print "Not Found" 
}
' File1 file2 file3 file4 mainfile

# 2  
Old 07-30-2015
Why don't you use the FILENAME variable for identifying the file being read?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 07-30-2015
If the filenames vary from run to run, you could try something like:
Code:
#!/bin/ksh
awk -F, '
FNR == 1 {
	for(filecounter = 1; filecounter <= ARGC; filecounter++)
		if(FILENAME == ARGV[filecounter])
			break
	printf("***Processing file %s, filecounter=%d\n", FILENAME, filecounter)
}
filecounter==1 {KRL[$1]=$2;next}
filecounter==2 {GUJ[$1]=$2;next}
filecounter==3 {DEL[$1]=$2;next}
filecounter==4 {UPW[$1]=$2;next}
{	if($1 in KRL)
		print "FOUND IN KRL"
	else if($1 in GUJ)
		print "FOUND IN GUJ"
	else if($1 in DEL)
		print "FOUND IN DEL"
	else if($1 in UPW)
		print "FOUND IN UPW"
	else	print "Not Found"
}' "$@"

and invoke this script with:
Code:
./scriptname File1 file2 file3 file4 mainfile

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk.

Note, however, that if you want to have parameters passed to your script as trailing arguments as in:
Code:
./scriptname OFS="," File1 file2 file3 file4 mainfile

you'd have to make the filename matching code more complex.

Or, you could leave out the FNR == 1 clause in your script and invoke it with:
Code:
./scriptname filecounter=1 File1 filecounter=2 file2 filecounter=3 file3 filecounter=4 file4 filecounter=5 mainfile

# 4  
Old 07-31-2015
Thanks Don. Trying to run the script using the following files in the code shared

file1, file2, file3 is empty,i.e, it is blank file & file4 has following content
Code:
9868108600|1025107385|1|9868|108600|20120109|9480
1024344800|1023976099|1|9136|006388|20130814|9429
8459103270|1025429705|1|8459|103270|20130924|9480

mainfile has following content
Code:
1024344800|1|919136004959|004959|31639136001714|20150729105905105905|79|0|0|2|60|NO_EXCHG|7|900|0|||0|9136|702|0|20150729120737||79|3163|0|702|0|0|0|0|0|0|0|0|0|0|24|24|702|0|0|9431|0|SSJPR1MS201507291102556714_PR.PRC|11:00:24|404009875008997
1024344801|1|919136004959|004959|31639136001716|20150729110247110247|50|0|0|1|60|NO_EXCHG|7|900|0|||0|9136|702|0|20150729120737||50|3163|0|702|0|0|0|0|0|0|0|0|0|0|24|24|702|0|0|9431|0|SSJPR1MS201507291105486715_PR.PRC|11:03:37|404009875008998

but when i run the script provided by you it only gives me processing file4 & mainfile with no output saying "FOUND IN DEL". Can you please suggest

---------- Post updated at 10:09 AM ---------- Previous update was at 10:06 AM ----------

apologies, it should give "FOUND IN UPW". Any file provided provided as input say from file1 to file4 can be empty/blank, so same must work for other conditions in case file1 or file2 or file3 is empty.
# 5  
Old 07-31-2015
You said your field separator was a comma (-F,), so with your sample input file file4, field 1 is:
Code:
1024344800|1023976099|1|9136|006388|20130814|9429

(not 1024344800), and
Code:
1024344800|1023976099|1|9136|006388|20130814|9429

in file4 is not the same as field 1 in mainfile:
Code:
1024344800|1|919136004959|004959|31639136001714|20150729105905105905|79|0|0|2|60|NO_EXCHG|7|900|0|||0|9136|702|0|20150729120737||79|3163|0|702|0|0|0|0|0|0|0|0|0|0|24|24|702|0|0|9431|0|SSJPR1MS201507291102556714_PR.PRC|11:00:24|404009875008997

If your field separator is the vertical bar (or pipe symbol), you need to change -F, in your script to -F'|'.
# 6  
Old 07-31-2015
Yup, changed in the code but didnt updated in the code shared by you.

Code:
#!/bin/ksh
awk -F'|' '
FNR == 1 {
	for(filecounter = 1; filecounter <= ARGC; filecounter++)
		if(FILENAME == ARGV[filecounter])
			break
	printf("***Processing file %s, filecounter=%d\n", FILENAME, filecounter)
}
filecounter==1 {KRL[$1]=$2;next}
filecounter==2 {GUJ[$1]=$2;next}
filecounter==3 {DEL[$1]=$2;next}
filecounter==4 {UPW[$1]=$2;next}
{	if($1 in KRL)
		print "FOUND IN KRL"
	else if($1 in GUJ)
		print "FOUND IN GUJ"
	else if($1 in DEL)
		print "FOUND IN DEL"
	else if($1 in UPW)
		print "FOUND IN UPW"
	else	print "Not Found"
}' "$@"

---------- Post updated at 12:30 PM ---------- Previous update was at 11:47 AM ----------

Hi Don, also if you see that file4 content, i am putting field1 in array whose value is field2 from file4
Code:
filecounter==4{UPW[$1]=$2;next}

& the same is being matched from mainfile. I have also highlighted the same in my earlier post. Can you please suggest how to handle this.
# 7  
Old 07-31-2015
Taken from stackoverflow.com/questions/24535753

Code:
awk 'END{print(NR>2)?"NOT EMPTY":"EMPTY"}'

You must put this part in END block otherwise it'll be executed for every line.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can I check, if on remote server directory is empty or have files?

I have a script, which is supposed to run 1 day of the month, connect to remote server certain directory, find files, tar the, and copy find . -ctime -1 | tar -cvf transfer_dmz_start_monthly.tar *${Today}*.*; if then echo "Cannot create a tar file, the terminated... (2 Replies)
Discussion started by: digioleg54
2 Replies

2. UNIX for Beginners Questions & Answers

Creating multiple empty files with touch

Hello, sorry to bother anyone reading this I have an assignment with a question that reads: Your current directory is stenton. Create empty files called f1, f2, and f12 (in that order), within stenton So my first thought was to enter: touch f1 f2 f12 but that does not work, does anyone... (1 Reply)
Discussion started by: eleuin
1 Replies

3. UNIX for Beginners Questions & Answers

Split file into multiple files based on empty lines

I am using below code to split files based on blank lines but it does not work. awk 'BEGIN{i=0}{RS="";}{x="F"++i;}{print > x;}' Your help would be highly appreciated find attachment of sample.txt file (2 Replies)
Discussion started by: imranrasheedamu
2 Replies

4. Shell Programming and Scripting

Empty file check

Hi gurus , I have two files and i want to perform different action based on the condition if both or either is empty If then Do something elif then do something elif then do something else do something fi I have tried the below bt its not... (4 Replies)
Discussion started by: r_t_1601
4 Replies

5. Shell Programming and Scripting

File check script fails for multiple files

I want to check if any file with testing*.txt exists but my script fails if more than 1 file exists. It works fine for a single file if then echo "TEST21" fi -------------- bash: How do I fix this? Thanks Please use code tags next time for your code and data. (8 Replies)
Discussion started by: sumang24
8 Replies

6. Shell Programming and Scripting

Empty out multiple files with a single command?

I have a log directory: /logs/foo.log /logs/bar.log /logs/err.out I'm trying to find a way to > /logs/*.log > /logs/*.out to blank them out, but of course, that doesn't work. Any suggestions? (4 Replies)
Discussion started by: Validatorian
4 Replies

7. Shell Programming and Scripting

Empty Multiple files contents

I would like to empty multiple files contents (without delete the file) which have similar name to begin with. e.g. log1.txt log2.txt log3.txt log4.txt I know cat /dev/null will do the job but that only apply to a single file. But when i tried cat /dev/null > {} \; that doesnt do... (7 Replies)
Discussion started by: kin
7 Replies

8. Shell Programming and Scripting

Check if a text file is empty or not (using ls -s)

Hello, I want to make a script which says if a text file is empty or not. I tried two ways of making it, but I have problems with both of them. Now I think that the better way is the ls -s solution (considering that an empty text file has a 0 weight, because "cat file.txt" fails when file is... (4 Replies)
Discussion started by: Link_
4 Replies

9. Shell Programming and Scripting

check if file is empty

How do I check if a file is empty in a sh script I want to test in my shell script if the output file is empty and if it is do one thing and if it isnt empty do another? any ideas? (8 Replies)
Discussion started by: stolz
8 Replies

10. UNIX for Dummies Questions & Answers

How to check if a file is empty?

Hi Masters..... I have problem !!! I need to check number of records in a file and if it is zero or file is empty i need to do some task. if ; then echo "File s empty" else echo "Not empty" fi so how to check this condition. I used wc -l < filename.txt => 1 for zero records same result... (1 Reply)
Discussion started by: shreekrishnagd
1 Replies
Login or Register to Ask a Question