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

Joshi

I have a text file which contains the following lines:

7.3.0
12

I am using the below lines to read the content line by line

while read -r LINE; do
    if test $n -eq 1 
    then 
       var01=$LINE
    fi
    if test $n -eq 2 
    then 
       var02=$LINE
    fi
    n=`expr $n + 1`
done < <(tr -d '\r' < /mnt/Share/hpsum_build.txt)

the above code doesn't read 2nd line that is last line '12' which doesn't end with a new line. How to read the last line as well

anubhava

If your text line doesn't have an ending newline then you can do this at start of your script to force a newline in it:

echo '' >> /mnt/Share/hpsum_build.txt

You can also call dos2unix utility to remove DOS's \r before \n from each line.

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

Read file line by line and store values

来自分类Dev

How to read a text file, then capture and read new added line

来自分类Dev

Cannot read first line of a file

来自分类Dev

Power Line Split for New Line的问题

来自分类Dev

使用line ... file指令

来自分类Dev

使用line ... file指令

来自分类Dev

EOF reading last line twice

来自分类Dev

Vim move / copy line to end of file without moving cursor?

来自分类Dev

Replace string with a new line in Batch

来自分类Dev

Xamarin forms Label new line

来自分类Dev

XML - Create element - New line

来自分类Dev

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

来自分类Dev

Skip blank lines when iterating through file line by line

来自分类Dev

Append to end of each line with sed on Windows?

来自分类Dev

ChartJS、Primeng、Gap first and end of line chart

来自分类Dev

如何用`read_line()`检查EOF?

来自分类Dev

while read line find – 用perl更快?

来自分类Dev

Trimming new line character from a string in java

来自分类Dev

CSS :after element being pushed to new line

来自分类Dev

为Elixir File.stream指定替代的line_end字符!功能

来自分类Dev

在Pine Script TradingView中使用if语句进行line.new时的值不适用

来自分类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

Missing line content when reading file with bash

来自分类Dev

Open a file under cursor with a line break in vim

来自分类Dev

Reading a specific line from CSV file

Related 相关文章

热门标签

归档