0

i have a command and pipe into different conditions as follows,

cat $txt | grep -v "\/\*" | awk -F$'\t' '{if ($16=="N") print $0}' | awk -F$'\t' '{if ($48=="Y" || $48==".") print $7}'

this command works perfect in the terminal

However, when I include the command in a bash.sh, it does't work at all. I created a test.sh The script just looks like this :

#!/bin/bash     
txt=file.txt
cat $txt | grep -v "\/\*" | awk -F$'\t' '{if ($16=="N") print $0}' | awk -F$'\t' '{if ($48=="Y" || $48==".") print $7}' >> output.txt

Then, I simplely use

sh test.sh

It generated a output.txt, but this is no any information in output.txt

Anyone has any idea what is different ?

user3631848
  • 433
  • 1
  • 6
  • 14
  • 3
    What happens when you put it in a script? What does the script look like? What errors or messages (if any) do you get? How do you attempt to run the script? – Some programmer dude Aug 23 '16 at 09:20
  • Did you try debugging by setting `set -x` in the beginning of your script? – Inian Aug 23 '16 at 09:30
  • 1
    The simplest way of debugging your script might be to do one thing at a time, starting with something you don't do now: Echoing the contents of `$txt`. Continue by just doing `cat $txt`. Then continue doing `cat $txt | grep ...`. And so on, verifying that each thing does what it's supposed to do. Once you find something that you don't expect, you can look closer into that. – Some programmer dude Aug 23 '16 at 09:31
  • Remove `>>output.txt` and check whether your script is generating the output or not first. – Sriharsha Kalluru Aug 23 '16 at 09:34
  • @SriharshaKalluru yes, I tried. There is no any output. I guess there is something wrong in awk. However, I have no idea how this could be different between bash and command line. – user3631848 Aug 23 '16 at 09:37
  • I tried both of them, your script works correctly, could you provided sample lines in file.txt – Mustafa DOGRU Aug 23 '16 at 11:22
  • 1
    Don't invoke it using `sh`, just run the script directly. Then the difference could be your `PATH`. Use `echo` in the script and the shell to see what is different. – chicks Aug 23 '16 at 12:16
  • As an aside, that's a [useless use of `cat`](http://www. Iki.fi/era/unix/award.html), a useless use of `grep | awk`, and (impressively) a useless use of `awk | awk`. – tripleee Aug 23 '16 at 13:35
  • Awk understands `-F '\t'`; the Bashism isn't really necessary. – tripleee Aug 23 '16 at 13:36
  • Provie the contents of `file.txt` then only we can see what is missing – Sriharsha Kalluru Aug 24 '16 at 02:43

1 Answers1

2

Most likely sh is not bash - run as bash test.sh - $'\t' is bash and may not work in other shells - also awk -F'\t' works just as well