Friday, October 5, 2018

One More Request For Sed Help

I believe that I have figured out how to do what I need to this spreadsheet for export inside Excel except for the date transformations. 
sed -E "s!^[[:digit:]]\{4\}!0,0,&!" massmurder.csv|sed -E "s!\([[:digit:]]\)/\([[:digit:]]\(4\)\)!\1/0/\2!"
The first command does convert YYYY to 0/0/YYYY.  The second command should convert M/YYYY to M/0/YYYY.  But the second command produces:
sed: -e expression #1, char 47: invalid reference \2 on 's' command's RHS
What am I doing wrong?

1 comment:

Rick C said...

I think you've got some typos in your example, but it looks like you don't want to use -E. (I will admit I probably don't know sed as well as you; I never used it a lot.)

I eventually came up with this variation:
sed "s/[[:digit:]]\{4\}/0\/0\/&/" x.csv | sed "s=\([[:digit:]]\)/\([[:digit:]]\(4\)\)=\1/0/\2="

With x.csv looking like this: 1,a,b,1234,1/1/1900, the above produced the following output: 1,a,b,0/0/1234,1/1/1900

Note that the "invalid reference" went away when I stopped using -E.