shell_scripting
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
shell_scripting [2023-09-21 04:41 pm] – hcho | shell_scripting [2023-09-21 05:15 pm] (current) – hcho | ||
---|---|---|---|
Line 12: | Line 12: | ||
<code bash> | <code bash> | ||
+ | # set a variable to know if a file is the first one | ||
+ | # because we don't want to skip the head line from the first file | ||
first=1 | first=1 | ||
+ | # for each of *.csv file names, assign it to i | ||
for i in *.csv; do | for i in *.csv; do | ||
+ | # if you repeat this script, we don't want to merge | ||
+ | # the previously merged file again | ||
if [ " | if [ " | ||
+ | # skip this file | ||
continue | continue | ||
fi | fi | ||
+ | # if this file is first | ||
if [ $first -eq 1 ]; then | if [ $first -eq 1 ]; then | ||
+ | # print everything | ||
cat " | cat " | ||
+ | # subsequent files won't be first anymore | ||
first=0 | first=0 | ||
else | else | ||
+ | # for non-first files, skip the first line | ||
tail -n +2 " | tail -n +2 " | ||
fi | fi | ||
- | done > merged.csv | + | done > merged.csv # forward any outputs from the above for loop |
+ | # to merged.csv | ||
</ | </ | ||
+ | ===== Finding the order of a column in a CSV file ===== | ||
+ | <code bash> | ||
+ | head -1 merged.csv | | ||
+ | sed ' | ||
+ | awk '{ | ||
+ | for(i=1; i<=NF; i++) | ||
+ | if($i == " | ||
+ | print i | ||
+ | }' | ||
+ | </ | ||
+ | |||
+ | ===== Counting non-null records in a column in a CSV file ===== | ||
+ | |||
+ | <code bash> | ||
+ | tail -n +2 merged.csv | | ||
+ | sed ' | ||
+ | awk '{ | ||
+ | if($57 != "" | ||
+ | count++ | ||
+ | } | ||
+ | END{ | ||
+ | print count | ||
+ | }' | ||
+ | </ |
shell_scripting.1695336071.txt.gz · Last modified: 2023-09-21 04:41 pm by hcho