I have to create a script who read the NUMBER of lines from any file. I've thought to use the structure do / while. How i can do it?
Asked
Active
Viewed 373 times
-3
-
Did you look similar questions on the topic and make an attempt? – Inian Feb 14 '17 at 08:49
-
yes but i have not found anything. – David Cep Feb 14 '17 at 08:51
-
2also this.. the funny thing is I just took your title and copy-pasted it into google: http://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash – dingalapadum Feb 14 '17 at 08:54
-
this show me the lines... I only want the NUMBER of lines – David Cep Feb 14 '17 at 09:17
-
1`wc -l
` – Biffen Feb 17 '17 at 09:29 -
@DavidCep extrapolate a little - if you know how to read each line, you can surely count how many lines you've read. – dimo414 Feb 17 '17 at 09:43
1 Answers
0
Syntax to read file line by line on a Bash Unix & Linux shell:
The syntax is as follows for bash, ksh, zsh, and all other shells –
1) while read -r line; do COMMAND; done < input.file
2) The -r option passed to read command prevents backslash escapes from being interpreted.
3) Add IFS= option before read command to prevent leading/trailing whitespace from being trimmed - while IFS= read -r line; do COMMAND_on $line; done < input.file
#!/bin/bash
COUNTER=0
input="/path/to/txt/file"
while IFS= read -r var
do
echo "$var"
COUNTER=$[$COUNTER +1]
done < "$input"
echo " $COUNTER "

Hiren Jungi
- 854
- 8
- 20