I have a csv file with data as below :

12,1234,28-07-2018 05:28:12.21,ABC60,3456,28-07-2018 05:28:12.21,AB60,7580,28-07-2018 06:28:12.21,PQ

I need to remove all date columns from the file through Unix.

closed as unclear what you're asking by jasonwryan, Jeff Schaller, thrig, maulinglawns, αғsнιη Sep 6 '18 at 2:19

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Are the positions of the date columns always the same? – KM. Sep 5 '18 at 17:54
  • Yes position is always same for date column – UnixLearner Sep 6 '18 at 9:00

Using awk:

echo "12,1234,28-07-2018 05:28:12.21,ABC60,3456,28-07-2018 05:28:12.21,AB60,7580,28-07-2018 06:28:12.21,PQ" | awk -F ','  '{for (i=1;i<=NF;i++) if($i !~ "-") printf "%s,",$i}' 

  • prints the columns which are not having -

Assuming there are no embedded commas inside a comma-delimited field, and that you want to remove columns 3, 6 and 9.

$ cut -d, -f1,2,4,5,7,8,10 <file

The cut command extracts the given columns out of a file. By default, tabs are used as column delimiters, but with -d, we set the delimiter to a comma. The -f option takes the column numbers (or ranges of column numbers) that are to be extracted.

Using csvcut from CSVkit, which is a real CSV parser, we can also handle fields which contains embedded commas:

$ csvcut -c 1,2,4,5,7,8,10 file

This tool also handles cutting on column names if the file has headers for each column.


Albeit not awk, but...

tr ',' '\n' < csv | grep -E -v '^[0-9]{2}-[0-9]{2}-[0-9]{4}' | tr '\n' ','

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