1/12/2023 0 Comments Modern csv downloadcsvcut -c 3,2,4,5 sample2.csv | csvsort -c 1 -r | csvlookĪ neat touch is that, even though the records are sorted, the header line with the field names is kept as the first line. To make the output prettier we can feed it through csvlook. csvcut -c 3,2,4,5 sample2.csv | csvsort -c 1 -r We’re using the -c (column) option to specify the column to sort by, and the -r (reverse) option to sort in descending order. We can add the csvsort command to sort the output by a field. csvcut -c lastname,firstname,job-title,email-address sample2.csv csvcut -c lastname,firstname,4,5 sample2.csv csvcut -c 3,2,4,5 sample2.csv Suppose we need to extract the first and last names, job titles, and email addresses from each record, but we want to have the name order as “last name, first name.” All we need to do is put the field names or numbers in the order we want them. Record three has no data in the email-address field.Record two has a word wrapped in two sets of quotation marks in the jobs-title field.Record one has a comma in the job-title field, so the field needs to be wrapped in quotation marks.id,firstname,lastname,job-title,email-address,branch,stateģ,Lexi,Roscoe,Pharmacist,Irlington,Vermont To show the problems we can encounter, we modified a small sample of the data to look like this. If the CSV you deal with is uncomplicated without commas or quotation marks in field data, what we’ve covered will probably meet your CSV parsing needs. This is what we get when we run the script./select.shīy adding the cut command, we’re able to select the fields we want and ignore the ones we don’t. Those four fields are read into four variables, which get printed in the body of the while loop. The -f (field) option tells cut we want fields one, four, six, and seven. We’re using the -d (delimiter) option to tell cut to use commas “ ,” as the delimiter. We’ve added the cut command into the process substitution clause. While IFS="," read -r id jobtitle branch stateĭone < <(cut -d "," -f1,4,6,7 sample.csv | tail -n +2) This script is called “select.sh.” #!/bin/bash We can obtain a selection of fields by incorporating the cut command. Perhaps we don’t want or need to retrieve every field. When we run the script, the records are correctly split into their constituent fields, with each field stored in a different variable./field.shĮach record is printed as a set of fields. Substitute the name of the appropriate script in each case. You’ll need to do this each time you copy a script from this article. Make the script executable using the chmod command. This is then redirected into the while loop, providing the text that the read command will parse. It causes Bash to accept the output of a process as though it were coming from a file descriptor. The <(.) construct is called process substitution. The -n +2 (line number) option tells tail to start reading at line number two. We’re using tail because it gives us a simple way to skip over the header line of the CSV file. The data is obtained as the output from the tail command.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |