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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:05:54+00:00 2026-06-04T23:05:54+00:00

I would like to convert data.txt into scheme list using sed into the following

  • 0

I would like to convert data.txt into scheme list using sed into the following format:

-Each every line with same starting number will be parsed and combined like so:

data.txt

1,{},344.233
1,{2},344.197
2,{16},290.281
2,{18},289.093
3,{1},220.896

foo.scm

(define v1 '(1 (() 344.233) ((2) 344.197))) ;; this is for first two lines starting with 1
(define v2 '(2 ((16) 290.281) ((18) 289.093))) ;; ... 2
(define v3 '(3 (() 237.558))) ;; ... 3
  • 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-04T23:05:55+00:00Added an answer on June 4, 2026 at 11:05 pm

    I know nothing about scheme, so I’d probably do this in awk rather than sed.

    [ghoti@pc ~]$ cat data.txt 
    1,{},344.233
    1,{2},344.197
    2,{16},290.281
    2,{18},289.093
    3,{1},220.896
    [ghoti@pc ~]$ cat doit.awk 
    #!/usr/bin/awk -f
    
    BEGIN {
      FS=",";
      last1=1;
    }
    
    $1 != last1 {
      printf("(define v%.0f '(%.0f %s))\n", last1, last1, substr(sect,2));
      last1=$1; sect="";
    }
    
    {
      gsub(/[^0-9]/,"",$2);
      sect=sprintf("%s ((%s) %s)", sect, $2, $3);
    }
    
    END {
      printf("(define v%.0f '(%.0f %s))\n", last1, last1, substr(sect,2));
    }
    
    [ghoti@pc ~]$ ./doit.awk data.txt 
    (define v1 '(1 (() 344.233) ((2) 344.197)))
    (define v2 '(2 ((16) 290.281) ((18) 289.093)))
    (define v3 '(3 ((1) 220.896)))
    [ghoti@pc ~]$ 
    

    It could certainly be written more tightly, but this gets the job done.

    UPDATE: (per comments)

    [ghoti@pc ~]$ tail -1 data.txt 
    3,{1,3,4},220.896
    [ghoti@pc ~]$ diff -u doit.awk doitnew.awk 
    --- doit.awk    2012-05-30 00:38:34.549680376 -0400
    +++ doitnew.awk 2012-05-30 00:38:52.893810815 -0400
    @@ -10,8 +10,15 @@
       last1=$1; sect="";
     }
    
    +$2 !~ /}$/ {
    +  while ($2 !~ /}$/) {
    +    pos=match($0, /,[0-9,]+}/);
    +    $0=substr($0, 0, pos-1) " " substr($0, pos+1);
    +  }
    +}
    +
     {
    -  gsub(/[^0-9]/,"",$2);
    +  gsub(/[^0-9 ]/,"",$2);
       sect=sprintf("%s ((%s) %s)", sect, $2, $3);
     }
    
    [ghoti@pc ~]$ ./doitnew.awk data.txt 
    (define v1 '(1 (() 344.233) ((2) 344.197)))
    (define v2 '(2 ((16) 290.281) ((18) 289.093)))
    (define v3 '(3 ((1 3 4) 220.896)))
    [ghoti@pc ~]$ 
    

    What’s going on here?

    In the new block we’re adding, test to see whether the second field ends in a }. If it doesn’t, we’ll loop until it does. For each run of the loop, we’ll remove a comma before the }, replacing it with a space.

    Sometimes, brute-force works. 😛

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

Sidebar

Related Questions

i would like to convert the data.txt file into the following output using awk
I'm querying data using wikipedia api and would like to convert the result into
I have a list of key/value pairs and would like to convert it into
I have a List<class> that I would like to convert into a json object
I would like to convert some value from a XML attribute into valid HTML
I would like to convert flat form data to recursive JSON data in python
I would like to find a fast way to convert a Data Contract to
I would like to convert an Oracle SQL query into SQL server query. But
Compiling on linux using gcc. I would like to convert this to hex. 10
I would like to convert a vector into a transitions matrix first (which I

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.