Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, I'm new to using sed and can't figure out how to get this to work. What I want to do is read a file and when a certain pattern ...
  1. #1
    DJM
    DJM is offline
    Just Joined!
    Join Date
    Nov 2008
    Posts
    9

    Using SED with a script file

    Hi, I'm new to using sed and can't figure out how to get this to work. What I want to do is read a file and when a certain pattern is matched I want to add a line after the one that matches the pattern. I created a file with the sed commands but when I execute it I get the following error: sed: Unsupported command '

    Here is my command file:

    '
    /#LoginGraceTime/ a\
    LoginGraceTime 60
    '
    '
    /#MaxAuthTries/ a\
    MaxAuthTries 3
    '
    '
    /#RSAAuthentication/ a\
    RSAAuthentication no
    '
    '
    /#PubkeyAuthentication/ a\
    PubkeyAuthentication yes
    '
    '
    /#AuthorizedKeysFile/ a\
    AuthorizedKeysFile ssh/authorized_keys
    '
    '
    /#PasswordAuthentication/ a\
    PasswordAuthentication no
    '
    '
    /#PermitEmptyPasswords/ a\
    PermitEmptyPasswords no
    '
    '
    /#Banner/ a\
    Banner /root/ssh/banner.txt
    '
    Here is what I entered to test it:

    Code:
    # cat ./OriginalFiles/WALL-E/sshd_config | sed -f ./Common/SSH/sshd_sed_cmds.sed
    This is when I get the error. Can anyone help?

    Thanks
    Don

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Welcome to the forums.

    When you are posting data and code, please use formatting tags around the lines. That makes them easier to read. To do that easily, simply highlight the lines with the mouse, then click the # button above the editing window.

    You did a good job showing us your work and the command file.

    Debugging often includes writing little test cases. That's what I did in the example below.

    You may have run across a sample of a sed command that used quotes, since I see you have quotes in the command file. The quotes are used when you supply the commands directly in the command line. That isolates them from interpretation by shell. However, a sed command file is not scanned by the shell, so that you don't need them there, and, in fact, will cause an error.

    Note also that when asking for aid, it is most helpful if you copy / paste the exact error message. The error message in the test case below includes detail that gives us the key to the problem.
    Code:
    #!/bin/bash -
    
    # @(#) s1       Demonstrate sed insert after in command file format.
    
    echo
    echo "(Versions displayed with local utility \"version\")"
    version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) sed
    set -o nounset
    echo
    
    FILE1=data1
    FILE2=data2
    
    echo
    echo " Data file $FILE1:"
    cat $FILE1
    
    echo
    echo " Data file $FILE2:"
    cat $FILE2
    
    # Create input file.
    
    INPUT="input"
    cat >$INPUT <<EOF
    1
    2
    3
    EOF
    
    echo
    echo " Input file $INPUT:"
    cat $INPUT
    
    echo
    echo " Results, first try:"
    sed -f $FILE1 $INPUT
    
    echo
    echo " Results, second try:"
    sed -f $FILE2 $INPUT
    
    exit 0
    Producing:
    Code:
    &#37; ./s1
    
    (Versions displayed with local utility "version")
    Linux 2.6.11-x1
    GNU bash 2.05b.0
    GNU sed version 4.1.2
    
    
     Data file data1:
    '
    /2/a\
    First attempt to insert
    '
    
     Data file data2:
    /2/a\
    Second attempt to insert
    
     Input file input:
    1
    2
    3
    
     Results, first try:
    sed: file data1 line 1: unknown command: `''
    
     Results, second try:
    1
    2
    Second attempt to insert
    3
    cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    DJM
    DJM is offline
    Just Joined!
    Join Date
    Nov 2008
    Posts
    9
    Hi DRL,

    Thanks for the tips.

    You may have run across a sample of a sed command that used quotes, since I see you have quotes in the command file. The quotes are used when you supply the commands directly in the command line. That isolates them from interpretation by shell. However, a sed command file is not scanned by the shell, so that you don't need them there, and, in fact, will cause an error.
    This is exactly what I did. I removed the quotes and it works perfectly now!

    I have found some OK tutorials on sed online but have not found any really good ones. Any recommendations?

    Thanks
    Don

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi, Don.

    You're welcome.

    I have seen this tutorial recommended often:

    Sed - An Introduction and Tutorial

    I think the IBM tutorials are quite good; this is part 1 of 3:

    Common threads: Sed by example, Part 1

    However, whenever you think of data "fields", I suggest you think of awk ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...