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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:30:48+00:00 2026-05-16T17:30:48+00:00

I have a caret delimited (key=value) input and would like to extract multiple tokens

  • 0

I have a caret delimited (key=value) input and would like to extract multiple tokens of interest from it.

For example: Given the following input

$ echo -e "1=A00^35=D^150=1^33=1\n1=B000^35=D^150=2^33=2"
1=A00^35=D^22=101^150=1^33=1
1=B000^35=D^22=101^150=2^33=2    

I would like the following output

35=D^150=1^
35=D^150=2^

I have tried the following

$ echo -e "1=A00^35=D^150=1^33=1\n1=B000^35=D^150=2^33=2"|egrep -o "35=[^/^]*\^|150=[^/^]*\^"
35=D^
150=1^
35=D^
150=2^

My problem is that egrep returns each match on a separate line. Is it possible to get one line of output for one line of input? Please note that due to the constraints of the larger script, I cannot simply do a blind replace of all the \n characters in the output.

Thank you for any suggestions.This script is for bash 3.2.25. Any egrep alternatives are welcome. Please note that the tokens of interest (35 and 150) may change and I am already generating the egrep pattern in the script. Hence a one liner (if possible) would be great

  • 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-05-16T17:30:48+00:00Added an answer on May 16, 2026 at 5:30 pm

    To get rid of the newline, you can just echo it again:

    $ echo $(echo "1=A00^35=D^150=1^33=1"|egrep -o "35=[^/^]*\^|150=[^/^]*\^")
    35=D^ 150=1^
    

    If that’s not satisfactory (I think it may give you one line for the whole input file), you can use awk:

    pax> echo '
    1=A00^35=D^150=1^33=1
    1=a00^35=d^157=11^33=11
    ' | awk -vLIST=35,150 -F^ ' {
            sep = "";
            split (LIST, srch, ",");
            for (i = 1; i <= NF; i++) {
                for (idx in srch) {
                    split ($i, arr, "=");
                    if (arr[1] == srch[idx]) {
                        printf sep "" arr[1] "=" arr[2];
                        sep = "^";
                    }
                }
            }
            if (sep != "") {
                print sep;
            }
        }'
    35=D^150=1^
    35=d^
    

     

    pax> echo '
    1=A00^35=D^150=1^33=1
    1=a00^35=d^157=11^33=11
    ' | awk -vLIST=1,33 -F^ ' {
            sep = "";
            split (LIST, srch, ",");
            for (i = 1; i <= NF; i++) {
                for (idx in srch) {
                    split ($i, arr, "=");
                    if (arr[1] == srch[idx]) {
                        printf sep "" arr[1] "=" arr[2];
                        sep = "^";
                    }
                }
            }
            if (sep != "") {
                print sep;
            }
        }'
    1=A00^33=1^
    1=a00^33=11^
    

    This one allows you to use a single awk script and all you need to do is to provide a comma-separated list of keys to print out.


    And here’s the one-liner version 🙂

    echo '1=A00^35=D^150=1^33=1
          1=a00^35=d^157=11^33=11
          ' | awk -vLST=1,33 -F^ '{s="";split(LST,k,",");for(i=1;i<=NF;i++){for(j in k){split($i,arr,"=");if(arr[1]==k[j]){printf s""arr[1]"="arr[2];s="^";}}}if(s!=""){print s;}}'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I get the caret position from within an input field? I have
I have a richtextbox and inside I have a listbox. I would like the
I have a tab-delimited list of pairs like this: apple yellow orange green apple
Suppose I would like to match for the following pipe-delimited strings: std::string re(a|*|c); std::string
I want to remove the caret from a TEdit control in Delphi. I have
I want a non-editable TextField (or a subclass) that doesn't even have the caret
I have a Perl program that I've written that parses SQL-like statements and creates
I have a contenteditable div where I need to insert text at the caret
Basically I have a series of lines in a textbox like this: ### =
I have the unfortunate task of having to import data from excel into a

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.