tags:

views:

151

answers:

1

Is there any implementation of scanf()(like in C) in awk(POSIX)?

I know that awk does not have back reference like in sed and perl. What is the easiest way to simulate in awk?

Thanks

Nyan

+2  A: 

sprintf() , printf() may be what you are looking for. awk does not support backreferences however gawk's gensub(), or even gsub() can provide backreferences.

Backreferences are just storing the strings that are matched. So, making use of awk's capability to differentiate fields and field delimiters, using its internal variables like OFS,FS,ORS etc are the way to go.. eg

$ echo "test.txt" | sed 's/\(.*\).txt/\1.pdf/'
test.pdf

$ echo "test.txt" | awk -F"." '{$NF="pdf"}1' OFS="."
test.pdf

Of course, this is just a simple example. But you get the idea.

ghostdog74
ya i know printf family in awk. but scanf??
Nyan
you can use getline to take user input, then use sprintf with the proper format specifier and store to variable
ghostdog74