How can I run "ack" in bash and save the output to a file?

469 views Asked by At

I have the following test.txt file with two lines

keyword 1, 2010-10-01 | 1 | 2 | 3 | 4
keyword 2, 2010-10-01 | 1 | 2 | 3 | 4

and would like to ack for keyword1 and keyword2. I call this script

#!/bin/bash

OUTPUTPATH=$1"/"
JOB=$2
ID="_"$3".csv"

for keyword in "${@:4}"
do
        trKeyWord=$(echo "$keyword" | sed 's/ //g')
        ack \""$keyword"\" $JOB > $OUTPUTPATH$trKeyWord$BSLD_ID
done

via

bash script.sh /home/username/ test.txt test "keyword 1" "keyword 2"

The ouput files keyword1_test.csv, keyword2_test.csv are created but they are empty! The problem must be how I am using ack \""$keyword"\" $JOB because just running this command gives an empty result.

1

There are 1 answers

2
tenticon On

Okay guys, this is the final (working) answer:

outputpath="$1"/
job="$2"
id=_"$3".csv
for keyword in "${@:4}"
do
  trKeyWord=${keyword// }
  ack "$keyword" $job > $outputpath$trKeyWord$id
done