Ignoring I/O errors in output redirection 
Author Message
 Ignoring I/O errors in output redirection

Hi folks,

I've got a small problem.

I have a log file like:
--------------
msg1    dir1
msg2    dir2
msg3    dir1
msg4    dir1
msg5    dir2
msg6    foo
msg7    dir2
--------------

Now, I want to have all entries with "dir1" at column2 in a file in directory "dir1", all entries with "dir2" in directory
dir2 and so on.

The script looks simple:
    {print >> $NR/log}

The problem are entries for which no directory exists (like "foo"). awk can't create the file and stops with an I/O error.

Is there any possibility to simply ignore those errors (would be no problem for me) ?

Thanks in advance

Daniel Matuschek
Swisscom IP-Plus



Mon, 13 Jan 2003 03:00:00 GMT  
 Ignoring I/O errors in output redirection


<snip>

Quote:
>The problem are entries for which no directory exists (like "foo").
>awk can't create the file and stops with an I/O error.

>Is there any possibility to simply ignore those errors (would be no
>problem for me) ?

Ignore them - no. Trap them - yes, but at that point you'd be better
off doing this as a shell script since it would take a system() call to
trap nonexistent directories.

This holds even more so if there are a great many different directories
in the input file. Unless you close each $NR/log (BTW, did you mean
$NF/log?) after the print statement, you may try to open too many files
in a single process, and the script would die then.

Sent via Deja.com http://www.deja.com/
Before you buy.



Mon, 13 Jan 2003 03:00:00 GMT  
 Ignoring I/O errors in output redirection

Quote:

>Hi folks,

>I've got a small problem.

>I have a log file like:
>--------------
>msg1    dir1
>msg2    dir2
>msg3    dir1
>msg4    dir1
>msg5    dir2
>msg6    foo
>msg7    dir2
>--------------

>Now, I want to have all entries with "dir1" at column2 in a file
>in directory "dir1", all entries with "dir2" in directory
>dir2 and so on.

>The script looks simple:
>    {print >> $NR/log}

>The problem are entries for which no directory exists (like "foo").
>awk can't create the file and stops with an I/O error.

>Is there any possibility to simply ignore those errors (would be
>no problem for me) ?

You could use the system() command to create non-existing
directories.  Something like this does that:

awk '{cmd="mkdir hello";system(cmd);
      print "This is a test too" > "hello/world"}'

If mkdir fails because the directory exists, it creates an error,
but doesn't stop the script, and that error could be redirrected.

I suppose you might also test if the directory existed before
trying to create it, with a more complicated cmd.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Mon, 13 Jan 2003 03:00:00 GMT  
 Ignoring I/O errors in output redirection

   >The problem are entries for which no directory exists (like "foo").
   >awk can't create the file and stops with an I/O error.
   >Is there any possibility to simply ignore those errors (would be no
   >problem for me) ?
I'm not sure if this will work, but you get the idea...
BEGIN {while("find -type d -print" | getline) dirlist[$0]++}
{if($NF in dirlist) print >$NF/log}

Net-Tamer V 1.08X - Test Drive



Tue, 14 Jan 2003 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. os.path.expanduser ignores os.seteuid

2. Problems with output redirection in gawk

3. RFO: output redirection

4. Command output redirection

5. output redirection within mod_ruby

6. Help about OUTPUT redirection

7. program for output redirection

8. Problem: Results depending on output redirection ?!

9. Output redirection

10. spawnv output redirection?

11. output redirection with nt

12. output redirection

 

 
Powered by phpBB® Forum Software