I wanted to remove white space from a CSV file, which I am able to do using s/\ //g, but at the same time I want to avoid removing the space between fields which has timestamps e.g. "06-JAN-15 13:20:00": currently it joins them which was expected "06-JAN-1513:20:00".

One solution is to let it delete all the white spaces and then grep for date 06-JAN-15 and add space after it. Not so sure how that can be done.

Sample CSV file: (one line only)

294335,"17-APR-15 00:00:00 ",6258,"C"," <-----huge blank space--->,"07-JAN-15 00:00:00"

huge blank space will contain XML message if not blank.

  • Crossposting: stackoverflow.com/questions/31145618/… – Cyrus Jun 30 '15 at 19:06
  • Please provide some example lines and your desired output from them.. – heemayl Jun 30 '15 at 19:18
  • ... or are you just trying to collapse any entirely blank fields? – steeldriver Jun 30 '15 at 19:54
  • Remove all whitespaces but in between numbers: s/\([^0-9]\)\s\+\([^0-9]\)/\1\2/g – Costas Jun 30 '15 at 20:05
  • 1
    @drewbenn adding " s/ \+/ /g " removed the blank space with single space. Thanks a lot new output 294335,"11-JUN-15 00:00:00 ",0,"N"," ",30-JUN-15 13:30:00 " – JavaQuest Jun 30 '15 at 20:15

Not sed, but here's a solution using negative lookbehind assertion (?<!...) in perlre.

perl -pe 's/(?<!\d\d-(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)-\d\d) //g'

Using awk:

< input awk 'BEGIN {FS=OFS=","} {gsub(" ", "", $1); gsub(" ", "", $3); gsub(" ", "", $4); gsub(" ", "", $5); print}'

This will have the effect of removing any space character from the 1st, 3rd, 4th and 5th comma-separated field in each record.


You could actually parse the file as a CSV file, and strip extra whitespace:

ruby -rcsv -ne '
    row = CSV::parse_line($_)
    puts CSV::generate_line(row.map {|field| field.strip})
' file
294335,17-APR-15 00:00:00,6258,C,"",07-JAN-15 00:00:00

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.