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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:59:23+00:00 2026-06-13T15:59:23+00:00

I am trying to use awk to parse a multiline expression. A single one

  • 0

I am trying to use awk to parse a multiline expression. A single one of them looks like this:

_begin  hello world !
_attrib0    123
_attrib1    super duper
_attrib1    yet another value
_attrib2    foo
_end

I need to extract the value associated to _begin and _attrib1. So in the example, the awk script should return (one per line):

hello world ! super duper yet another value 

The separator used is a tab (\t) character. Spaces are used only within strings.

  • 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-13T15:59:24+00:00Added an answer on June 13, 2026 at 3:59 pm

    The following awk script does the job:

    #!/usr/bin/awk -f
    BEGIN { FS="\t"; }
    /^_begin/      { output=$2; }
    $1=="_attrib1" { output=output " " $2; }
    /^_end/        { print output; }
    

    You didn’t specify whether you want a tab (\t) to be your output field separator. If you do, let me know and I’ll update the answer. (Or you can; it’s trivial.)

    Of course, if you want a scary alternative (since we’re getting close to Hallowe’en), here a solution using sed:

    $ sed -ne '/^_begin./{s///;h;};/^_attrib1[^0-9]/{s///;H;x;s/\n/ /;x;};/^_end/{;g;p;}' input.txt 
    hello world ! super duper yet another value
    

    How does this work? Mwaahahaa, I’m glad you asked.

    • /^_begin./{s///;h;}; — When we see _begin, strip it off and store the rest of the line to sed’s “hold buffer”.
    • /^_attrib1[^0-9]/{s///;H;x;s/\n/ /;x;}; — When we see _attrib1, strip it off, append it to the hold buffer, swap the hold buffer and pattern space, replace newlines with spaces, and swap the hold buffer and pattern space back again.
    • /^_end/{;g;p;} — We’ve reached the end, so pull the hold buffer into the pattern space and print it.

    This assumes that your input field separator is just a single tab.

    SO simple. Who ever said sed was arcane?!

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

Sidebar

Related Questions

I have output with this format: /ignore-this/^/../I/want/this@ignore-this I am trying to use an awk
I'm trying to edit a text file that looks like this: TYPE=Ethernet HWADDR=00:.... IPV6INIT=no
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
I am need paint my image. I'm trying use JQuery in here this link:
I'm trying to parse file2 for record 23 and use it to replace file1's
I'm trying to learn to use awk but it's not behaving how I expect.
I'm stil pretty new to regular expression and just started learning to use awk.
I have a messy html that looks like this: <div id=:0.page.0 class=page-element style=width: 1620px;>
I'm trying to use the AWK in a unix shell script to substitue an
I have a regular expression for use with awk to find any of the

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.