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

  • Home
  • SEARCH
  • 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 8338967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:45:16+00:00 2026-06-09T04:45:16+00:00

I’m trying to parse a csv file I made with Google Spreadsheet. It’s very

  • 0

I’m trying to parse a csv file I made with Google Spreadsheet. It’s very simple for testing purposes, and is basically:

1,2
3,4
5,6

The problem is that the csv doesn’t end in a newline character so when I cat the file in BASH, I get

MacBook-Pro:Desktop kkSlider$ cat test.csv 
1,2 
3,4 
5,6MacBook-Pro:Desktop kkSlider$ 

I just want to read line by line in a BASH script using a while loop that every guide suggests, and my script looks like this:

while IFS=',' read -r last first
do
    echo "$last $first"
done < test.csv

The output is:

MacBook-Pro:Desktop kkSlider$ ./test.sh
1 2
3 4

Any ideas on how I could have it read that last line and echo it?

Thanks in advance.

  • 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-09T04:45:18+00:00Added an answer on June 9, 2026 at 4:45 am

    You can force the input to your loop to end with a newline thus:

    #!/bin/bash
    (cat test.csv ; echo) | while IFS=',' read -r last first
    do
        echo "$last $first"
    done
    

    Unfortunately, this may result in an empty line at the end of your output if the input already has a newline at the end. You can fix that with a little addition:

    !/bin/bash
    (cat test.csv ; echo) | while IFS=',' read -r last first
    do
        if [[ $last != "" ]] ; then
            echo "$last $first"
        fi
    done
    

    Another method relies on the fact that the values are being placed into the variables by the read but they’re just not being output because of the while statement:

    #!/bin/bash
    while IFS=',' read -r last first
    do
        echo "$last $first"
    done <test.csv
    if [[ $last != "" ]] ; then
        echo "$last $first"
    fi
    

    That one works without creating another subshell to modify the input to the while statement.


    Of course, I’m assuming here that you want to do more inside the loop that just output the values with a space rather than a comma. If that’s all you wanted to do, there are other tools better suited than a bash read loop, such as:

    tr "," " " <test.csv
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have just tried to save a simple *.rtf file with some websites and
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the
I'm trying to create an if statement in PHP that prevents a single post
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.