Solved

SED in Bash

Posted on 2014-03-11
8
403 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
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

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!

Question has a verified solution.

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

Suggested Solutions

The purpose of this article is to demonstrate how we can use conditional statements using Python.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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