awk command not getting executed in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command not getting executed in shell script
# 1  
Old 10-23-2015
awk command not getting executed in shell script

I am able to execute awk command from shell prompt. but the same command is not getting executed when written and run in a bash script

the command from bash cmd prompt.
Code:
awk '/world/{for (i=2; i<NF; i++) printf $i " "; print $NF}1' myfile >tmp$$ ; mv tmp$$ myfile

file:
#  hello world my friend
# this is a beautiful place
$ mary had a little lamb
# jhony jhony yes papa
 
OP
hello world my friend
# this is a beautiful place
$ mary had a little lamb
# jhony jhony yes papa

But the same thing on trying with a bash script, no output is observed
script
Code:
#!/bin/bash
#!/usr/bin/awk 
NAME="world"
awk '/$NAME/{for (i=2; i<NF; i++) printf $i " "; print $NF}1' myfile >tmp$$ ; mv tmp$$ myfile

# 2  
Old 10-23-2015
There's two reasons that construct doesn't work - neither within nor without a script:
- within single quotes shell expansion is not performed - $NAME is taken literally.
- awk interprets anything between / as a regex constant - again, $NAME will be taken literally.

Use the -v option to awk to hand over parameters, and use the match function in lieu of the /... /

On top, only the shebang in the first line will be interpreted.
# 3  
Old 10-23-2015
Hello Ashima,

In awk value of variables dosn't work like shell ones. Could you please try following and let me know if this helps you.
Code:
#!/bin/bash
NAME="world"
awk -vname=$NAME  '{if($0 ~ /name/){for (i=2; i<NF; i++) printf $i " "; print $NF}}1' myfile >tmp$$ ; mv tmp$$ myfile

Let me know if you have any queries.


Thanks,
R. Singh

Last edited by RavinderSingh13; 10-23-2015 at 11:43 AM.. Reason: Edited the code by adding if condition. Thanks to vgresh.
# 4  
Old 10-23-2015
Quote:
Originally Posted by RavinderSingh13
Hello Ashima,

In awk value of variables dosn't work like shell ones. Could you please try following and let me know if this helps you.
Code:
#!/bin/bash
NAME="world"
awk -vname=$NAME  '/name/{for (i=2; i<NF; i++) printf $i " "; print $NF}1' myfile >tmp$$ ; mv tmp$$ myfile

Let me know if you have any queries.


Thanks,
R. Singh
no, this won't work either for the reasons RudiC explained above.
Code:
#!/bin/bash
NAME="world"
awk -vname=$NAME  '$0 ~ name{for (i=2; i<NF; i++) printf $i " "; print $NF}1' myfile >tmp$$ ; mv tmp$$ myfile

This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 10-23-2015
Code:
awk '/world/{for (i=2; i<NF; i++) printf $i " "; print $NF}1' myfile >tmp$$ ; mv tmp$$ myfile

I am afraid that the above command will never produce what you said, but rather the following:

Code:
hello world my friend
#  hello world my friend
# this is a beautiful place
$ mary had a little lamb
# jhony jhony yes papa

If your intention is to remove the comment and the spaces in front of any sentence that has the word `world', perhaps sed would be a bit more suitable for it.

Code:
#!/bin/bash
NAME="world"
sed "/$NAME/ s/^#[[:blank:]]*//" myfile >tmp$$
mv tmp$$ myfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to check a command executed sucessfully or not

Hi All, I am trying to write a shell script to check if a command executed successfully or not in rhel 7 and finding the installed tomcat version. I am using below script. var4=$(find / -name "catalina.jar" ! -size 0 |egrep -v... (6 Replies)
Discussion started by: sravani25
6 Replies

2. UNIX for Dummies Questions & Answers

Set Command to output a log of every command executed in the script

Hi Guys, I like to output every command executed in the script to a file. I have tried set -x which does the same. But it is not giving the logs of the child script which is being called from my script. Is there any parameters in the Set command or someother way where i can see the log... (2 Replies)
Discussion started by: mac4rfree
2 Replies

3. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

4. UNIX for Dummies Questions & Answers

How to send keyboard inputs toa UNIX command executed from a shell script?

I have a unix command that prompts for 'y'. How do I run this from my shell script? (4 Replies)
Discussion started by: Sree10
4 Replies

5. Shell Programming and Scripting

Pass awk field to a command line executed within awk

Hi, I am trying to pass awk field to a command line executed within awk (need to convert a timestamp into formatted date). All my attempts failed this far. Here's an example. It works fine with timestamp hard-codded into the command echo "1381653229 something" |awk 'BEGIN{cmd="date -d... (4 Replies)
Discussion started by: tuxer
4 Replies

6. Shell Programming and Scripting

Shell script not getting executed

Hi As per my requirement when I run . ./file.sh am getting the following error -bash:ELF: command not found when i execute as ./file.sh it is getting executed.How to resolve this. Thanks in advance. (3 Replies)
Discussion started by: pracheth
3 Replies

7. Shell Programming and Scripting

Need help! command working ok when executed in command line, but fails when run inside a script!

Hi everyone, when executing this command in unix: echo "WM7 Fatal Alerts:", $(cat query1.txt) > a.csvIt works fine, but running this command in a shell script gives an error saying that there's a syntax error. here is content of my script: tdbsrvr$ vi hc.sh "hc.sh" 22 lines, 509... (4 Replies)
Discussion started by: 4dirk1
4 Replies

8. Shell Programming and Scripting

Verifying if the shell command executed or not?

Hi, I am working on a shell script that would verify if the mount command has executed or not. So , i have been doing this. mount /dev/cdrom /mnt/cdrom echo "$?" if ; then echo " Mount succesful" else echo " Mount unsuccessful" fi (3 Replies)
Discussion started by: eamani_sun
3 Replies

9. Shell Programming and Scripting

perl - why is the shell script executed before the print command?

i'm writing some simple scripts to help me learn perl. why does the print command get called after the shell script is executed? the purpose of the shell script is to simply echo to the screen "script run". which is does, but before the print command, you can clearly see the shell script is... (3 Replies)
Discussion started by: mjays
3 Replies

10. Shell Programming and Scripting

problem executed shell command from PL/SQL

i wrote plsql procedure that executed shell command using java class my problem is that in some reason the shell command ( liks Is -l , mv ... ) are not recordnize can someone help me with that 10x Alodvg (2 Replies)
Discussion started by: alodvg
2 Replies
Login or Register to Ask a Question