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 8385001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:33:25+00:00 2026-06-09T17:33:25+00:00

I am trying to write a bash script that will take a text file

  • 0

I am trying to write a bash script that will take a text file and use its content to populate a MySQL table.

The text file contains numerous entries that I will need to add to the table.

Example of text file contents:

Firstname=bob
Surname=ross
Age=9
Firstname=gary
Surname=graeme
Age=19
Firstname=henry
Surname=harry
Age=23

The text file is structured like the example above, the same 3 variables are constantly assigned new values for each unique entry that would go into the mysql table.

I have used sed to remove all empty lines and lines beginning with “#” in the text file that I would not want to process.

My question is how do I use bash to make an sql insert every three lines using the contents in textfile to generate the mysql query. I know I need to loop through the file 3 lines at a time until the end of the text file is reached and somehow parse each value after “=”
and assign it to a variable in the loop iteration to insert the mysql data but I am not sure how to do this.

I know that I will need to have a new table entry every 3 lines.

echo "INSERT INTO $table (Firstname, Surname, Age) VALUES ('$Firstname', '$Surname', '$age');" | mysql $db

Thanks

  • 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-09T17:33:27+00:00Added an answer on June 9, 2026 at 5:33 pm

    I split each line in the input on = and use the second part.
    Then I pipe this to the while-loop where I use read fname sname age to populate the variables inside the loop. I use NR % 3==0 {print("")} to introduce a linebreak in the output every three lines.

    awk -F"=" '{printf("%s ", $2)}; NR % 3==0 {print("")}' input_file | \
    while read fname sname age; do
        echo "INSERT INTO $table (Firstname, Surname, Age)
          VALUES ('$fname', '$sname', '$age');" | mysql $db
    done
    

    UPDATED:
    input:

    Firstname=bob
    Surname=ross
    Age=9
    Activity=1 2 3 4 5
    Firstname=gary
    Surname=graeme
    Age=19
    Activity=1 2 3 4 5
    Firstname=henry
    Surname=harry
    Age=23
    Activity=1 2 3 4 5
    

    with this code:

    awk -F"=" '{printf("%s ", $2)}; NR % 4==0 {print("")}' input | \
    while read fname sname age act; do
        echo "'$fname', '$sname', '$age', '$act');" 
    done
    

    gives:

    'bob', 'ross', '9', '1 2 3 4 5');
    'gary', 'graeme', '19', '1 2 3 4 5');
    'henry', 'harry', '23', '1 2 3 4 5');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write my first semi advanced bash script that will take input
I'm trying to write a bash script that will process a list of files
I am trying to write a bash or PHP script that will execute a
I'm trying to write a bash script that will download all of the youtube
I'm trying to write a bash script that will let me download multiple web
I am trying to write a simple bash script that will search for files
I'm trying to write a Bash script that uploads a file to a server.
I'm trying to write a bash script that takes a file name, and return
I'm trying to write a Bash script that will SSH into a machine and
I am trying to write a simple bash script that will copy the entire

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.