Sponsored Content
Top Forums Shell Programming and Scripting Check file from multiple files is empty using awk Post 302950924 by Don Cragun on Thursday 30th of July 2015 05:44:20 AM
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

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
getusershell(3C)					   Standard C Library Functions 					  getusershell(3C)

NAME
getusershell, setusershell, endusershell - get legal user shells SYNOPSIS
#include <unistd.h> char *getusershell(void); void setusershell(void); void endusershell(void); DESCRIPTION
The getusershell() function returns a pointer to a legal user shell as defined by the system manager in the file /etc/shells. If /etc/shells does not exist, the following locations of the standard system shells are used in its place: /bin/bash /bin/csh /bin/jsh /bin/ksh /bin/ksh93 /bin/pfcsh /bin/pfksh /bin/pfsh /bin/sh /bin/tcsh /bin/zsh /sbin/jsh /sbin/pfsh /sbin/sh /usr/bin/bash /usr/bin/csh /usr/bin/jsh /usr/bin/ksh /usr/bin/ksh93 /usr/bin/pfcsh /usr/bin/pfksh /usr/bin/pfsh /usr/bin/sh /usr/bin/tcsh /usr/bin/zsh /usr/sfw/bin/zsh /usr/xpg4/bin/sh The getusershell() function opens the file /etc/shells, if it exists, and returns the next entry in the list of shells. The setusershell() function rewinds the file or the list. The endusershell() function closes the file, frees any memory used by getusershell() and setusershell(), and rewinds the file /etc/shells. RETURN VALUES
The getusershell() function returns a null pointer on EOF. BUGS
All information is contained in memory that may be freed with a call to endusershell(), so it must be copied if it is to be saved. NOTES
Restricted shells should not be listed in /etc/shells. SunOS 5.11 1 Nov 2007 getusershell(3C)
All times are GMT -4. The time now is 05:31 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy