How to use shell commands within awk?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to use shell commands within awk?
# 1  
Old 11-29-2015
How to use shell commands within awk?

Hey guys i want to use shell commands like ls, find, cd and more with in awk statements with inputs from the awk variables.
Like in the below code how can i change the directory using the value of path. Please suggest

Code:
awk '{ while (i<NR) 
	{
	 i++;
	 percentage[i] = $5;
	 path[i] = $6;
	 split(percentage[i],array,"%");
	 percentage[i]=array[1];

	 if(percentage[i] > 40 && percentage[i]!="Use")
	 {		
		print "Greater than 40",percentage[i],path[i];
		system("cd" path[i]);
		system("ls -l");
		
	 }
	}
     }' formated.txt

# 2  
Old 11-29-2015
Every single system call creates a subshell in which the respective commands are run by sh. The environment will be lost when the subshell exits. If you want to ls that path, either combine the two commands into one compound or do "ls -l " path [i]. Be aware that this might become lengthy...
This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-29-2015
ohhh... i got it now.. Thanks Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help me in awk commands

can you please let me know what does the below awk command does. awk 'NR>2{printf "%-30s %-15s\n", $1, !($4)?$2:$4;}' result.txt (1 Reply)
Discussion started by: ramkumar15
1 Replies

2. Shell Programming and Scripting

Any shell or hack that makes the shell command line take vi commands?

basically i'm tired of hitting the left arrow a few dozen times when correcting a mistake or modifying a history command i'd like to use vim style key shortcuts while on the command line so that a 55 moves the cursor 55 places to the left... and i want all the other vi goodies, search of... (3 Replies)
Discussion started by: marqul
3 Replies

3. Shell Programming and Scripting

Executing a shell script containing awk commands

Hi All, I am trying to execute a shell script containg awk commands. But unable to do so. Below is my script. Please help. The name of the script is scan.sh and I have tried executing it using the command sh scan.sh It is giving an error which reads like: awk:syntax error near line 7... (3 Replies)
Discussion started by: misb
3 Replies

4. Shell Programming and Scripting

Using shell commands in awk

Hi, I've searched this site and the wider web and have not found anything (that I can understand..) that helps me. I've used shell commands in awk fine in the past, the difference is that I want to pass the shell command a field variable within awk from the current input. A simple example... (3 Replies)
Discussion started by: Ronnie717
3 Replies

5. Shell Programming and Scripting

Can BASH execute commands on a remote server when the commands are embedded in shell

I want to log into a remote server transfer over a new config and then backup the existing config, replace with the new config. I am not sure if I can do this with BASH scripting. I have set up password less login by adding my public key to authorized_keys file, it works. I am a little... (1 Reply)
Discussion started by: bash_in_my_head
1 Replies

6. UNIX for Dummies Questions & Answers

what are some different commands in c shell and korn shell??

I am doing this simple script using c shell and korn shell. The commands I use are fgrep , ls, and also some redirecting. Is there any difference in using both of these commands in c shell and korn shell? Thanks and sorry for the stupid question. (1 Reply)
Discussion started by: EquinoX
1 Replies

7. Shell Programming and Scripting

to join the corresponding lines using shell commands or awk

Suppose u have this file gi_1 ABCDEFDHIJ KMNOPQRSTU VWXYZABCDE gi_2 JKLMNOPQRS TUVWXYZABC DEFGHIJKLM gi_3 PQRSTUVWXY ZABCDEFGHI JKLMNOPQRS gi_4 CDEFGHIJKL MNOPQRSTUV WXYZABCDEF gi_5 IJKLMNOPQR STUVWXYZAB CDEFGHIJKLM FGHIJKLMNO PQRSTUVWXY ZABCDEFABC NOPQRSTUVW XYZABCDEFG... (7 Replies)
Discussion started by: cdfd123
7 Replies

8. Shell Programming and Scripting

How to run unix commands in a new shell inside a shell script?

Hi , I am having one situation in which I need to run some simple unix commands after doing "chroot" command in a shell script. Which in turn creates a new shell. So scenario is that - I need to have one shell script which is ran as a part of crontab - in this shell script I need to do a... (2 Replies)
Discussion started by: hkapil
2 Replies

9. Shell Programming and Scripting

how to execute shell commands in awk script?

Greetings Friends, Suppose I wish to invoke some shell level commands inside an awk script how to do that? say I wish to use : "wc" on a record to count number of characters in a record Any clues? Thanks, Rishi (1 Reply)
Discussion started by: RishiPahuja
1 Replies

10. Shell Programming and Scripting

KORN Shell - Spawn new shell with commands

I want to be able to run a script on one server, that will spawn another shell which runs some commands on another server.. I have seen some code that may help - but I cant get it working as below: spawn /usr/bin/ksh send "telnet x <port_no>\r" expect "Enter command: " send "LOGIN:x:x;... (2 Replies)
Discussion started by: frustrated1
2 Replies
Login or Register to Ask a Question