Jazzkatt
Jazzkatt

Reputation: 65

Specifying and manipulating columns inside file - shell/bash

line in file:

aaa, bbb, ccc, ddd, Sept 18, 2016 ##:##PM

trying to parse it out (desired output) -

aaa, bbb, ccc, ddd, "Sept 18, 2016 etc..."

using unix shell - trying to wrap last two columns in quotes for a date importing process. However, am trying to aim for a specified column approach in case a situation arises where the two columns are internal ie:

aaa, bbb, Sept 18, 2016 etc..., ccc, ddd

thus outputing:

aaa, bbb, "Sept 18, 2016 etc...", ccc, ddd

The command to display it is fine - as i can redirect it to a file and work with it from there

Upvotes: 0

Views: 56

Answers (2)

Nikolay Nikolov
Nikolay Nikolov

Reputation: 13

Your unix tool for column manipulation is awk:

awk 'BEGIN{FS=", ";OFS=", "} {$5="\""$5; $6=$6"\""; print $0;}' YOUR_FILE.txt

$5 and $6 are the numbers of the columns you need to put " around. If you want to put " before the 5th and after the 7th column, just update the awk expression accordingly.

Upvotes: 0

Charles Duffy
Charles Duffy

Reputation: 295649

The following works with GNU sed, when replacing ##:## in your sample data with actual digits (and assuming the "etc..." given in your second "example" to match the format from your first one):

sed -re 's@ ((Jan|Feb|Mar|Apr|May|June|July|Aug|Sept|Oct|Nov|Dec) [[:digit:]]+, [[:digit:]]{4} [[:digit:]]{1,2}:[[:digit:]]{2}[AP]M)([, ]|$)@ "\1"\3 @g'

Upvotes: 1

Related Questions