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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:27:37+00:00 2026-05-23T11:27:37+00:00

I want to create a sed command to find in a file the 1st

  • 0

I want to create a sed command to find in a file the 1st line that matchs a pattern and the delete all that line or replace it all with some other text. I dont want to match all the line because the rule is to match part of it.

how can i do it with sed?

for instance:

myline 1 is pretty
line 2 is ugly
myline 111 is nice

I want to delete the first line that contains “1 is”

UPDATE: My line may have characters like “/” and “<”

Regards
Fak

  • 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-23T11:27:38+00:00Added an answer on May 23, 2026 at 11:27 am

    I tend to use awk for more complicated tasks, it’s a bit more powerful than sed, having proper looping and selection constructs (expanded for readability, you can compress it back to one line if you wish):

    pax$ echo '
    xyz
    myline 1 is pretty
    line 2 is ugly
    myline 111 is nice'  | awk '
         /1 is/ {if (f) {print} else {f = 1}}
        !/1 is/ {print}'
    
    xyz
    line 2 is ugly
    myline 111 is nice
    

    For any line not matching the pattern (!/1 is/), it just prints it.

    For lines that do match the pattern, it prints them all if the flag f is set (it’s initially not set). When the flags not set and it encounters a matching line, it sets the flag and doesn’t print it. This basically deletes the first matching line as desired.

    If you want to modify the first matching line rather than deleting it, you just insert code alongside the f = 1 to do that, something like:

    pax$ echo '
    xyz
    myline 1 is pretty
    line 2 is ugly
    myline 111 is nice'  | awk '
         /1 is/ {if (f) {print} else {f = 1;print "URK!!!"}}
        !/1 is/ {print}'
    
    xyz
    URK!!!
    line 2 is ugly
    myline 111 is nice
    

    To use a shell variable in awk, you can use the -v option to pass it in as a real awk variable:

    pax$ export xyzvar=URK ; echo '
    xyz
    myline 1 is pretty
    line 2 is ugly
    myline 111 is nice'  | awk -vmyvar=$xyzvar '
         /1 is/ {if (f) {print} else {f = 1;print myvar}}
        !/1 is/ {print}'
    
    xyz
    URK
    line 2 is ugly
    myline 111 is nice
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a simple http proxy server that does some very basic
I want create a tarball that when extracted the file(s) will be placed at
I want create a site by command line using appcmd. How can I associate
I have a sed command that I want to run on a huge, terrible,
I want to create a function that performs a function passed by parameter on
I want create some view, and add dynamically to it text and images Somebody
I have a tab delimited file that contains date, header row, some values, empty
I want create texture atlas(matrix of images) from multiple images.Is their any applescript that
I want create a cache so that when an item doesn't exist, only one
I want create a function that verify if the numbers of a list fall

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.