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

WARNING: DO NOT DO THIS!*

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 Mode1Model.csv
Which format? $1""["$2"]" -" "$2
Japan"$3" - 1999

$ ./tst.sh Mode1.csv"$4
Which[1999] format?Utada $2"Hikaru - "$4First Love
1999[1999] -Lee FirstJung LoveHyun - 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.

WARNING: DO NOT DO THIS!*

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'¬,¬' '
    {
        gsub(/^¬|¬$/,"")
        print '"$format"'
    }
' "${@:--}"

 

$ ./tst.sh Mode1.csv
Which format? $1" - "$2
Japan - 1999

$ ./tst.sh Mode1.csv
Which format? $2" - "$4
1999 - First Love

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.

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.

added 182 characters in body
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60

WARNING: DO NOT DO THIS!*

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 more traditionalstandards-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'¬,¬' '
    {
        gsub(/^¬|¬$/,"")
        print '"$format"'
    }
' "${@:--}"

$ ./tst.sh Mode1.csv
Which format? $1" - "$2
Japan - 1999

$ ./tst.sh Mode1.csv
Which format? $2" - "$4
1999 - First Love

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.

WARNING: DO NOT DO THIS!*

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 more traditional CSV. 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'¬,¬' '
    {
        gsub(/^¬|¬$/,"")
        print '"$format"'
    }
' "${@:--}"

$ ./tst.sh Mode1.csv
Which format? $1" - "$2
Japan - 1999

$ ./tst.sh Mode1.csv
Which format? $2" - "$4
1999 - First Love

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.

WARNING: DO NOT DO THIS!*

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'¬,¬' '
    {
        gsub(/^¬|¬$/,"")
        print '"$format"'
    }
' "${@:--}"

$ ./tst.sh Mode1.csv
Which format? $1" - "$2
Japan - 1999

$ ./tst.sh Mode1.csv
Which format? $2" - "$4
1999 - First Love

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.

added 325 characters in body
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60

WARNING: DO NOT DO THIS!*

From your sample input it looks like your field separator is actually ,, not ¬, and those ¬ characters are more likebeing used the way that double quotes (") we'd expect to seeare used in a more traditional CSV. Assuming that is the case, this will do what you asked forwant in any POSIX awk:

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

read -p "Which format? " format

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

$ ./tst.sh Mode1.csv
Which format? $1" - "$2
Japan - 1999

$ ./tst.sh Mode1.csv
Which format? $2" - "$4
1999 - First Love

but beI'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.

From your sample input it looks like your field separator is actually ,, not ¬ and those ¬ characters are more like the double quotes (") we'd expect to see in a more traditional CSV. Assuming that is the case, this will do what you asked for in any POSIX awk:

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

read -p "Which format? " format

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

$ ./tst.sh Mode1.csv
Which format? $1" - "$2
Japan - 1999

$ ./tst.sh Mode1.csv
Which format? $2" - "$4
1999 - First Love

but 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.

WARNING: DO NOT DO THIS!*

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 more traditional CSV. 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'¬,¬' '
    {
        gsub(/^¬|¬$/,"")
        print '"$format"'
    }
' "${@:--}"

$ ./tst.sh Mode1.csv
Which format? $1" - "$2
Japan - 1999

$ ./tst.sh Mode1.csv
Which format? $2" - "$4
1999 - First Love

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.

added 325 characters in body
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60
Loading
Source Link
Ed Morton
  • 35.9k
  • 6
  • 25
  • 60
Loading