0

I have a c program named calculate which sorts out the correct data

Data input (initial file.txt):

abcd!1023!92
ckdw!3251!io
efgh!9873!xk

Data returned:

abcd!1023!92
efgh!9873!xk

My shell script contains:

./calculate | awk -F '!' '{sum += $2} END{print sum}' "$1"

When I run the script ./check file.txt it ignores the values returned from the calculate function and instead calculates from the input file.

How do I fix this so that the "awk" function works from the data returned from the ./test function?

2
  • Make sure you don't name your test scripts as 'test'. On linux, 'test' in an existing program, and if you forget to use ./ then you might wonder what's wrong. Commented Mar 31, 2015 at 15:27
  • @Deanie it was actually called calculate just tried to rush the unimportant parts Commented Mar 31, 2015 at 15:30

2 Answers 2

1

You are passing a file to the awk script as well as input.

./calculate | awk -F '!' '{sum += $2} END{print sum}' "$1"

awk only uses one or the other and it prefers file arguments when given.

Drop the "$1" bit from there.

Sign up to request clarification or add additional context in comments.

Comments

1

I did my try.

sorting.sh (my version of your filtering program)

#!/usr/bin/env bash
egrep 'a|e' input.txt

program.sh (your shell command)

./program.sh | awk -F '!' '{sum += $2} END{print sum}'

@Updated, deleted "$1" as stated by Etan Reisner

2 Comments

Stick that pipeline in a script and pass the input file as an argument to the script and you'll see the problem.
@josegomezr my program was working. I just removed the "$1" like the other answer indicated and it seems to work fine!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.