I have a big non-standard xhtml file that I'm using sed (about 4 times) to iterate down to just the basics I need to throw the data into a MySQL database. My last bit I'm struggling with. The file is formatted like: Tue Aug 18 20150,0,0,0,00,0,0,2,2750,0,0,3,2870,0,0,0,3270,0,0,3,3350,0,0,0,413 Wed Aug 19 20150,0,0,0,00,0,0,2,2750,0,0,3,2870,0,0,2,308 Thu Aug 20 20150,0,0,0,00,0,0,2,4580,0,0,3,4690,0,0,0,4720,0,0,3,5030,0,0,2,534There is always a space before the day.There can be any number of CSV value lines after the date.What I'm hoping can be achieved is:Tue Aug 18 2015,0,0,0,0,0Tue Aug 18 2015,0,0,0,2,275Tue Aug 18 2015,0,0,0,3,287Tue Aug 18 2015,0,0,0,0,327Tue Aug 18 2015,0,0,0,3,335Tue Aug 18 2015,0,0,0,0,413Wed Aug 19 2015,0,0,0,0,0Wed Aug 19 2015,0,0,0,2,275Wed Aug 19 2015,0,0,0,3,287Wed Aug 19 2015,0,0,0,2,308Thu Aug 20 2015,0,0,0,0,0Thu Aug 20 2015,0,0,0,2,458Thu Aug 20 2015,0,0,0,3,469Thu Aug 20 2015,0,0,0,0,472Thu Aug 20 2015,0,0,0,3,503Thu Aug 20 2015,0,0,0,2,534and if it's possible, to strip the day and throw in a few more commas for easier manipulation in a PHP script, like:Aug,18,2015,0,0,0,0,0Aug,18,2015,0,0,0,2,275Aug,18,2015,0,0,0,3,287Aug,18,2015,0,0,0,0,327Aug,18,2015,0,0,0,3,335Aug,18,2015,0,0,0,0,413Aug,19,2015,0,0,0,0,0Aug,19,2015,0,0,0,2,275Aug,19,2015,0,0,0,3,287Aug,19,2015,0,0,0,2,308Aug,20,2015,0,0,0,0,0Aug,20,2015,0,0,0,2,458Aug,20,2015,0,0,0,3,469Aug,20,2015,0,0,0,0,472Aug,20,2015,0,0,0,3,503Aug,20,2015,0,0,0,2,534Is there a few commands that can be used? I'm still pretty new to the command-line batch processing of text. If possible, could you include a small help as to what each part of the command does so I can figure out how to do it myself next time?
Get a string and place it at the start of a line until the next string is found
text processing;sed
Here's one way to do it:sed '/,/!{                       # if there's no comma on this liney/ /,/                           # translate spaces to commash                                # copy pattern space over the hold bufferd                                # delete pattern space}//{                              # if the line contains commasG                                # append hold space content to pattern spaces/\(.*\)\n,[^,]*,\(.*\)/\2,\1/   # swap lines removing newline, the day part and}                                # first two commas and adding a comma after year' infileIf you prefer a gnu sed one-liner:sed -E '/,/!{y/ /,/;h;d};//{G;s/(.*)\n,[^,]*,(.*)/\2,\1/}' infileIt's similar with awk:If the line contains no commas you could format the date via sprintf, save the result into a variable e.g. dt and then go to next record. Else just prepend dt to $0 (that is the current line):awk '!/,/{dt=sprintf(%s,%s,%s,, $2, $3, $4);next};$0=dt$0' infile