Visit Our UNIX and Linux User Community

Using shell commands in awk

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using shell commands in awk
# 1  
Old 02-14-2011
Using shell commands in awk


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 I'm struggling to get working is below:
echo rpm|awk '{"rpm -qc " $1 "|grep etc|getline configfile;print configfile}'

The error I get is:
awk: {"rpm -qc " $1 "|grep etc|getline configfile;print configfile}
awk:                ^ unterminated string
-bash: echo: write error: Broken pipe

I'm using this noddy example just so I can understand how it works before I look at using it properly.

Any guidance would be much appreciated.
# 2  
Old 02-14-2011
echo rpm|awk '{system "rpm -qc " $1 "|grep etc|getline configfile";print configfile}'

What is getline for? awk or shell ?
# 3  
Old 02-14-2011
I've never used the system function before. I've always run the command and used getline to populate a variable with the information, such as:

awk ' BEGIN {"who|wc -l"|getline users;print "Users logged on: " users}'
Users logged on: 2

# 4  
Old 02-14-2011
awk -v name=rpm 'BEGIN{while("rpm -qc " name "|grep etc"|getline configfile) print configfile}'

awk -v name=rpm 'BEGIN{while("rpm -qc " name |getline) if(/etc/)print}'

Of course, this is elaborate, you do not need awk for this as it can be done straight in the shell.
rpm -qc rpm | grep etc

Last edited by Scrutinizer; 02-14-2011 at 09:25 AM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #676
Difficulty: Medium
The first version of the 802.11 protocol was released in 1997, and provided up to 12 MB/s link speeds.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

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 awk '{ while (i<NR) { i++; percentage = $5; path = $6; ... (2 Replies)
Discussion started by: rishi90
2 Replies

2. 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

3. 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

4. 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 and I have tried executing it using the command sh It is giving an error which reads like: awk:syntax error near line 7... (3 Replies)
Discussion started by: misb
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

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

Featured Tech Videos