Unix interview Questions and answers: reading from a file
Unix for software developers
Q. If you have a shell script file or data file created in DOS, how will you convert it to Unix (especially, end of file and end of line characters)?
A. Using the dos2unix command.
dos2unix ReadWriteFile.sh ReadWriteFile.sh
Q. How do you remove the Control-M characters from a file?
A. Using the sed command that replaces Control-M with nothing
sed 's/^M//g' ReadWriteFile.sh > ReadWriteFileNew.sh
Note: The ^M is typed on the command line with ctrl+v and ctrl+M
Q. How will you go about reading a CSV file as shown below?
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
A.
#!/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
Q. How will you read and then write to another file?
A.
#!/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
Q. How will you empty or clear the contents of a file?
A.
cat /dev/null > /drives/c/jpm_out/simple-out.csv
Q. How will you read the file and then write to another file with CSV data in different order?
Account,Portfolio,PositionIndicator,Amount CURR-AUD,AA123,CREDIT,2244.14000000 CURR-AUD,AA123,CREDIT,5.60000000 CURR-AUD,AA123,DEBIT,2249.74000000 CURR-GBP,AA123,CREDIT,0.01000000
A. Here is the revised shell script
#!/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 export IFS="," # read till the end of the file until [ $done ] do # read 4 columns into a,b,c, and d read <&3 a b c d #$? 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 $b,$a,$c,$d >> $2 done
Labels: UNIX
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home