I want to use awk and the system()
function to move a couple of directories around.
I have a file that I want to process with awk
names file.cfg
which is organized in the following way:
/path1 /path2
/some_path /some_other_path
and so on..
each first path is separated from the second path by a whitespace So here's how I did it:
awk '{system(mv -R $1" "$2)}' file.cfg
but it doesn't work and I get
sh: 0/home/my_user/path1: No such file or directory
But file.cfg
looks like this:
/home/my_user/path1 /home/my_user/path2
and there is no 0 before /home
. So what am I missing here?
You just can't call shell functions from awk. You have to write an equivalent awk function, e.g. add this to your awk script: function dots(n) {while(n-- > 0) print "."}
AWK (awk) is a domain-specific language designed for text processing and typically used as a data extraction and reporting tool. Like sed and grep, it is a filter, and is a standard feature of most Unix-like operating systems.
The awk language has a special built-in command called getline that can be used to read input under your explicit control. The getline command is used in several different ways and should not be used by beginners.
The “NF” AWK variable is used to print the number of fields in all the lines of any provided file. This built-in variable iterates through all the lines of the file one by one and prints the number of fields separately for each line.
You have to quote the command you give to system
:
awk '{system("mv -R " $1 " " $2)}' file.cfg
Currently mv -R
is interpreted as the value of variable mv minus the value of R, which is 0
since neither is defined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With