line 43: syntax error: unexpected end of file - unix

I am getting a syntax error on line 43 when compiling the unix script code. This code is to search through a folder of textfile and match any word from the input.txt.
code still hasn't finish yet though
#!/bin/bash
findkeyword () {
file="$1"
keyword="$2"
value="$3"
int count = 0
cat $file | awk '{
while read line
do
for (ii=1;ii<=NF;ii++) {
if ($ii == $keyword)
count++
fi
}
done
}'
echo "Profile: " $file
scorefile $value $count
scorefile () {
value="$1"
count="$2"
echo "Score: " $value*$count
}
cat input.txt | awk '{
while read line
do
keyword=$1
value=$2
for xx in `ls submissions/*`
do
filename=$xx
findkeyword $filename $keyword $value
done
done
}'

#!/bin/bash
findkeyword () {
file="$1"
keyword="$2"
value="$3"
int count = 0
while read line; do
for word in $line; do
if [[ $word = $keyword ]]; then
$((count++))
fi
done
done <$file
echo "Profile: " $file
scorefile $value $count
} # missing closing brace here
scorefile () {
value="$1"
count="$2"
echo "Score: " $(($value*$count)) # Math evaluation broken
}
while read line
do
keyword=$1
value=$2
for xx in `ls submissions/*`
do
filename=$xx
findkeyword $filename $keyword $value
done
done<input.txt

Related

Remove all the hash out value of teradata in unix

Can anyone help me on this
I have one variable which contain
var="DDP_COMPLIANCE ,ONT_COMPLIANCE_CD --,TECHNICIAN_EMPLOYEE_ID --,ENTRY_OPERATOR_EMPLOYEE_ID --,LAST_UPDATE_OPERATOR_EMPLOYEE_ID ,PARENT_TROUBLE_CALL_WORK_ORDER_ID --,abc_line ,PARENT_INSTALL_WORK_ORDER_ID ,TECH_COMPLIANCE_STARTBY_TS "
i want to remove all the "--,columnname" and print rest
and the output will be
DDP_COMPLIANCE ,ONT_COMPLIANCE_CD,PARENT_TROUBLE_CALL_WORK_ORDER_ID ,PARENT_INSTALL_WORK_ORDER_ID ,TECH_COMPLIANCE_STARTBY_TS
i am using Sed command as
echo $var | sed 's/--.*,/,/'
but got output as
DDP_COMPLIANCE ,ONTRAC_COMPLIANCE_CD ,TECH_COMPLIANCE_STARTBY_TS
which is incorrect
I'm sure there's a simple way, but for time being, this works for you?
echo $var | awk '{ for (i=1; i<=NF; i++) print $i }' | grep -v "^--" | awk 'BEGIN { ORS=" " }; { print $0 }'
the last awk part was to format the result back in a single line.
Result:
$ echo $var | awk '{ for (i=1; i<=NF; i++) print $i }' | grep -v "^--" | awk 'BEGIN { ORS=" " }; { print $0 }'
DDP_COMPLIANCE ,ONT_COMPLIANCE_CD ,PARENT_TROUBLE_CALL_WORK_ORDER_ID ,PARENT_INSTALL_WORK_ORDER_ID ,TECH_COMPLIANCE_STARTBY_TS

What is "c==0 { c=0;system("cat") }" in AWK

I found this command in dockerfile like below.
echo -e 'skip-host-cache\nskip-name-resolve' | awk '{ print } $1 == "[mysqld]" && c == 0 { c = 1; system("cat") }' /etc/my.cnf > /tmp/my.cnf
I couldn't undersand it.
The awk program prints each line from the input file. If it finds the first line that starts with [mysqld], it uses the system command cat to print the text from the pipe.
Personally, I would use something like this:
awk -v add='skip-host-cache\nskip-name-resolve' \
'{ print } $1 == "[mysqld]" && found == 0 { print add; found = 1; }' my.cnf

If: Expression syntax - Error with a C Shell Script

I'm working on debugging a C-shell script that was given to me to add options to. This script, clean, is supposed to display the name of each file in a given directory and allows the user to decide whether or not keep or delete them. The problem is, when executed with a directory as an argument (or without) the error "If: Expression Syntax." Here's what it looks like. (now, as of 11/11)
## script name: clean
## helps to remove unwanted files from a directory
#!/bin/csh -vx
if ($#argv !=1) then
echo usage: $0 directory; exit(1)
else if (! -d $1) then
echo $1 not a directory; exit(1)
endif
set dir = $1
chdir $dir
set files = *
# process files
foreach file ($files)
if (! -f $file) continue
echo ' ' ## gives a blank line
echo "file = $file" ## identifies file being processed
echo ' '
head $file
while (1)
echo -n rm $file '?? (y, n, \!, or q)' :
set c = $<
switch ($c)
case y:
if ( {rm $file} ) then
echo '*****' $file rm-ed
else
echo cannot rm $file
endif
break ## break out of while
case n:
echo '*****' $file not rm-ed
break
case q:
exit(0)
case \!:
echo command:
eval $<
## in $< the variable $file can be used
endsw
end ## of while
end ## of foreach

Unix run a script with -help option

I have the below script that is expected to work when the user invokes sh <scriptName> <propertyfile> It does work when I provide this at the dollar prompt. However, I am having two issues with the script.
If I provide just one argument, ie if I do - sh <scriptName>, I see the below error -
my-llt-utvsg$ sh temp.sh
Usage temp.sh
When I do -help, I see the below error -
my-llt-utvsg$ sh tmp.sh -help
-help does not exist
What am I doing wrong? Can someone please advise? I am a software developer that very rarely needs to do shell scripting, so please go easy on me ;)
#!/bin/bash
FILE="system.properties"
FILE=$1
if [ ! -f $FILE ];
then
echo "$FILE does not exist"
exit
fi
usage ()
{
echo "Usage $0 $FILE"
exit
}
if [ "$#" -ne 1 ]
then
usage
fi
if [ "$1" = "-help" ] ; then
echo ""
echo '############ HELP PROPERTIES ############ '
echo ""
echo 'Blah.'
exit
The reason your
if [ "$1" = "-help" ] ; then
check is not working is that it only checks $1 or the first argument.
Try instead:
for var in "$#"
do
if [ "$var" = "-help" ] ; then
echo ""
echo '############ HELP PROPERTIES ############ '
echo ""
echo 'Blah.'
fi
done
Which will loop over each argument and so will run if any of them are -help.
Try this as well:
#!/bin/bash
FILES=()
function show_help_info_and_exit {
echo ""
echo '############ HELP PROPERTIES ############ '
echo ""
echo 'Blah.'
exit
}
function show_usage_and_exit {
echo "Usage: $0 file"
exit
}
for __; do
if [[ $__ == -help ]]; then
show_help_info_and_exit
elif [[ -f $__ ]]; then
FILES+=("$__")
else
echo "Invalid argument or file does not exist: $__"
show_usage_and_exit
fi
done
if [[ ${#FILES[#]} -ne 1 ]]; then
echo "Invalid number of file arguments."
show_usage_and_exit
fi
echo "$FILES"

awk command not working

My code gives me an error at line 3 with message:
syntax error near unexpected token ``ARRAY1=$(awk 'FNR == 1{print $2}' $file)
set -vx
for file in ls bowtie-0.12.7-win32/bowtie-0.12.7/Hits_635_25bp/*.txt
ARRAY1=$(awk 'FNR == 1{print $2}' $file) # stores the value
let ARRAY1=$ARRAY1/100+1
echo $ARRAY1
value1=$(awk 'FNR == '$ARRAY1'{print $1}' bowtie-0.12.7-win32/bowtie-0.12.7/list_for_635_fasta.list) # GETS THE VALUES FROM THE LIST
echo $value1
value2=$(awk 'FNR == 1{print $2,$3,$4,$5,$6,$7}' bwa-0.5.9/bwa-0.5.9/GENOMES/${file:0:13}) # GETS THE VALUES FROM THE LIST
echo $value2
done
I'd appreciate it if someone could point out the mistake. It seems to make sense, but I guess I'm missing something.
Try this:
set -vx
for file in bowtie-0.12.7-win32/bowtie-0.12.7/Hits_635_25bp/*.txt; do
ARRAY1=$( awk 'FNR == 1 { print $2 }' "$file" )
let ARRAY1=$ARRAY1/100+1
echo "$ARRAY1"
value1=$( awk -v a1="$ARRAY1" 'FNR == a1 { print $1 }' bowtie-0.12.7-win32/bowtie-0.12.7/list_for_635_fasta.list )
echo "$value1"
value2=$( awk 'FNR == 1 { print $2, $3, $4, $5, $6, $7 }' bwa-0.5.9/bwa-0.5.9/GENOMES/"${file:0:13}" )
echo "$value2"
done
If you tell us what shell you're using, we could improve the code.
for file in ls bowtie-0.12.7-win32/bowtie-0.12.7/Hits_635_25bp/*.txt
above line should be
for file in `ls bowtie-0.12.7-win32/bowtie-0.12.7/Hits_635_25bp/*.txt`
do

Resources