Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk system call

Tags:

bash

awk

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?

like image 406
rares.urdea Avatar asked Apr 04 '13 23:04

rares.urdea


People also ask

How do you call a function in awk?

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 "."}

What is awk in operating system?

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.

What is Getline in awk?

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.

What does awk NF do?

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.


1 Answers

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.

like image 193
Joni Avatar answered Sep 19 '22 16:09

Joni