Reputation: 155
I want to read all file and print Nth letter (just letter) from beginning in all lines.
I read file like this:
while read line
do
echo $line >> text.txt
done < input.txt
Then i do this:
cut -c3 text.txt
But i realize that it doesn't working as i want in these inputs:
He llo
or
re+set
I want to get 'l' for first input and 's' for second input.
But this code found ' ' for first input and '+' for second input
And anubhava wrote this code:
awk -v n=3 '{gsub(/ +/, ""); print substr($0, n, 1)}' file
It was working for Nth letter except space
But now i want print Nth letter (just letter)
What should i do ?
Upvotes: 1
Views: 1300
Reputation: 785058
You can use this awk:
awk -v n=3 '{gsub(/ +/, ""); print substr($0, n, 1)}' file
Testing:
s='re set'
awk -v n=3 '{gsub(/ +/, ""); print substr($0, n, 1)}' <<< "$s"
s
s='He llo'
awk -v n=3 '{gsub(/ +/, ""); print substr($0, n, 1)}' <<< "$s"
l
s='1 2 345'
awk -v n=3 '{gsub(/ +/, ""); print substr($0, n, 1)}' <<< "$s"
3
Upvotes: 1