Skip to main content
5 of 5
added 182 characters in body
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60

From your sample input it looks like your field separator is actually ,, not ¬, and those ¬ characters are being used the way that double quotes (") are used in a standards-compliant CSV (see whats-the-most-robust-way-to-efficiently-parse-csv-using-awk).

Assuming that is the case, this will do what you want in any POSIX awk:

$ cat tst.sh
#!/usr/bin/env bash

read -p "Which format? " format

awk -F'¬,¬' '
    NR > 1 {
        gsub(/^¬|¬$/,"")
        print '"$format"'
    }
' "${@:--}"

and adjusting the field numbers to treat the input as a CSV:

$ ./tst.sh Model.csv
Which format? "["$2"]" " "$3" - "$4
[1999] Utada Hikaru - First Love
[1999] Lee Jung Hyun - Wa

I'm assuming that ¬ and newline cannot appear within fields of your CSV.

Warning: Be aware that the format shell variable is expanding to become part of the body of the awk script before awk sees it so you'll have to be VERY careful with who you allow to use the script and ensure they fully understand awk syntax as, in addition to potentially cryptic errors:

$ ./tst.sh Mode1.csv
Which format? $#
awk: cmd. line:5:       print $#
awk: cmd. line:5:              ^ syntax error

$ ./tst.sh Mode1.csv
Which format? $1 / $4
awk: cmd. line:4: (FILENAME=Mode1.csv FNR=1) fatal: division by zero attempted

it's inviting a code injection vulnerability:

$ ./tst.sh Mode1.csv
Which format? system("echo hello")
hello
0

Personally, I would not do this at all but instead figure out some other interface to let users select fields to print and format to print them in.

Ed Morton
  • 35.9k
  • 6
  • 25
  • 60