running bash command inside awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting running bash command inside awk
# 1  
Old 06-23-2009
running bash command inside awk

Org file

192.168.1.10 d:\adir\xdir
192.168.1.11 d:\bdir\ydir

want to covert it into

robocopy \\192.168.1.10\d$\adir\xdir\log* some_localdir\adir
robocopy \\192.168.1.10\d$\adir\ydir\log* some_localdir\bbdir
# 2  
Old 06-23-2009
what have you tried so far?
And why do you need to invoke 'bash command inside awk'?
# 3  
Old 06-23-2009
able to achieve atleast,

robocopy \\192.168.1.10\d$\adir\xdir\log* some_localdir\
robocopy \\192.168.1.10\d$\adir\ydir\log* some_localdir\

awk '{printf "robocopy \\\\"$1 "\\" $2 "\\ecm\*.log some_localdir\\\n"}' ecm_app_server_list.txt | sed 's/:/$/'


now want
robocopy \\192.168.1.10\d$\adir\xdir\log* some_localdir\xdir
robocopy \\192.168.1.10\d$\adir\ydir\log* some_localdir\ydir

now I want to use following bash command to achieve this ..

awk '{printf "robocopy \\\\"$1 "\\" $2 "\\ecm\*.log some_localdir\\$(cut -d ":" -f3 $2)`\n"}' ecm_app_server_list.txt | sed 's/:/$/'

but not working .. giving error
# 4  
Old 06-23-2009
using command substitution:

Code:
$(some command)

is NOT how you call non-awk commands from awk.

What you want to do can be achieved with a little awk magic....just awk.

split(Image,Image,sep)

so somewhere before your print statement, you need a split() statement, like so:

Code:
split($2, array1, "\\")

so then, lets use the "192.168.1.10 d:\adir\xdir" line as an example:
array1[1] is "d:"
array1[2] is "adir"
array1[3] is "xdir"
# 5  
Old 06-24-2009
nawk -f ydk.awk myFile

ydk.awk:
Code:
function rindex(str,c)
{
  return match(str,"\\" c "[^\\" c "]*$")? RSTART : 0
}
{
  split($2, f2, ":")
  print "robocopy \\\\" $1 "\\" f2[1] "$" f2[2] "\log* somelocaldir\\" substr($2,1, rindex($2,"\\")-1)
}

# 6  
Old 06-24-2009
excellent.. thanks..
split is really good..
any good website for reference having details about awk functions
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

awk trouble inside another command

I tried running this. dsh -w server1 'lsof /audit | awk '{ print $2 }'' It did not like above so I tried to escape the single parenthesis at the end. dsh -w server1 'lsof /audit | awk '{ print $2 }\'' It then hung so I changed up the parenthesis to this. This worked. dsh -w server1... (6 Replies)
Discussion started by: cokedude
6 Replies

2. Shell Programming and Scripting

Executing sed command inside a bash script

I want to run commands inside a bash script. An example is I want to pass the command in a string as regexp as an argument to the script, then run sed on the bash variable sed.sh regexp sed.sh "-i \"s/<p>//g\"" then call sed "$regexp" $fl (3 Replies)
Discussion started by: Kangol
3 Replies

3. Shell Programming and Scripting

Run bash command inside zsh script

Hi, I would like to run following code in bash inside a zsh script. (In this case is output unfortunately very different if you run it in zsh). I tried to put "bash" in front of the code but I obtained following error message "bash: do: No such file or directory " eve though I merged the whole... (7 Replies)
Discussion started by: kamcamonty
7 Replies

4. Shell Programming and Scripting

Multiple command execution inside awk command during xml parsing

below is the output xml string from some other command and i will be parsing it using awk cat /tmp/alerts.xml <Alert id="10102" name="APP-DS-ds_ha-140018-componentFailure-S" alertDefinitionId="13982" resourceId="11427" ctime="1359453507621" fixed="false" reason="If Event/Log Level(ANY) and... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

Bash- Command run from script does not pass full parameters with spaces inside

There's a JavaScript file that I call from command line (there's a framework) like so: ./RunDiag.js param1:'string one here' param2:'string two here' I have a shell script where I invoke the above command. I can run it in a script as simple as this #!/bin/bash stuff="./RunDiag.js... (4 Replies)
Discussion started by: AcerAspirant
4 Replies

6. Shell Programming and Scripting

How i can put the result of a command inside a bash variable?

#!/bin/bash #... for i in `ls -c1 /usr/share/applications` do name="cat $i | grep ^Name= | cut -d = -f2" echo $name #... done Now inside name as output is present: while i want only the result of the command. Ideally i would like obtain that information using only bash ... or... (8 Replies)
Discussion started by: alexscript
8 Replies

7. UNIX for Dummies Questions & Answers

Running set options from the command line and bash command

I'm reading about debugging aids in bash and have come across the set command. It says in my little book that an addition to typing set you can also use them "on the command line when running a script..." and it lists this in a small table: set -o option Command Line... (5 Replies)
Discussion started by: Straitsfan
5 Replies

8. UNIX for Dummies Questions & Answers

using awk inside bash script?

Hello, I'm trying to write a bash script that will query the current system time (OS X 10.6.6) and then convert the output from HH:MM:SS into time in seconds. The output of the system time command (systemsetup -gettime) is returned as: Time: HH:MM:SS so I wanted to use awk -F: to grab... (5 Replies)
Discussion started by: xaiu
5 Replies

9. Shell Programming and Scripting

How to use same variable value inside as well as outside of the awk command?

Hi Jim, The following script is in working state. But i m having one more problem with awk cmd. Could you tell me how to use any variable inside awk or how to take any variable value outside awk. My problem is i want to maintain one property file in which i am declaring variable value into that... (12 Replies)
Discussion started by: Ganesh Khandare
12 Replies

10. Shell Programming and Scripting

Running command inside awk

Hi, I have a awk script to read a CSV file. After reading the values i want to call a executable (nameely call_it) with the values what i read from the scv file. I dont want to use system command inside the awk. Is there any other way to run the executable from the awk script Thanks ... (1 Reply)
Discussion started by: Raghuram.P
1 Replies
Login or Register to Ask a Question