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 8296657

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:06:31+00:00 2026-06-08T15:06:31+00:00

I’m searching through input to pull out specific info about each record. The sad

  • 0

I’m searching through input to pull out specific info about each record. The sad thing is that each record is spread out over multiple lines, e.g. (simplified excerpt)

01238584 (other info) more info, more info
[age=81][otherinfo][etc, etc]

The only thing I really care about is the identifier and the age (01238584 and 81, in the example). To be crystal-clear, the only regex I can reliably search for in the input to get close to these two lines is

\[age=[0-9]+\]

… and of course I want to print out that age along with the identifying record number from the line above it, e.g.

 01238584   81

With all my sysadmin shell experience and decent awk mastery, I haven’t come up with a solution yet. I can of course use grep -B1 to get each set of lines, but then what? I always use awk for these kinds of things… but associated data is always in the same line. sigh This is definitely beyond my current awk skills.

Thanks for reading. Got any pointers?


EDIT
I’m going with Charlie’s suggestion and changing awk’s record separator, which I had never done before. It’s not pretty, but neither is the input. Job is done.

egrep -B1 '\[age=[0-9]+\]' inputfile |
awk '
  BEGIN{ RS = "--" }
  { printf "%s  %s\n", $1, gensub(/.*\[age=([0-9]+)\].*/, "\\1", 1) }'
  • 0 0 Answers
  • 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-08T15:06:35+00:00Added an answer on June 8, 2026 at 3:06 pm

    Can you show more of the input file? For example, if the data records are separated by blank lines, you can change the record separator using the RS special variable in Awk to have it treat multiple lines as one record. (See, eg, http://www.staff.science.uu.nl/~oostr102/docs/nawk/nawk_19.html)

    In any case, I’d be tempted to do something that puts all your data records onto one line or in one logical record.

    If you can’t do that but you know the record ID is always on the line before the age tag, then it’s easy to do in Python with readlines, which reads the whole file into a list of lines, something like this

     with open("file.dat") as f:
         lines = f.readlines()
         for ix, line in enumerate(lines):
             if # line has age field
                # get record from lines[ix-1]
    

    or, of course, you can always just keep the previous line in memory in Awk

     BEGIN { prevline = "" }
           { # process the line
             prevline = $0
           }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping 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.