I must say you're doing quite well. I'm impressed.
"
;" is called a semicolon.
To target a specific line (or more accurately a record) in awk, test the "NR" variable.
Also, I wouldn't use that complex field delimiter. Just set it to a single semicolon, and use the
gsub function to remove the quotes from the fields you want. It's safer than using a regex that could mismatch data if a field is misquoted.
Code:
awk -F ";" 'NR==1 { gsub(/\"/,"",$1) ; gsub(/\"/,"",$2) ; print $1"|"$2 }' Musica.csv
The gsub regex above simply removes all (") quote-marks from the field. We'll have to alter that if you need something more complex.
You can do pretty much the same thing without awk by using arrays. Use the
IFS environment variable to set the separator. A simple loop over every line in the file would look like this:
Code:
file="Musica.csv"
IFS=";"
while read -a fields ; do
echo "${fields[0]//\"}|${fields[1]//\"}" #parameter substitution removes
#quote-marks when printing.
done <"$file"
Here
read with the
-a option sets the array
fields to the words of the input line (with "word" breaks being defined by the current IFS setting).
If you want to be able to access specific lines, you can store the lines in another array. Bash v.4 even has a new
mapfile command to set an array with lines from a file. Or else use IFS again, this time set to newline, and set the array in a more traditional way.
Code:
file="Musica.csv"
#bash version 4
mapfile -t musiclines <"$file"
#bash version 3
IFS=$'\n'
musiclines=( $(<"$file") )
IFS=";"
read -a fields <<<"${musiclines[0]}" #reads the first line from the array.
#uses a "here string" for input.
echo "${fields[0]//\"}|${fields[1]//\"}"