Solved

SED in Bash

Posted on 2014-03-11
8
398 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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

705 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

16 Experts available now in Live!

Get 1:1 Help Now