0

I trying to modify one column in csv file Csv file looks like

"Name", "Gender", "Link"
"Josh", "Male", "https://url.com/josh"
"Samanta", "Female", "https://url.com/samanta"

Main task: add Html tag to all links.

Example of how csv should look:

"Name", "Gender", "Link"
"Josh", "Male", "<a href="https://url.com/josh"/a>"
"Samanta", "Female", "<a href="https://url.com/samanta"/a>"

I do not completely understand how it works in awk or sed

Thanks in advance

Mykola
  • 9
  • 4

1 Answers1

0

Using awk, here's a solution:

cat text.csv | awk 'NR>1 && NF{ OFS=","; print $1 $2 "<a href=\"" $3 "\">" substr($1, 1, length($1)-1) "<a/>" }'

NR>1 - true for line numbers greater than 1.

NF - number of fields, 0/false for empty lines.

OFS="," - output field separator, splits on comma.

print - you can intersperse $1 variables with "quoted strings"

substr() - as I used <a>$1</a> so the links have a name, I trimmed the trailing comma that awk keeps in the $1 variable.

Some other similar stackoverflow links on awk & csv, with suggestions like cut: extract-specific-columns-from-delimited-file-using-awk, how-to-print-a-range-of-columns-in-a-csv-in-awk, how-to-extract-one-column-of-a-csv-file

camsnz
  • 93
  • 4