bash Cheatsheet
1. read line from file
#!/bin/bash
#
#example: my_read_file.sh input.csv
#
max_level=0
max_line=""
#loop read line
while read line || [ -n "$line" ]
#read line not end with '/n'
do
elements=(${line//|/ })
# IFS='|' read -a elements <<< "$line"
index_last=${#elements[@]}-1
level="${elements[$index_last]}"
#max level
if [ $level -gt $max_level ]
then
max_level=$level
max_line=$line
fi
done < "$1"
echo "$max_line"
2. sample
#/bin/bash
#
#usage: bash_sample.sh hello wolrd sample_file.csv
#read from typing
var_a1=$1
var_a2=$2
echo "${var_a1} ${var_a2}"
while read line || [ -n "$line" ]
do
elements=(${line//|/ })
len=${#elements[@]}
last_element=${elements[$len-1]}
if [ $last_element -gt 4 ]
then
echo $last_element
else
echo "not condition"
fi
done < $3
path_output="./output.txt"
rm "${path_output}"
#echo "test" >> "${path_output}"
cat $3 >> "${path_output}"
#current folder
for d in `ls ./`; do
echo ${d}
done
#parent folder
for d in `ls ../`; do
echo ${d}
done
3. merge files
cat ./* > bigfile.sql