user3068067
user3068067

Reputation: 1

Awk Sum skipping Special Character Row

I am trying to take the sum of a particular column in a file i.e. column 18. Using awk command along with Printf to display it in proper decimal format.

SUM=`cat ${INF_TARGET_FILE_PATH}/${EXTRACT_NAME}_${CURRENT_DT}.txt|awk -F"" '{s+=$18}END{printf("%24.2f\n", s)}'

Above command is skipping those rows in file which has the special character in one of the column 5 - RÉPARATIONS. Hence Awk skips these rows and doesnt consider sum for that row. Please help how to resolve this issue to take sum of all rows.

Upvotes: 0

Views: 147

Answers (1)

Jotne
Jotne

Reputation: 41460

There is missing a back tics in your example, should be:

SUM=`cat ${INF_TARGET_FILE_PATH}/${EXTRACT_NAME}_${CURRENT_DT}.txt|awk -F"" '{s+=$18}END{printf("%24.2f\n", s)}'`

But you should not use back tics, you should use parentheses $(code)
Using cat to enter data to awk is also wrong way to do it, add pat after awk

SUM=$(awk -F"" '{s+=$18} END {printf "%24.2f\n",s}' ${INF_TARGET_FILE_PATH}/${EXTRACT_NAME}_${CURRENT_DT}.txt)

This may resolve your problem, but gives a more correct code.
If you give us your input file, it would help us to understand the problem.

Upvotes: 1

Related Questions