Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Another challenge: Having awk determine file type? 1

Status
Not open for further replies.

Czar24

Technical User
Mar 22, 2004
16
US
Is this possible?

I want to have my awk script check to make sure the input file is a text file? I've been having a heck of a time trying to do this. I've been trying variations on the system command such as:

(system("file " FILENAME)==*"text"*)

to no avail. Any ideas?


Thanks in advance!


Scott
 
A simple pipe in AWK
Code:
gawk 'BEGIN { "ls" | getline ; close("ls"); print $0 }'

Basically, construct a command in a string variable, like
Code:
cmd = "file " FILENAME
cmd | getline
close( cmd )
print $0

--
 
The second part works like a champ. Thanks Salem!



 
Hope the following helps...
===================================
awk '
FS=","
function trim(str)
{
nsub1=sub("^[ ]*", "", str);
nsub2=sub("[ ]*$", "", str);
return str;
}

{
cmd="file " $1;
while ( cmd | getline output ) {
split(trim(output),myArr,":");

}

print myArr[2]
if ( trim(myArr[2]) ~ /'"c program text"|"shell"'/ ) {
print "I am a c program or a shell script";
}
close(cmd);

} ' /tmp/input_file_with_list_of_files.txt

===================================
 
The same one, without errors (i hope)
[tt]
awk '
function trim(str)
{
nsub1=sub("^[[:space:]]*", "", str);
nsub2=sub("[[:space:]]*$", "", str);
return str;
}

{
cmd="file " $1;
while ( cmd | getline output ) {
split(trim(output),myArr,":");
}

print myArr[2]
if ( trim(myArr[2]) ~ /c program text|shell/ ) {
print "I am a c program or a shell script";
}
close(cmd);

} ' /tmp/input_file_with_list_of_files.txt
[/tt]


Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top