Skip blank lines when iterating through file line by line

MAXGEN

I am iterating through a file line by line and put each word into a array and that works. But it also picks up blank lines and puts it as an item in the array, how can I skip the blank lines?

example file

      Line 1
line 2

line 3
        line 4 

line 5
   line 6

My code

while read line ; do
            myarray[$index]="$line"
            index=$(($index+1))
    done < $inputfile

Possible psuedo code

while read line ; do
           if (line != space);then
            myarray[$index]="$line"
             fi
            index=$(($index+1))
    done < $inputfile
SzG

Remove the blank lines first with sed.

for word in `sed '/^$/d' $inputfile`; do
    myarray[$index]="$word"
    index=$(($index+1))
done

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Missing line content when reading file with bash

来自分类Dev

使用line ... file指令

来自分类Dev

使用line ... file指令

来自分类Dev

Read file line by line and store values

来自分类Dev

Add files to Xcode project through command line ? Use of project.pbxproj file in Xcode?

来自分类Dev

Sort lines in ArrayList by elements inside line

来自分类Dev

Read line by line of a file in shell script where last line doesn't end with new line

来自分类Dev

Cannot read first line of a file

来自分类Dev

Get total number of non-blank lines from text file?

来自分类Dev

UnicodeDecodeError in Python when reading a file, how to ignore the error and jump to the next line?

来自分类Dev

Ruby script to read file line by line and do if statments with puts?

来自分类Dev

replace word in line only if line number start with + csv file

来自分类Dev

How to add line-height based on number of lines of text

来自分类Dev

Update android notification with new line is removing previous lines

来自分类Dev

Plotting fault lines without connecting every line together

来自分类Dev

How to delete all lines after line N that match "ABC"

来自分类Dev

How do I get my command line utility to update one line instead of printing multiple lines?

来自分类Dev

Simplest way to return data from line in file

来自分类Dev

How to find which line is missing in another file

来自分类Dev

How to erase line from text file in Python?

来自分类Dev

qDebug不显示__FILE __,__ LINE__

来自分类Dev

Open a file under cursor with a line break in vim

来自分类Dev

Reading a specific line from CSV file

来自分类Dev

qDebug不显示__FILE __,__ LINE__

来自分类Dev

Print line for word found in file in directory

来自分类Dev

Iterating through list of Dataframes Pandas

来自分类Dev

Iterating through nested hashes in Ruby

来自分类Dev

Visual C++ 2013 - Windows XP Sp1 C Program Not Working - Blank Command Line

来自分类Dev

How to loop text file each line and append other text wrap each line

Related 相关文章

  1. 1

    Missing line content when reading file with bash

  2. 2

    使用line ... file指令

  3. 3

    使用line ... file指令

  4. 4

    Read file line by line and store values

  5. 5

    Add files to Xcode project through command line ? Use of project.pbxproj file in Xcode?

  6. 6

    Sort lines in ArrayList by elements inside line

  7. 7

    Read line by line of a file in shell script where last line doesn't end with new line

  8. 8

    Cannot read first line of a file

  9. 9

    Get total number of non-blank lines from text file?

  10. 10

    UnicodeDecodeError in Python when reading a file, how to ignore the error and jump to the next line?

  11. 11

    Ruby script to read file line by line and do if statments with puts?

  12. 12

    replace word in line only if line number start with + csv file

  13. 13

    How to add line-height based on number of lines of text

  14. 14

    Update android notification with new line is removing previous lines

  15. 15

    Plotting fault lines without connecting every line together

  16. 16

    How to delete all lines after line N that match "ABC"

  17. 17

    How do I get my command line utility to update one line instead of printing multiple lines?

  18. 18

    Simplest way to return data from line in file

  19. 19

    How to find which line is missing in another file

  20. 20

    How to erase line from text file in Python?

  21. 21

    qDebug不显示__FILE __,__ LINE__

  22. 22

    Open a file under cursor with a line break in vim

  23. 23

    Reading a specific line from CSV file

  24. 24

    qDebug不显示__FILE __,__ LINE__

  25. 25

    Print line for word found in file in directory

  26. 26

    Iterating through list of Dataframes Pandas

  27. 27

    Iterating through nested hashes in Ruby

  28. 28

    Visual C++ 2013 - Windows XP Sp1 C Program Not Working - Blank Command Line

  29. 29

    How to loop text file each line and append other text wrap each line

热门标签

归档