shell - read lines from a txt file in bash -
how read lines format inta intb intc intd charp
"chara" optional? there possibility of comment marked # text
i tried this
file = 'test.txt' while ifs=' ' read -r numa numb numc numd charp #do done < file
but don't know whether i'm on right path , charp
sample:
# comment 12345678 24 15 3 p 87654321 11 4 8 43218765 27 10 2 p
you're on right track, there problems code:
- remove spaces around
=
infile =
line - script break otherwise. - your
while
statement missingdo
line (or; do
appendedwhile
line directly). - instead of referring variable
$file
indone
line, passing string literalfile
instead - use"$file"
(the quotes there ensure works filenames have embedded spaces , other chars. interpreted shell).
as ignoring optional character on end of line: adding variable, code (charp
), sufficient - assigned remainder of line, , can ignore it.
if put together, adding code ignoring comment lines, get:
file='test.txt' while ifs=' ' read -r numa numb numc numd charp if [[ $numa != \#* ]]; # ignore comment lines # ... fi done < "$file"
Comments
Post a Comment