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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:29:58+00:00 2026-05-11T08:29:58+00:00

I am trying to clean up some data, and I would eventually like to

  • 0

I am trying to clean up some data, and I would eventually like to put it in CSV form.

I have used some regular expressions to clean it up, but I’m stuck on one step.

I would like to replace all but every third newline (\n) with a comma.

The data looks like this:

field1 field2 field3 field1 field2 field3 

etc..

I need it in

field1,field2,field3 field1,field2,field3 

Anyone have a simple way to do this using sed or awk? I could write a program and use a loop with a mod counter to erase every 1st and 2nd newline char, but I’d rather do it from the command line if possible.

  • 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. 2026-05-11T08:29:58+00:00Added an answer on May 11, 2026 at 8:29 am

    With awk:

    awk '{n2=n1;n1=n;n=$0;if(NR%3==0){printf'%s,%s,%s\n',n2,n1,n}}' yourData.txt 

    This script saves the last three lines and print them at every third line. Unfortunately, this works only with files having a multiple of 3 lines.

    A more general script is:

    awk '{l=l$0;if(NR%3==0){print l;l=''}else{l=l','}}END{if(l!=''){print substr(l,1,length(l)-1)}}' yourData.txt 

    In this case, the last three lines are concatenated in a single string, with the comma separator inserted whenever the line number is not a multiple of 3. At the end of the file, the string is printed if it is not empty with the trailing comma removed.

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Dont use a foreach then. Use a 'for loop'. Your… May 11, 2026 at 11:57 pm
  • Editorial Team
    Editorial Team added an answer Add a System.Windows.Forms.ContextMenuStrip item to the form, then set the… May 11, 2026 at 11:57 pm
  • Editorial Team
    Editorial Team added an answer You can add a class to all the links that… May 11, 2026 at 11:57 pm

Related Questions

I've been trying to implement unit testing and currently have some code that does
I've got a layout bug that shows up in IE6, and I'm trying to
I am just starting on ASP.NET MVC trying to understand the philosophy first. I
I am having a hard time figuring out why when the cfquery runs in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.