Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
END {
file = "a.txt"
system(cat file)
}

I wish to do something like that? (open a file whose name is in an awk variable). How is it done?

share|improve this question
1  
Why are you cat-ing a file from awk? Sounds like you are trying to do something uber-complex... –  sparticvs Nov 12 '12 at 13:14
    
My awk script processes a file and writes everything to an output file. In the end, I wish to ask the user if he wants to see the output file. If he wishes to, I cat the file. –  learner Nov 12 '12 at 15:34
    
Sorry I've made a small edit in the question BEGIN -> END –  learner Nov 12 '12 at 15:35

3 Answers 3

up vote 4 down vote accepted

The problem is that system() passes the command line to a shell, so in the general case, you need to escape all shell special characters in the name of the file.

awk has a ENVIRON associative array that is mapped to the environment it received, but unfortunately, assigning to it doesn't affect the environment of the commands executed via system or getline

awk -v q="'" '
   function escape(str) {
     gsub(q, q "\\" q q, str)
     return q str q
   }
   BEGIN {
     file = "a.txt"
     system("cat " escape(file))
   }'

Of course, if the file is "a.txt" or you can make sure that its path will never contain any shell special characters, you can get away with:

system("cat " file)

If you can make sure it doesn't contain single quote characters, you could do:

system("cat '\''" file "'\''")
share|improve this answer
    
Um, what is meant by special Shell characters? And what exactly does escape function do? –  learner Nov 13 '12 at 10:32
    
characters special to the shell, part of its syntax like ;, &, >, space, quotes... The escape function, as its name suggest, escapes those, by wrapping everything between single quotes and then escaping every single quote in there. –  Stéphane Chazelas Nov 13 '12 at 10:42

getline is your friend:

awk 'BEGIN{file="a.txt";while ((getline<file) > 0) {print}}'
share|improve this answer

If what you intend to do is to define the file that should be processed in the awk script itself, you could alter the ARGV array, and the ARGC variable accorgingly :

awk 'BEGIN{ ARGV[ARGC] = "your_file.txt" ; ARGC++} {print}'
share|improve this answer
    
No, that isn't what I wanted. The file should simply be printed not, processed. –  learner Nov 13 '12 at 10:36
    
I'm quite new to the place, shall I delete my answer, or leave it in case someone arrives on this question willing to do what I suggested ? –  Vincent Nivoliers Nov 13 '12 at 10:52
    
I think it's useful to have this answer, even though it doesn't fit the original questioner's needs. It may fit someone else's needs who comes to this question later. Note that the ARGV/ARGC mangling won't have any effect on which files get processed if you do it in the END block, as in the original question. It has to be in the BEGIN block, as in this answer, or in the main processing loop. –  dubiousjim Nov 13 '12 at 12:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.