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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:39:04+00:00 2026-06-18T11:39:04+00:00

I have a file like: something1 something2 201101130000 thing thing1 thing2 AAA, -2, 4,

  • 0

I have a file like:

something1

something2 201101130000

thing

thing1

thing2

AAA, -2, 4, 0, 54;

thing3

thing4

AAA, 43, 43, 0, 5, 0, 0,;

thing5

AAA, 132.0, 43.0,  0.0,  0.0, 43.0,210.0,'

thing5

How to copy the date (201101130000) from the second line, add a comma (,) then put the numbers of the line before last (132,0, 43.0, 0.0, 43.0, 210.0) in newfile.txt
the new file should look like:(the original file does not have spaces between lines as it is here)

20110113, 132.0, 43.0,  0.0,  0.0, 43.0,210.0

I tried grep and sed with no luck. Thanks for your help

  • 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-18T11:39:06+00:00Added an answer on June 18, 2026 at 11:39 am

    Here’s how I’ve interpreted your question:

    • You’re trying to ‘grep’ and join parts of two lines. These two lines are always the second and second last lines.

    • You’re also trying to redirect output to another file. You can use shell redirection for this, like: awk ... file > outputfile.



    Here’s one way using sed:

    sed '2h; $!N; $!D; ${ G; s/[^,]*\([^\n]*\).* \([0-9]\{8\}\).*/\2\1/; s/..$// }' file
    

    Since you’ve tagged this as linux, I’m guessing you’ve got GNU sed and don’t mind golf:

    sed -r '2h;$!N;$!D;${G;s/[^,]*([^\n]*).*\s([0-9]{8}).*/\2\1/;s/..$//}' file
    

    Results:

    20110113, 132.0, 43.0,  0.0,  0.0, 43.0,210.0
    

    Explanation:

    2h         # copy the second line to hold space
    $!N        # if not the last line append the next line
    $!D        # if not the last line delete up to the first newline in the pattern
    $ { ... }  # one the last line, perform two substitutions   
    


    Alternatively, awk may be easier to understand:

    awk 'FNR==NR { c++; next } FNR==2 { x = substr($NF,0,8) } FNR==c-1 { sub(/[^,]*/,x); sub(/..$/,""); print }' file file
    

    Results:

    20110113, 132.0, 43.0,  0.0,  0.0, 43.0,210.0
    

    Explanation:

    FNR==NR { c++; next }    # read the first file in the arguments list, to get a 
                             # count of the number of lines in the file
    FNR==2 { ... }           # when reading the second line of the second file in the
                             # arguments list, take a substring of the last field
    FNR==c-1 { ... }         # one the second last line of the second file in the
                             # arguments list, perform two substitutions and print
                             # the line.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file that defines constant variables, like this: define_vars.php <? define(something,value); define(something1,value);
I have a sample position file like below. 789754654 COLORA SOMETHING1 19370119FYY076 2342423234SS323423 742784897
I have a text file something like: lineat2 corrupt exitCode() and so on... Each
I have text file with something like first line line nr 2 line three
This is a longshot but I have a file with something like this: document
A really simple shell script question. I have a file with something like this:
I have a PHP File with something like this: <script type=text/javascript> var page =
I have a file that looks something like this: ABC DEF GHI I have
If I have a xml file with something like this: <?xml version=1.0 encoding=utf-8 ?>
I have a ID.txt file that looks like this: http://something.net/something-ak/41389_718783565_1214898307_q.jpg http://something.net/something-ak/372142_106502518141813_1189943482_q.jpg and so on

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.