Extract specific lines from a text file using Stream Editor (sed)

sed -n -e 1186,1210p A-small-practice.in
Using sed to extract lines in a text file If you write bash scripts a lot, you are bound to run into a situation where you want to extract some lines from a file. Yesterday, I needed to extract the first line of a file, say named somefile.txt. cat somefile.txt Line 1 Line 2 Line 3 Line 4 This specific task can be easily done with this: head -1 somefile.txt Line 1 For a more complicated task, like extract the second to third lines of a file. head is inadequate. So, let's try extracting lines using sed: the stream editor. My first attempt uses the p sed command (for print): sed 1p somefile.txt Line 1 Line 1 Line 2 Line 3 Line 4 Note that it prints the whole file, with the first line printed twice. Why? The default output behavior is to print every line of the input file stream. The explicit 1p command just tells it to print the first line .... again. To fix it, you need to suppress the default output (using -n), making explicit prints the only way to print to default output. sed -n 1p somefile.txt Line 1 Alternatively, you can tell sed to delete all but the first line. sed '1!d' somefile.txt Line 1 '1!d' means if a line is not(!) the first line, delete. Note that the single quotes are necessary. Otherwise, the !d will bring back the last command you executed that starts with the letter d. To extract a range of lines, say lines 2 to 4, you can execute either of the following: sed -n 2,4p somefile.txt sed '2,4!d' somefile.txt Note that the comma specifies a range (from the line before the comma to the line after). What if the lines you want to extract are not in sequence, say lines 1 to 2, and line 4? sed -n -e 1,2p -e 4p somefile.txt Line 1 Line 2 Line 4
Sample Output
6
     1
    6 6
   3 9 3
  1 2 2 1
 4 2 7 2 4
1 0 3 3 0 1
 4 2 7 2 4
  1 2 2 1
   3 9 3
    6 6
     1
4
   6
  5 7
 1 6 5
9 2 2 3
 3 7 9
  6 0
   0

0
By: evandrix
2011-06-04 10:53:46

1 Alternatives + Submit Alt

  • You can actually do the same thing with a combination of head and tail. For example, in a file of four lines, if you just want the middle two lines: head -n3 sample.txt | tail -n2 Line 1 --\ Line 2 } These three lines are selected by head -n3, Line 3 --/ this feeds the following filtered list to tail: Line 4 Line 1 Line 2 \___ These two lines are filtered by tail -n2, Line 3 / This results in: Line 2 Line 3 being printed to screen (or wherever you redirect it).


    0
    head -n1 sample.txt | tail -n1
    gtcom · 2011-06-14 17:45:04 5

What Others Think

Here they have shared the code that helps in extracting the specific lines from a text file using the stream editor https://www.horizoncenters.org and everything related to it is clearly explained in the force which makes it easy for the users to get an idea of it
Kaitlyn · 103 weeks ago
Hope this will be helpful for someone who really wanted to know about the extract specific lines from a text file using stream editor as they have shared the code for it and also have clearly explained the code that is shared here flat roofing company los angeles
Dwyane · 81 weeks and 3 days ago
Pug Puppies for Sale Near Me pugs puppies for sale teacup pugs for sale pug puppies for sale by owner pug puppies ohio PUG PUPPY FOR SALE NEAR ME PUG PUPPIES FOR SALE pug puppies for sale in kentucky Pug Puppies for Sale Under $500 Near Me pug puppies for sale in texas pug puppies for sale $200 pugs for sale near me under $500 pugs for sale under $400 near me pugs for sale near me puppies for sale near me under $500 pug puppies for sale under $1,000 near me pug for sale pug puppies for sale under $300 Brindle Pug Pitbull Pug Mix Pugs for sale cheap Cheap pug affordable pug puppies for sale near me black pugs for sale near me White Pugs for sale pug dog for sale free pug puppies pug puppies for sale in my area mn pug breeders pug puppies indiana pugs for sale michigan PUG PUPPY ADOPTION Pug puppies for sale Pug puppies for sale near me Pug puppies near me Pug Puppies for Sale Under $500 Near Me Cute Pug Puppies Black pug puppies Black pug puppies for sale pug puppies for adoption black pug puppies for sale near me chihuahua pug mix puppies how much is a pug puppy teacup pug puppies baby pug puppies pictures of pug puppies pug puppies for sale in Ohio pug puppies price pug mix puppies teacup pug puppies for sale best food for pug puppy newborn pug puppies pug puppies craigslist pug puppies for sale craigslist adorable pug puppies how much does a pug puppy cost Pitbull pug mix puppies pug pit mix puppy pug puppies for sale $200 pug puppies for sale in NJ Pug puppies for sale in Wisconsin pug puppy cost pug puppy food royal canin pug puppy royal canin pug puppy food fawn pug puppy pug puppies for sale florida pug puppies for sale in Indiana pug puppies for sale in KY pug puppies for sale in NC pug dog puppy AKC Registered Pug Puppies For sale cheap pug puppies for sale near me cheap pug puppies for sale in California cheap pug puppies for sale in nj Black Pug Puppies for sale
rahimhh21 · 30 weeks ago
Pug Puppies for Sale Near Me pugs puppies for sale teacup pugs for sale pug puppies for sale by owner pug puppies ohio PUG PUPPY FOR SALE NEAR ME PUG PUPPIES FOR SALE pug puppies for sale in kentucky Pug Puppies for Sale Under $500 Near Me pug puppies for sale in texas pug puppies for sale $200 pugs for sale near me under $500 pugs for sale under $400 near me pugs for sale near me puppies for sale near me under $500 pug puppies for sale under $1,000 near me pug for sale pug puppies for sale under $300 Brindle Pug Pitbull Pug Mix Pugs for sale cheap Cheap pug affordable pug puppies for sale near me black pugs for sale near me White Pugs for sale pug dog for sale free pug puppies pug puppies for sale in my area mn pug breeders pug puppies indiana pugs for sale michigan PUG PUPPY ADOPTION Pug puppies for sale Pug puppies for sale near me Pug puppies near me Pug Puppies for Sale Under $500 Near Me Cute Pug Puppies Black pug puppies Black pug puppies for sale pug puppies for adoption black pug puppies for sale near me chihuahua pug mix puppies how much is a pug puppy teacup pug puppies baby pug puppies baby pug puppies for sale pictures of pug puppies pug puppies for sale in Ohio pug puppies price pug mix puppies teacup pug puppies for sale best food for pug puppy newborn pug puppies pug puppies craigslist pug puppies for sale craigslist adorable pug puppies how much does a pug puppy cost Pitbull pug mix puppies pug pit mix puppy pug puppies for sale $200 pug puppies for sale in NJ Pug puppies for sale in Wisconsin pug puppy cost pug puppy food royal canin pug puppy royal canin pug puppy food fawn pug puppy pug puppies for sale florida pug puppies for sale in Indiana pug puppies for sale in KY pug puppies for sale in NC pug dog puppy AKC Registered Pug Puppies For sale cheap pug puppies for sale near me cheap pug puppies for sale in California cheap pug puppies for sale in nj Black Pug Puppies for sale pugs puppies for sale
Perfecthomepugs · 20 weeks and 5 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: