Reputation: 582
I am trying to write a small shell script, which searches a pattern (string) in a .fas-file and prints the line and position, where the pattern was found. The following code snippet works, when I call the shell script:
#!/bin/bash
awk 's=index($0, "CAATCTCC"){print "line=" NR, "start position=" s}' 100nt_upstream_of_mTSS.fas
$ ./search.sh
When I change the script to:
awk 's=index($0, "CAATCTCC"){print "line=" NR, "start position=" s}'
and do the following command line call in my bash:
$ ./search.sh 100nt_upstream_of_mTSS.fas
"nothing" happens (something is running, but it takes way too long and no results come up, so terminate the process).
I might be "screen blind", but I can't find the reason, why I am unable to pass a command line argument to my script.
Furthermore, I would like to make the motif (the string) I search for variable. I tried this:
#!/bin/bash
FILE=$1
MOTIF=$2
awk 's=index($0, "$MOTIF"){print "line=" NR, "start position=" s}' "$FILE"
$ ./search.sh 100nt_upstream_of_mTSS.fas CAATCTCC
Idea: First command-line argument worked and was substitued correctly. Why is the second one not substituted correctly?
#!/bin/bash
file=$1
awk -v s="$2" 'i=index($0, s){print "line: " NR, "pos: " i}' "$file"
Testfile (test.txt):
1 GAGAGAGAGA
2 CTCTCTCTCT
3 TATATATATA
4 CGCGCGCGCG
5 CCCCCCCCCC
6 GGGGGGGGGG
7 AAAAAAAAAA
8 TTTTTTTTTT
9 TGATTTTTTT
10 CCCCCCCCGA
$ ./search.sh test.txt GA
will print:
line: 1 pos: 1
line: 4 pos: 2
line: 6 pos: 1
line: 9 pos: 2
line: 10 pos: 9
This script will print line and first match position in the line of only the first character of my pattern. How do I manage to have all results printed and the full pattern being used?
Upvotes: 2
Views: 4925
Reputation: 133730
As far as I understood you want to pass the Input_file(file which you want to process by script) as an argument, if this is the case then following may help you in same.
cat search.sh
#!/bin/bash
variable=$1
awk 's=index($0, "CAATCTCC"){print "line=" NR, "start position=" s}' "$variable"
./search.sh 100nt_upstream_of_mTSS.fas
Upvotes: 2