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 = in file = line - script break otherwise.
  • your while statement missing do line (or ; do appended while line directly).
  • instead of referring variable $file in done line, passing string literal file 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

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -