Results 1 to 6 of 6
Hello. I've just started a training course and the language we are learning is bash, i found this practice exercise in the online course.
I'd appreciate it lots if someone ...
- 03-04-2009 #1Just Joined!
- Join Date
- Mar 2009
- Posts
- 1
Bash Help - CSV Seperation
Hello. I've just started a training course and the language we are learning is bash, i found this practice exercise in the online course.
I'd appreciate it lots if someone could tell me what a working model would look like. So i can look through the code and work it out.
This is the CSV file to be seperated.
ISBN,Title,Author,Category,Cost,No copies
978-0340681138,Five Get into Trouble (Famous Five),Enid Blyton,Childrens,6,3
978-1861058492,His Way: The Brian Clough Story,Patrick Murphy,Sport,6,5
978-0142301883,The Little Match Girl,Hans Christian Anderson,Childrens,3,2
978-1565924260,Learning the Vi Editor,Linda Lamb,Computing,13,1
978-0007231324,Blood Beast,Darren Shan,Horror,7,1
978-0752433660,Nottingham Forest: Champions 1977-1978,John Shipley,Sport,8,10
978-0596100292,Unix in a Nutshell,Arnold Robbins,Computing,17,2
978-0340921531,Cell,Stephen King,Horror,3,5
978-1859835135,The Legends of Nottingham Forest,Dave Bracegirdle,Sport,11,8
978-0596005955,Classic Shell Scripting,Arnold Robbins,Computing,16,4
It asks:
*create files with the names, children_books, horror_books, sports_books and computing_books. These files will have a format like...
ISBN: 978-0340681138
Author: Enid Blyton
Title: Five Get Into Trouble (Famous Five)
*Records which do not belong to any of the above categories should be printed as an error
*Summary report will be displayed on the screen and contain the following information:
-Date
-names of files created (full path)
-total number of books processed
-total value of books
Thanks in advance to any help
- 03-04-2009 #2Linux Newbie
- Join Date
- Feb 2009
- Location
- Third ring of Pergatory
- Posts
- 199
Bash is a kernel interface, commonly referred to as a shell, it's not a programming language. You can write a script that bash will execute, but that script isn't written in bash, it's just shell scripting (still, very powerful stuff in linux).
A CSV is a file containing coma separated values. It's also known as a flat file. CSVs' are good for holding tables used in databases and spreadsheets. The database engine can parse the CSV easily because of it's format.
You want to write a bash script that will parse a flat file and print a report? Are we using a database engine or are we going to do text matching? If you are supposed to use a database engine...which one? You mentioned a web site...do you have a link?
It'll look like this...
spit_it_out(chop_it_up(suck_it_in));
- 03-05-2009 #3Just Joined!
- Join Date
- Feb 2009
- Posts
- 45
<Edit time="2009-03-05T17:01:48.308029890+02:00" reason="correction">
This post is, in retrospect, a flame, nit-picks about a matter of linguistic nature and has nothing to do with the topic at hand.
It should be ignored if youʼre only interested in the topical discussion.
</Edit>
The authors of the bash reference manual take a different stance on that issue, because What is a shell? - Bash Reference Manual says:
Originally Posted by dijetlo
Originally Posted by Bash Reference Manual
There are scores of shell scripting languages. Simply saying «bash» is a shell script interpreter would be correct, but not very precise.
Originally Posted by dijetlo
I assume thatʼs the reasons the Perl folks differentiate between «Perl» (language) and «perl» (interpreter).
- 03-05-2009 #4Linux Newbie
- Join Date
- Feb 2009
- Location
- Third ring of Pergatory
- Posts
- 199
and the guys who wrote grep thinks it's a programming language too, it's just nobody has ever put "program grep" on a resume.The authors of the bash reference manual take a different stance on that issue
You miss the point. Every trade has its own language. People in the trade make judgments about you based on your use of that language. I'm not going to argue whether or not coding and programming should be called programming, I'll just point out that's not the way it's done. Look at the tittle for the thread-"scripting and programming". Use the language as it exists in the industry and people are reassured you are competent, you sling the lingo. If you don't, people wonder if you know what your doing.Simply saying «bash» is a shell script interpreter would be correct, but not very precise.
Since none of us actually know what we're doing, we spend a large portion of our professional lives hiding behind a curtain technoBS, pounding madly on a keyboard, shouting "No problems here, move along". Once you understand that you'll understand why we don't encourage any alternative forms of the voodoo we practice on the PTWTC (people who write the checks). If it got out of hand, we could loose our mojo....so don't tell people you program bash, they wont give you a job. You'll get skinny and cold, homeless people have no place to plug in their laptops and if you think you don't get laid now, wait until your homeless...
I'm just looking out for the boy...
- 03-05-2009 #5
I corrected the category name sports_books to sport_books and the input file from childrens to children
(you can workaround this inconsistence with more code if you insist to have sports_books / sport and children_books / childrens).
Anyway, now that bash4 is out, I think that using a specific bash3 syntax is not so innovative
So you need to run this script with bash >= 3.
I used two external commands:
1. sort for the uniqueness. With zsh you don't need an external command for that, I'm not sure for bash4.
2. cat for the formatting, it could be done in pure shell, but it won't be that readable.
Code:#! /bin/bash _categories=(children_books horror_books sport_books computing_books) _infile='<your_input_file_here>' shopt -s nocaseglob while IFS= read -r; do IFS=, set -- $REPLY (( ++count < 2 )) && continue unset _filename for _c in "${_categories[@]}"; do [[ $_c =~ ${4}_books ]] && _filename="$_c" && break done if [[ $_filename ]]; then printf 'ISBN: %s\nAuthor: %s\nTitle: %s\n\n' \ "$1" "$3" "$2" >> "$_filename" _filenames=("$PWD/$_filename" "${_filenames[@]}") (( co += $6 )) (( val += $5 )) else printf '\n%s - invalid record\n\n' "$REPLY" >&2 fi done < "$_infile" cat <<! $(date) Files created: $( printf '%s\n' "${_filenames[@]}" | sort -u ) Total number of books processed: $co Total value: $val !
- 03-05-2009 #6
The same task written in AWK:
(use nawk or /usr/xpg4/bin/awk on Solaris)
Code:awk -F, 'END { print dt RS print "files created:" for (f in files) print ENVIRON["PWD"] "/" f print RS "number of books:", number_of_books print RS "total value:", total } NR > 1 { if ((tolower($4) "_books") in categories) { printf "ISBN: %s\nAuthor: %s\nTitle: %s\n\n", $1, $3, $2 > (tolower($4) "_books") files[tolower($4) "_books"] number_of_books += $6 total += $5 } else print RS "invalid record:", $0 RS } BEGIN { _ = "children_books,horror_books,sport_books,computing_books" n = split(_, t) while (++i <= n) categories[t[i]] "date" | getline dt close("date") }' infile


Reply With Quote
