Saturday, May 2, 2015

Shell script example for dissecting a column in .csv file

#!/bin/bash
#echo "Please enter input  file in  csv file format: "
echo "File you entered is: $1"
inputfile=$1;
echo $inputfile
awk -F "," '{print $4}' $inputfile >> list.unsorted
sort list.unsorted | uniq -c | column -ts $'\t'| grep -R 'XXX*' > sortedlist.sed
awk 'BEGIN{printf("%-8s%-30s%-10s\n","#","Column1 Name","Column 2 Name")}{printf("%-8s%-30s%-10s\n",NR,$1,$2)}' sortedlist.sed > $inputfile "Final_Report.txt"
rm -r list.unsorted  sortedlist.sed

  • My string is present in 4th column so i am printing 4th column using awk
  • Printf() is very nice function to look into if you want to customize the spaces between the different columns.
consider the following example,in this example i am looking for the following pattern to check how many times this pattern has occurred.

since i was only looking for XXX* only it showed all the matching strings.this string input to grep can be any string of your choice.

Example Pattern for the above script.
XXX-YYYY-ZZ-AABBCCDD.1234


Output of the above script

#       No.of Times                   My Pattern
1       6                             XXX-YYYY-ZZ-AABBCCDD.1234

No comments:

Post a Comment