Solved

SED in Bash

Posted on 2014-03-11
8
399 Views
Last Modified: 2014-03-13
#!/bin/bash
# Question 3
INT_TXT=interger.txt
cat interger.txt|sort -n > sorted_file
echo "first 25%"
sed -n '751,1000p' sorted_file|sort -n -r
echo "26% to 75%"
sed -n '751,1000p' sorted_file|sort -n -r
echo "small 25%"
sed -n '1,250p' sorted_file|sort -n -r

Open in new window


can someone explain what sed is doing and why?
0
Comment
Question by:Mark_Co
8 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 39921884
Is it homework?
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 167 total points
ID: 39921896
man sed

NAME
     sed -- stream editor

SYNOPSIS
     sed [-Ealn] command [file ...]
     sed [-Ealn] [-e command] [-f command_file] [-i extension] [file ...]

DESCRIPTION
     The sed utility reads the specified files, or the standard input if no files are specified, modifying the input as spec-
     ified by a list of commands.  The input is then written to the standard output.

...
     The form of a sed command is as follows:

           [address[,address]]function[arguments]

     Whitespace may be inserted before the first address and the function portions of the command.

     Normally, sed cyclically copies a line of input, not including its terminating newline character, into a pattern space,
     (unless there is something left after a ``D'' function), applies all of the commands with addresses that select that
     pattern space, copies the pattern space to the standard output, appending a newline, and deletes the pattern space.

     Some of the functions use a hold space to save all or part of the pattern space for subsequent retrieval.

Sed Addresses
     An address is not required, but if specified must be a number (that counts input lines cumulatively across input files),
     a dollar (``$'') character that addresses the last line of input, or a context address (which consists of a regular
     expression preceded and followed by a delimiter).

     A command line with no addresses selects every pattern space.

     A command line with one address selects all of the pattern spaces that match the address.

     A command line with two addresses selects an inclusive range.  This range starts with the first pattern space that
     matches the first address.  The end of the range is the next following pattern space that matches the second address.

...

     [2addr]p
             Write the pattern space to standard output.
0
 
LVL 5

Accepted Solution

by:
Joe Jenkins earned 167 total points
ID: 39921912
sed is a stream editor which can transform text.

The -n means silent or quiet.  

Here is a little primer on sed:
http://www.tty1.net/sed-intro_en.html

In the event this is homework, this is a good place to learn it.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 31

Assisted Solution

by:farzanj
farzanj earned 166 total points
ID: 39921937
You can see man pages on sed.

man sed

Basically, -n option is for not printing lines unless they match.  So you will only print lines that fit the criteria.

p  is for printing.   You can give a range of line numbers with p option.

Range has to be separated by commas.
0
 

Author Comment

by:Mark_Co
ID: 39922210
thanks. I didn't know there was a policy. It's not homework I am required to do.This is a "learn on your own" document I have been provided as an option to learn more if i want to and I'm trying to understand what the code is doing. I am not receiving a grade on this in any way nor have i ever asked for homework answers on EE. If I was trying to get answers for homework to turn in then yes I would see your point. However, I am simply trying to understand something, not to do homework and receive credit for something I didn't do.

Nonetheless, I will avoid posting things like this. Thanks
0
 

Author Closing Comment

by:Mark_Co
ID: 39922212
thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to count occurrences of each item in an array.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now