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

Related Questions

Following on from a previous question, I'm trying to clean up some data where
I was trying to clean up some accessability stuff in my code, and inadvertently
I am trying to clean up some email addresses in sql server. I can
I have some data similar to the following chart: http://developer.yahoo.com/yui/examples/charts/charts-seriescustomization_clean.html Only difference is that
So I've been trying to design a clean way of grabbing data for my
I'm trying to create clean URLs on my website. Now I succeeded in configuring
I'm trying to come up with a clean way of sorting a set of
I am trying to figure out a clean way to intercept uncaught exceptions that
Trying to setup an SSH server on Windows Server 2003. What are some good
Trying to get my css / C# functions to look like this: body {

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.