Server Administration

  Home arrow Server Administration arrow Page 5 - Stream Editor in the UNIX Shell
SERVER ADMINISTRATION

Stream Editor in the UNIX Shell
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-11-25

    Table of Contents:
  • Stream Editor in the UNIX Shell
  • How does it work?
  • Sed Addresses
  • Sed Commands
  • The hold space and whole scripts

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Stream Editor in the UNIX Shell - The hold space and whole scripts


    (Page 5 of 5 )

    You can use the hold space with the following commands:

    Command

    Effect

    N

    Take a new line from the input.

    Append a new line at the end of the current pattern space.

    Append a new line to the end of the space.

    If this is the last input line, the sed will exit.

    P

    Print the content of the pattern space until the first new line.

    h

    Print the pattern space to the hold space.

    H

    Append with a new line the content of the pattern space to the hold space.

    g

    Get. It will get the content of the hold space to the pattern space.

    G

    Append the content of the hold space with a new line to the end of the pattern space. (The new line will be prior to the hold content).

    x

    Switch the content of the hold space and the pattern space.

    D

    Print a line from the pattern space, all the way to the first new line. If there is more text in the pattern space, it will start a new cycle without reading the next token from the input.

    For example, let's look at the simulation of the tac tool. The tac tool will list the content of a file line by line in reverse order:

    #!/bin/sed -nf

    #The simulation of the tac tool with the help

    #Usage:

    # cat file | sed -nf rev.sed

    1!G #Unless we are at the first line get back the whole hold

    #space

    $p #This will print the last line from the pattern space

    h #put back all to the hold space

    Alternatively:

    cat file | sed -n -r '1h;1!{x;H};${g;p}'

    In the end, let me present a more complex script that takes advantage of the sed tool. It is about a little text editor tool that will add a new line, delete a line, and list the file.

    #!/bin/bash

     

    if [ $# -ne 1 ]

    then

    echo A single parameter is required

    exit 1

    fi

     

    if (file $1 | egrep "file")

    then

    touch "$1"

    fi

     

     

    select menu in "New line" "Delete" "List" "End"

    do

    case $REPLY in

    1) echo New line

    read newLine

    sed -i '$a'"$newLine" "$1"

     

    ;;

    2) echo Delete

    read row

    sed -i -n "$row"'d' "$1";;

    3) echo list

    read from to

    sed -n "$from"','"$to"' p' "$1"

    ;;

    4) echo End

    exit 1;;

    ?)echo Invalid Choice;;

    esac

    done

    I will give you the privilege to try out and learn on the upper script. With this, I thank you for your attention and hope that you learned a lot of useful information today. I invite you to rate my article. In addition, you can ask questions related to this article here in the comment area.blog. Next week I will continue with the awk, so if you are interested make sure to check back. Live With Passion!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.
    blog comments powered by Disqus

    SERVER ADMINISTRATION ARTICLES

    - Server Responses to Client Communication
    - Authentication in Client/Server Communication
    - Client/Server Communication
    - Understanding Awk in the UNIX Shell
    - Stream Editor in the UNIX Shell
    - Processes in the UNIX Shell
    - Migrating from Windows to Wine
    - Wine: Not Another Emulator
    - Preventive Measures to Block SSH Attacks
    - Monitoring Temperatures with Cacti
    - Cacti: RRDTool-based Graphing Solution
    - Network Magic 5.0 Review
    - Netfilter and Iptables Overview
    - Installing and Configuring Squid
    - Clickfree PC Backup Systems Compared


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap