Home > OS >  How to extract cloumn data from csv file using shell command
How to extract cloumn data from csv file using shell command

Time:10-05

I have the following CSV file as input

sample1 compute.googleapis.com/projects/project_ID1/zones/europe-west2-a test1
sample1 compute.googleapis.com/projects/project_ID2/zones/europe-west2-a test2
sample1 compute.googleapis.com/projects/project_ID3/zones/europe-west2-a test3

How I can use a shell command to extract project ID from column 2 of the above file and save it as CSV as shown below enter code here

sample1 project_ID1 test1
sample1 project_ID2 test2
sample1 project_ID3 test3

Thanks Ajit

CodePudding user response:

Wit awk:

$ awk -F '[/ ]' '{print $1, $4, $7}' FILE
sample1 project_ID1 test1
sample1 project_ID2 test2
sample1 project_ID3 test3

CodePudding user response:

Little bit of modification to above @Arkadiusz Drabczyk solution would give comma separated data

Code :

awk -F '[/ ]' '{print $1 "," $4 "," $7}' demo.txt

Expected Output :

sample1,project_ID1,test1
sample1,project_ID2,test2
sample1,project_ID3,test3

You can write the above data to new file using below command

awk -F '[/ ]' '{print $1 "," $4 "," $7}' demo.txt >> demo.csv
  • Related