Reputation: 627
I was solving one problem from one site, question is as follows:
"Given N lines of input in a file, print the 3rd character from each line as a new line of output. It is guaranteed that each of the n lines of input will have a 3rd character."
So to solve the problem I have written a command
while read -r line
do
echo ${line:2:1}
done < sample.txt
Content in "sample.txt" is :
C.B - Cantonment Board/Cantonment
C.M.C – City Municipal Council
C.T – Census Town
E.O – Estate Office
G.P - Gram Panchayat
I.N.A – Industrial Notified Area
I.T.S - Industrial Township
M – Municipality
M.B – Municipal Board
M.C – Municipal Committee
I am getting output as follows:
B
M
T
O
P
N
T
â
B
C
According to the site, the answer should be:
B
M
T
O
P
N
T
в
B
C
Please not that 3rd last output is "в" and I am getting "â" I am new to the ascii, uts-8 conversion so not aware if this has any relation .
What should be change in the code to bring this answer?
Upvotes: 1
Views: 4484
Reputation: 18381
something with awk
: Columns are changed to characters using FS=""
awk -vFS="" '{print $3}' file
Upvotes: 2
Reputation: 311428
This can be done with a straightforward cut
call:
$ cut -c3 sample.txt
Upvotes: 4