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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:24:03+00:00 2026-06-08T12:24:03+00:00

I am using linux and bash. I have a text file with the context

  • 0

I am using linux and bash. I have a text file with the context generated in run time by other program. The length, number of lines and content of the text file changed from time to time. But there is some pattern unchanged in the text, one typical example is

123098230984LD#2e3
123098230984LD#23234
XER_3424324_23424
33: 34: 35: node:9-72-1408 &82 &34
$1231313
*3435322
link to port:323
3424242424LD#2234
332424LD#23424234

Here, I want to extract the pattern “node:NUMBER-NUMBER-NUMBER” and “port:NUMBER” but where it occurs in the text varied from time to time too. Now I manually extract the information. I am wondering if there is any way to extract the information automatically. What make it really difficult is the content change every time when the file generated.

  • 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-08T12:24:04+00:00Added an answer on June 8, 2026 at 12:24 pm

    You can use sed to extract the desired fields by getting rid of the undesired bits:

    pax> echo 'junk node:9-72-1408 more junk port:323 last junk'
         | sed -E 's/^.*(node:[0-9]+-[0-9]*-[0-9]*).*(port:[0-9]+).*$/\1 \2/'
    node:9-72-1408 port:323
    

    The .* bits simply represent any junk and the parentheses are used to “capture” the matching text so it can be used in the replacement (as \1 and \2).


    Sidebar:

    If your version of sed doesn’t support -E for extended regexes, it may support -r, as with certain versions of GNU sed.

    Otherwise, you’ll need to escape the parentheses and + characters:

    pax> echo 'junk node:9-72-1408 more junk port:323 last junk'
         | sed  's/^.*\(node:[0-9]\+-[0-9]\+-[0-9]\+\).*\(port:[0-9]\+\).*$/\1 \2/'
    node:9-72-1408 port:323
    

    The source code for GNU sed contains this little snippet:

    /* Undocumented, for compatibility with BSD sed.  */
        case 'E':
        case 'r':
    

    but this appears to have been introduced in 4.2 (i.e., it’s in 4.2 but not in 4.1.5, the last of the 4.1 series). See here for details.


    And, if you need the actual values in variables, you can use something like:

    pax> inpstr='junk-here node:9-72-1408 more-junk port:323 last-junk'
    
    pax> node=$(echo "$inpstr" | sed -E 's/^.*node:([0-9]+-[0-9]+-[0-9]+).*$/\1/')
    
    pax> port=$(echo "$inpstr" | sed -E 's/^.*port:([0-9]+).*$/\1/')
    
    pax> echo $inpstr
    junk-here node:9-72-1408 more-junk port:323 last-junk
    
    pax> echo $node
    9-72-1408
    
    pax> echo $port
    323
    

    (taking into account the earlier comments about using -r or adding extra escaping for “lesser” sed implementations).

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

Sidebar

Related Questions

I have been using 'bash' since the time I have been using Unix- Linux/Solaris.
I'm using linux and bash I have two files filea.wav and fileb.wav which are
I am new to bash and Linux . I have a program I have
I want to change the directory in linux using bash file. Below is the
I'm using objcopy on bash (ubuntu linux) and im trying to copy 2 sections
In a bash script in Linux, I am using flock [the command flock, not
Using Linux environment with java,I'm having the config file which should be configured before
I am using Linux Mint 12, Kernel 3. I have installed and configured Open
I'm using linux. I have a function called like: PlayBackgroundIntroMusic((char *)IntroMusic); The functions is:
I'm using Linux Inotify to detect FS events on my program. How could 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.