Skip to main content
Rollback to Revision 6
Source Link
αғsнιη
  • 41.9k
  • 17
  • 75
  • 117

I am having a csv file with Header. I would like to create a bash script without using AWK to calculate the sum of let's say the 6th column if the variable on the column 13 equals to an input that I will pass via CMD.

File looks like:

IDENTIF,RIVER,LOCATION,ERECTED,PURPOSE,LENGTH,LANES,CLEAR-G,T-OR-D,MATERIAL,SPAN,REL-L,TYPE
E2,A,25,1819,HIGHWAY,1037,2,N,THROUGH,WOOD,SHORT,S,WOOD
E7,A,27,1840,HIGHWAY,990,2,N,THROUGH,WOOD,MEDIUM,S,WOOD
E16,A,25,1859,HIGHWAY,1030,2,N,THROUGH,IRON,MEDIUM,S-F,SUSPEN

So I would like for example if I execute script.sh WOOD , to print the sum of the 6th columns for the values in column 13 that equal to WOOD.

I have seen many solutions using AWK and solving it immediately. I was hoping how would you do the same without it.

EDITCode I tried:

Solved thanks to steeldriver

#!/bin/bash

skip_line = 0
total_length = 0
while IFS=, read -r -a fields 
do
if [ $skip_line eq 0 ]
then
  skip_line = $(($skip_line + 1))
  continue
fi

if [ ${fields[12]} == $1 ]
then
    total_length = $(($total_length + ${fields[5]}))
fi
done < file.csv

echo "$total_length"

I am having a csv file with Header. I would like to create a bash script without using AWK to calculate the sum of let's say the 6th column if the variable on the column 13 equals to an input that I will pass via CMD.

File looks like:

IDENTIF,RIVER,LOCATION,ERECTED,PURPOSE,LENGTH,LANES,CLEAR-G,T-OR-D,MATERIAL,SPAN,REL-L,TYPE
E2,A,25,1819,HIGHWAY,1037,2,N,THROUGH,WOOD,SHORT,S,WOOD
E7,A,27,1840,HIGHWAY,990,2,N,THROUGH,WOOD,MEDIUM,S,WOOD
E16,A,25,1859,HIGHWAY,1030,2,N,THROUGH,IRON,MEDIUM,S-F,SUSPEN

So I would like for example if I execute script.sh WOOD , to print the sum of the 6th columns for the values in column 13 that equal to WOOD.

I have seen many solutions using AWK and solving it immediately. I was hoping how would you do the same without it.

EDIT:

Solved thanks to steeldriver

I am having a csv file with Header. I would like to create a bash script without using AWK to calculate the sum of let's say the 6th column if the variable on the column 13 equals to an input that I will pass via CMD.

File looks like:

IDENTIF,RIVER,LOCATION,ERECTED,PURPOSE,LENGTH,LANES,CLEAR-G,T-OR-D,MATERIAL,SPAN,REL-L,TYPE
E2,A,25,1819,HIGHWAY,1037,2,N,THROUGH,WOOD,SHORT,S,WOOD
E7,A,27,1840,HIGHWAY,990,2,N,THROUGH,WOOD,MEDIUM,S,WOOD
E16,A,25,1859,HIGHWAY,1030,2,N,THROUGH,IRON,MEDIUM,S-F,SUSPEN

So I would like for example if I execute script.sh WOOD , to print the sum of the 6th columns for the values in column 13 that equal to WOOD.

I have seen many solutions using AWK and solving it immediately. I was hoping how would you do the same without it.

Code I tried:

#!/bin/bash

skip_line = 0
total_length = 0
while IFS=, read -r -a fields 
do
if [ $skip_line eq 0 ]
then
  skip_line = $(($skip_line + 1))
  continue
fi

if [ ${fields[12]} == $1 ]
then
    total_length = $(($total_length + ${fields[5]}))
fi
done < file.csv

echo "$total_length"
deleted 346 characters in body
Source Link

I am having a csv file with Header. I would like to create a bash script without using AWK to calculate the sum of let's say the 6th column if the variable on the column 13 equals to an input that I will pass via CMD.

File looks like:

IDENTIF,RIVER,LOCATION,ERECTED,PURPOSE,LENGTH,LANES,CLEAR-G,T-OR-D,MATERIAL,SPAN,REL-L,TYPE
E2,A,25,1819,HIGHWAY,1037,2,N,THROUGH,WOOD,SHORT,S,WOOD
E7,A,27,1840,HIGHWAY,990,2,N,THROUGH,WOOD,MEDIUM,S,WOOD
E16,A,25,1859,HIGHWAY,1030,2,N,THROUGH,IRON,MEDIUM,S-F,SUSPEN

So I would like for example if I execute script.sh WOOD , to print the sum of the 6th columns for the values in column 13 that equal to WOOD.

I have seen many solutions using AWK and solving it immediately. I was hoping how would you do the same without it.

Code I triedEDIT:

#!/bin/bash

skip_line = 0
total_length = 0
while IFS=, read -r -a fields 
do
if [ $skip_line eq 0 ]
then
  skip_line = $(($skip_line + 1))
  continue
fi

if [ ${fields[12]} == $1 ]
then
    total_length = $(($total_length + ${fields[5]}))
fi
done < file.csv

echo "$total_length"

Solved thanks to steeldriver

I am having a csv file with Header. I would like to create a bash script without using AWK to calculate the sum of let's say the 6th column if the variable on the column 13 equals to an input that I will pass via CMD.

File looks like:

IDENTIF,RIVER,LOCATION,ERECTED,PURPOSE,LENGTH,LANES,CLEAR-G,T-OR-D,MATERIAL,SPAN,REL-L,TYPE
E2,A,25,1819,HIGHWAY,1037,2,N,THROUGH,WOOD,SHORT,S,WOOD
E7,A,27,1840,HIGHWAY,990,2,N,THROUGH,WOOD,MEDIUM,S,WOOD
E16,A,25,1859,HIGHWAY,1030,2,N,THROUGH,IRON,MEDIUM,S-F,SUSPEN

So I would like for example if I execute script.sh WOOD , to print the sum of the 6th columns for the values in column 13 that equal to WOOD.

I have seen many solutions using AWK and solving it immediately. I was hoping how would you do the same without it.

Code I tried:

#!/bin/bash

skip_line = 0
total_length = 0
while IFS=, read -r -a fields 
do
if [ $skip_line eq 0 ]
then
  skip_line = $(($skip_line + 1))
  continue
fi

if [ ${fields[12]} == $1 ]
then
    total_length = $(($total_length + ${fields[5]}))
fi
done < file.csv

echo "$total_length"

I am having a csv file with Header. I would like to create a bash script without using AWK to calculate the sum of let's say the 6th column if the variable on the column 13 equals to an input that I will pass via CMD.

File looks like:

IDENTIF,RIVER,LOCATION,ERECTED,PURPOSE,LENGTH,LANES,CLEAR-G,T-OR-D,MATERIAL,SPAN,REL-L,TYPE
E2,A,25,1819,HIGHWAY,1037,2,N,THROUGH,WOOD,SHORT,S,WOOD
E7,A,27,1840,HIGHWAY,990,2,N,THROUGH,WOOD,MEDIUM,S,WOOD
E16,A,25,1859,HIGHWAY,1030,2,N,THROUGH,IRON,MEDIUM,S-F,SUSPEN

So I would like for example if I execute script.sh WOOD , to print the sum of the 6th columns for the values in column 13 that equal to WOOD.

I have seen many solutions using AWK and solving it immediately. I was hoping how would you do the same without it.

EDIT:

Solved thanks to steeldriver

added 4 characters in body
Source Link

I am having a csv file with Header. I would like to create a bash script without using AWK to calculate the sum of let's say the 6th column if the variable on the column 13 equals to an input that I will pass via CMD.

File looks like:

IDENTIF,RIVER,LOCATION,ERECTED,PURPOSE,LENGTH,LANES,CLEAR-G,T-OR-D,MATERIAL,SPAN,REL-L,TYPE
E2,A,25,1819,HIGHWAY,1037,2,N,THROUGH,WOOD,SHORT,S,WOOD
E7,A,27,1840,HIGHWAY,990,2,N,THROUGH,WOOD,MEDIUM,S,WOOD
E16,A,25,1859,HIGHWAY,1030,2,N,THROUGH,IRON,MEDIUM,S-F,SUSPEN

So I would like for example if I execute script.sh WOOD , to print the sum of the 6th columns for the values in column 13 that equal to WOOD.

I have seen many solutions using AWK and solving it immediately. I was hoping how would you do the same without it.

Code I tried:

#!/bin/bash

skip_line = 0
total_length = 0
while IFS=, read -r -a fields 
do
if [$skip_line[ $skip_line eq 0]0 ]
then
  skip_line = $(($skip_line + 1))
  continue
fi

if [$[ ${fields[12]} eq== $1]$1 ]
then
    total_length = $(($total_length + ${fields[5]}))
fi
done < file.csv

echo "$total_length"

I am having a csv file with Header. I would like to create a bash script without using AWK to calculate the sum of let's say the 6th column if the variable on the column 13 equals to an input that I will pass via CMD.

File looks like:

IDENTIF,RIVER,LOCATION,ERECTED,PURPOSE,LENGTH,LANES,CLEAR-G,T-OR-D,MATERIAL,SPAN,REL-L,TYPE
E2,A,25,1819,HIGHWAY,1037,2,N,THROUGH,WOOD,SHORT,S,WOOD
E7,A,27,1840,HIGHWAY,990,2,N,THROUGH,WOOD,MEDIUM,S,WOOD
E16,A,25,1859,HIGHWAY,1030,2,N,THROUGH,IRON,MEDIUM,S-F,SUSPEN

So I would like for example if I execute script.sh WOOD , to print the sum of the 6th columns for the values in column 13 that equal to WOOD.

I have seen many solutions using AWK and solving it immediately. I was hoping how would you do the same without it.

Code I tried:

#!/bin/bash

skip_line = 0
total_length = 0
while IFS=, read -r -a fields 
do
if [$skip_line eq 0]
then
  skip_line = $(($skip_line + 1))
  continue
fi

if [${fields[12]} eq $1]
then
    total_length = $(($total_length + ${fields[5]}))
fi
done < file.csv

echo "$total_length"

I am having a csv file with Header. I would like to create a bash script without using AWK to calculate the sum of let's say the 6th column if the variable on the column 13 equals to an input that I will pass via CMD.

File looks like:

IDENTIF,RIVER,LOCATION,ERECTED,PURPOSE,LENGTH,LANES,CLEAR-G,T-OR-D,MATERIAL,SPAN,REL-L,TYPE
E2,A,25,1819,HIGHWAY,1037,2,N,THROUGH,WOOD,SHORT,S,WOOD
E7,A,27,1840,HIGHWAY,990,2,N,THROUGH,WOOD,MEDIUM,S,WOOD
E16,A,25,1859,HIGHWAY,1030,2,N,THROUGH,IRON,MEDIUM,S-F,SUSPEN

So I would like for example if I execute script.sh WOOD , to print the sum of the 6th columns for the values in column 13 that equal to WOOD.

I have seen many solutions using AWK and solving it immediately. I was hoping how would you do the same without it.

Code I tried:

#!/bin/bash

skip_line = 0
total_length = 0
while IFS=, read -r -a fields 
do
if [ $skip_line eq 0 ]
then
  skip_line = $(($skip_line + 1))
  continue
fi

if [ ${fields[12]} == $1 ]
then
    total_length = $(($total_length + ${fields[5]}))
fi
done < file.csv

echo "$total_length"
added 391 characters in body
Source Link
Loading
added 391 characters in body
Source Link
Loading
deleted 6 characters in body
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718
Loading
added 103 characters in body
Source Link
Loading
Source Link
Loading