Solved

SED in Bash

Posted on 2014-03-11
8
404 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

690 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