Skip to content

Latest commit

 

History

History
42 lines (30 loc) · 618 Bytes

bash-helper-snippets.md

File metadata and controls

42 lines (30 loc) · 618 Bytes

Bash Helper Snippets

Check if file exists

if [ ! -f /tmp/foo.txt ]; then
    echo "File not found!"
fi

Manipulate CSV file

{% code title="csv_split.sh" %}

# Manipulate csv data using bash
#
# You can use read to split the CSV, then output the desired number of
# columns.

cat test.csv | while read line; do
  IFS=\; read -ra COL <<< "$line"
  echo "${COL[0]};${COL[1]};${COL[2]};${COL[3]};${COL[4]};"
done

# For example, with test.csv containing:

1;2;3;4
1;2;3;4;5
1;2;3;4;
1;2;3;4;5;

# The above script outputs:

1;2;3;4;;
1;2;3;4;5;
1;2;3;4;;
1;2;3;4;5;

{% endcode %}