For each row in a CSV file, I'd like to extract a field and reposition it in the row using Bash. The row is a URL and I am using / as a delimiter.
This is the starting file (start.csv):
https://docs.website.com/12-3/articles/guide-1/article-1.html
https://docs.website.com/12-2/articles/guide-2/article-5.html
https://docs.website.com/12-1/articles/guide-3/article-6.html
For later reference, the URL is https://{url}/{version}/irrelevant/{guide}/irrelevant.html.
The desired output is (end.csv):
url,name,tag,version,guide,views
https://docs.website.com/12-3/articles/guide-1/article-1.html,,,12-3,guide-1,0
https://docs.website.com/12-2/articles/guide-2/article-5.html,,,12-2,guide-2,0
https://docs.website.com/12-1/articles/guide-3/article-6.html,,,12-1,guide-3,0
I've unsuccessfully tried many variations of:
file="start.csv"
var="$(<<<"${file}" cut -d'/' -f4)"
sed -e "s|$|,$var,,,,0|g" < start.csv > end.csv
However this successfully produces a column with the version:
cut -d'/' -f4 < start.csv
Somewhere my logic is seriously flawed. Is anyone able to help me spot my problem? Thank you.
<<<"${file}" cut -d'/' -f4passes the string "start.csv" as input tocut, rather than reading from the file by that name. The serious one is that (if you fix the first one)$varwill contain the version for all lines (separated by newlines), and then using that withsedwill try to attach all of that to each and every line in the file. It doesn't apply line one of$varto line one of the file etc, it applies all lines of$varto line one of the file, then again to line two, etc.