Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7909959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:40:15+00:00 2026-06-03T12:40:15+00:00

I have a file that does not have an EOL at the end of

  • 0

I have a file that does not have an EOL at the end of the last line. I noticed this when my bash script that reads from it was not processing the last line.

I read from it like this:

while read LINE
do
    ...
done <thefile

The file is supplied to me, and so there is nothing I can do to it before it lands on my system (Linux – OpenSuSE 10.2). I do run dos2unix on the file, but that does not resolve the missing EOL.

I have seen a couple of solutions involving vi and ed, but they are a bit clunky and I was hoping there is a neater solution, maybe using sed, that I can use from within my bash script?

Oddly, when I vi the file and do a :set list, I can see a “$” at the end of the last line. I was expecting that to be missing as I thought that “$” represented \n. Or maybe there is a difference between newline and end-of-line?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T12:40:21+00:00Added an answer on June 3, 2026 at 12:40 pm

    sed -i -e '$a\' "path" will add a newline at EOF only if it doesn’t already have one (explanation after the jump).

    If you want to process a file which might have no newline at EOF, you have to check whether the read output variable exists after the loop:

    while read LINE
    do
        ...
    done <thefile
    
    if [ "${LINE+defined}" = defined ]
    then
        ...
    fi
    

    Not very elegant, but at least you don’t have to modify the input before processing it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to create a .BAT file that does this: If C:\myprogram\sync\data.handler exists, exit;
I have a fairly simple bash script that does some grep to find all
I have a PHP script that does the following: Uses file_get_contents() to get content
I have a file that contain lines that looks like this: >AF001546_1 [88 -
I have a file that is similar to this: <many lines of stuff> SUMMARY:
I have a file that contain list of numbers that looks like this: 10^-92
I need help creating a self extracting zip file that does not display anything.
I have a tab-delimited file that looks something like this: \>WB02 \t F27C8.1 IV
I have a deploy.launch file that I checked out with a project that does
I have a rewrite in my htaccess file that removes index.php from the url

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.