Solved

SED in Bash

Posted on 2014-03-11
8
401 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

821 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