Q1 How will you go about reading a CSV file as shown below?
1 2 3 4 5 6 |
Portfolio,Account,PositionIndicator,Amount AA123,CURR-AUD,CREDIT,2244.14000000 AA123,CURR-AUD,CREDIT,5.60000000 AA123,CURR-AUD,DEBIT,2249.74000000 AA123,CURR-GBP,CREDIT,0.01000000 AA123,CURR-GBP,DEBIT,0.01000000 |
A1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#!/bin/bash # $# means number of arguments supplied to the command. In this case the file to to be read if [ $# != 1 ]; then echo "Usage: $0 input-file" exit 1 else infile=$1 fi # assign file descriptor 3 to input file exec 3< $infile # read till the end of the file until [ $done ] do read <&3 myline #$? is the exit code, and 0 means success and != 0 means not success. That is no line is read. if [ $? != 0 ]; then done=1 continue fi # process file data line-by-line in here echo $myline done |
Q2 How will you read and then write to another file?
1 2 3 4 5 6 |
Portfolio,Account,PositionIndicator,Amount AA123,CURR-AUD,CREDIT,2244.14000000 AA123,CURR-AUD,CREDIT,5.60000000 AA123,CURR-AUD,DEBIT,2249.74000000 AA123,CURR-GBP,CREDIT,0.01000000 AA123,CURR-GBP,DEBIT,0.01000000 |
A2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#!/bin/bash # $# means number of arguments supplied to the command if [ $# != 2 ]; then # $0 is the script name $1 is input file and $2 is output file echo "Usage: $0 input-file output-file" exit 1 else infile=$1 fi # assign file descriptor 3 to input file exec 3< $infile # read till the end of the file until [ $done ] do read <&3 myline #$? is the exit code, and 0 means success and != 0 means not success. That is no line is read. if [ $? != 0 ]; then done=1 continue fi # process file data line-by-line # append to output file. If you run it more than once, it keeps appending. echo $myline >> $2 done |
Q3 How will you read the file and then write…