0

how to cut the selected columns from the file.

Example:

load.sh students.csv | select.sh ‘name’ ‘school_name’ 

This will output the name and school_name columna from the loaded file to the terminal.

  • 1
    Please provide sample input and expected output – Jesse_b Mar 22 at 13:17
  • @Jesse_b, Agreed. Please provide example which will be more easy. Is it not possible to do so in same script ? – Vivek Kanadiya Mar 22 at 13:25
  • You're thinking too much like Windows Powershell. UNIX/Linux shells don't pass objects through pipes. – roaima Mar 23 at 13:36
2

Use csvcut or csvsql from csvkit:

csvcut -c name,school_name students.csv
csvsql --query 'select name, school_name from students' students.csv

Example:

$ cat students.csv
id,name,surname,school_name,favorite_color
1,Tom,Sawyer,Harvard,red
2,Bob,"the Builder",Oxford,orange
3,John,Sinclair,"Columbine High",blue
4,Walter,Mitty,"Thomas Jefferson High",green
5,Donald,Trump,Unknown,blue

$ csvcut -c name,school_name students.csv
name,school_name
Tom,Harvard
Bob,Oxford
John,Columbine High
Walter,Thomas Jefferson High
Donald,Unknown
  • cvscut would be easier, if the columns have named headers. – Kusalananda Mar 22 at 14:24
  • True... I'll add that.csvsql is good if we need some grouping/joins etc, but for such simple select you're right. – RoVo Mar 22 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.