Q:

Create a new column in CSV based on contents of another

I have a pipe delimited CSV with a URL in the 5th field and I'm looking to use an awk command to copy everything before a question mark character and add a new column with the contents.  The command I'm using currently is: 
cat test.log | awk -F\| -v OFS=\| '{sub(/\?(.*)/,OFS "&", $7); print}' > test2.log

There are a few issues with this:

The regex in the current command is for finding everything AFTER the question mark.  The appropriate regex for finding everything before is /[^?]* however I'm getting a syntax error attempting to use that in the command.  I'm not as worried about this as I can likely sort it out with more testing.
The above command creates a new column with the data matching the regex, however it strips it from the column rather than copying it.

This is an example of what I'm looking for:
||||test.com|moredata|
||||test2.com?p1=1|moredata|
||||test3.com?p1=1&p2=2|moredata|

Becomes:
||||test.com|moredata|test.com
||||test2.com?p1=1|moredata|test2.com
||||test3.com?p1=1&p2=2|moredata|test3.com

Is there a function that behaves similarly without substituting that would be better suited for this (ie - match?)  Thanks in advance!

A:

Use split() to split the URL at the ? character, then you can get the part before this and append it.
awk -F\| -v OFS=\| '{split($5, url, "\?"); $5 = $5 OFS url[1]; print}'

