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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:54:19+00:00 2026-05-13T06:54:19+00:00

On a Linux system, I have a very large text file and I need

  • 0

On a Linux system, I have a very large text file and I need to create a new text file which contains every line between the first and last of occurrence of a particular sessionId (those lines included).

I guess I probably need to use sed or something?

As a bonus, sometimes I won’t know which log file will contain the session trace. So a script that can work with regular expressions would be ideal. In this case I would expect the script to find the first file with the sessionId in it and then crop that file before exiting.

Example Log file looking for sessionId 1111-ABCD-1111-SOME-GUID :

line one containing other session id: 2222-ABCD-1111-SOME-GUID blaa blaa blaa
line two blaa blaa blaa
line three containing my session id: 1111-ABCD-1111-SOME-GUID blaa blaa blaa
line four containing other session id: 2222-ABCD-1111-SOME-GUID
line five blaa blaa blaa
line six containing other session id: 3333-ABCD-1111-SOME-GUID blaa blaa blaa
line seven containing other session id: 2222-ABCD-1111-SOME-GUID
line eight containing my session id: 1111-ABCD-1111-SOME-GUID blaa blaa blaa
line nine containing other session id: 3333-ABCD-1111-SOME-GUID
line ten containing my session id: 1111-ABCD-1111-SOME-GUID
line eleven
line twelve containing other session id: 3333-ABCD-1111-SOME-GUID blaa blaa blaa
line thirteen containing my session id: 1111-ABCD-1111-SOME-GUID
line fouteen blaa blaa blaa
line fifteen containing other session id: 3333-ABCD-1111-SOME-GUID blaa blaa blaa

output file would contain lines three to thirteen inclusive.

  • 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-13T06:54:20+00:00Added an answer on May 13, 2026 at 6:54 am

    The following script will do all of what you asked, including the bonus. Put this script in the top-level directory that contains all the possible files with the ‘uid’ you want to crop. The script will recursively search this directory and crop all files that match and put the result in a new file with a .crp extension at the end (see example below). I took special consideration to make sure that this script will work with whatever filename you throw at it, whether it contain spaces or newlines or whatever in its name.

    #!/bin/bash
    uid="1111-ABCD-1111-SOME-GUID"
    
    while IFS= read -r -d $'\0' file; do
        printf "%s\n" "?$uid?+1,\$d" "1,/$uid/-1d" "%p" | ex -s "$file" > "$file".crp
        echo "$file being cropped"
    done < <(grep -lZR --exclude="${0#*/}" --exclude="*.crp" "$uid" .)
    

    Result

    $ ./uid.sh
    ./sample1.txt being cropped
    ./subdir/sample2.txt being cropped
    
    $ cat ./sample1.txt.crp
    line three containing my session id: 1111-ABCD-1111-SOME-GUID blaa blaa blaa
    line four containing other session id: 2222-ABCD-1111-SOME-GUID
    line five blaa blaa blaa
    line six containing other session id: 3333-ABCD-1111-SOME-GUID blaa blaa blaa
    line seven containing other session id: 2222-ABCD-1111-SOME-GUID
    line eight containing my session id: 1111-ABCD-1111-SOME-GUID blaa blaa blaa
    line nine containing other session id: 3333-ABCD-1111-SOME-GUID
    line ten containing my session id: 1111-ABCD-1111-SOME-GUID
    line eleven
    line twelve containing other session id: 3333-ABCD-1111-SOME-GUID blaa blaa blaa
    line thirteen containing my session id: 1111-ABCD-1111-SOME-GUID
    
    $ cat ./subdir/sample2.txt.crp
    line three containing my session id: 1111-ABCD-1111-SOME-GUID blaa blaa blaa
    foo
    bar
    line eight containing my session id: 1111-ABCD-1111-SOME-GUID blaa blaa blaa
    baz
    line ten containing my session id: 1111-ABCD-1111-SOME-GUID
    

    As you can see in the example above, my script found two files which matched, one of which was in a sub-directory below the top-level directory.

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

Sidebar

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.