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.
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
I am afraid that the above command will never produce what you said, but rather the following:
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.
#!/bin/bash
NAME="world"
sed "/$NAME/ s/^#[[:blank:]]*//" myfile >tmp$$
mv tmp$$ myfile