How to Use CUT command on CSV file in Ubuntu Linux

In this post, I have given cut CSV bash commands to cut specific columns from the input CSV file.

CSV files are comma-separated files. To manipulate in UBUNTU, you need first them to convert to text files. Below are the steps you need and these are tested commands in Ubuntu using Cut command and Libre Office.

How to Copy CSV file from Local Machine to Virtual Machine

  1. Drag and Drop the file
  2. Import the file to Ubuntu

How to Save CSV file in UBUNTU by Using Libre Office

In Ubuntu, you can save as the file you copied as ‘textcsvformat’ by using Libre office of Ubuntu.

When you save input.CSV file as TextCSV, you will lose format and formulas. This is fine. Anyway, you need to only deal with Text data.

How to Cut the Specific Columns From CSV File

The first step by using head -5 command, I have shown first 5 records.

$head -5 FL_insurance_sample.csv

policyID,statecode,county,eq_site_limit,hu_site_limit,fl_site_limit,fr_site_limit,tiv_2011,tiv_2012,eq_site_deductible,hu_site_deductible,fl_site_deductible,fr_site_deductible,point_latitude,point_longitude,line,construction,point_granularity

119736,FL,CLAY COUNTY,498960,498960,498960,498960,498960,792148.9,0,9979.2,0,0,30.102261,-81.711777,Residential,Masonry,1

448094,FL,CLAY COUNTY,1322376.3,1322376.3,1322376.3,1322376.3,1322376.3,1438163.57,0,0,0,0,30.063936,-81.707664,Residential,Masonry,3

206893,FL,CLAY COUNTY,190724.4,190724.4,190724.4,190724.4,190724.4,192476.78,0,0,0,0,30.089579,-81.700455,Residential,Wood,1

333743,FL,CLAY COUNTY,0,79520.76,0,0,79520.76,86854.48,0,0,0,0,30.063236,-81.707703,Residential,Wood,3

How to Use ‘Cut’ command in Linux

srinimf@ubuntu:~$ cut -d’,’ -f1-2 FL_insurance_sample.csv | head -5

policyID,statecode
119736,FL
448094,FL
206893,FL
333743,FL
srinimf@ubuntu:~$

Using the above command, first two columns (1-2) copied to output.

How to Copy the output ‘after CUT command’ to a File

srinimf@ubuntu:~$ cut -d’,’ -f1-2 FL_insurance_sample.csv | head -5 > myoutput.txt

 srinimf@ubuntu:~$ ls
 admin.conf               mydata.txt
 anaconda3               myoutput.txt
 Anaconda3-2018.12-Linux-x86_64.sh  mypython.py
 blockchain-network-on-kubernetes   myscripts.py
 [Command Line]               Pictures
 Desktop                   Public
 Documents               pycache
 Downloads               Python-3.7.2
 examples.desktop           Python-3.7.2.tgz
 FL_insurance_sample.csv           Templates
 go1.12.1.linux-amd64.tar.gz       Videos
 Music
 srinimf@ubuntu:~$ ^C 
 srinimf@ubuntu:~$ cat myoutput.txt
 policyID,statecode
 119736,FL
 448094,FL
 206893,FL
 333743,FL
 srinimf@ubuntu:~$  

Related Posts

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.