One last line - one last time

One last line - one last time

Post by Todd » Sat, 24 Jun 2006 03:59:07



Thanks to all for the guidance, and I am one line away from success.

sed "$1"d myfile.txt > /tmp/myfile.txt

If I echo $1 I receive the correct parameter for example /4/

if I use
sed 4d myfile.txt > /tmp/myfile.txt  'removes line 4

What is the correct syntax? In debug mode I get the error

sed ''\''9'\''d' myfile
sed: -e expression #1, char 1: unknown command:`''

It has to be in the way the argument is getting passed to sed right?

Todd

 
 
 

One last line - one last time

Post by Chris F.A. Johnso » Sat, 24 Jun 2006 04:13:02



> Thanks to all for the guidance, and I am one line away from success.

> sed "$1"d myfile.txt > /tmp/myfile.txt

> If I echo $1 I receive the correct parameter for example /4/

    Where do the slashes come from? Please post the exact code you are
    using, the exact command line you use to call it, and the exact
    output.

Quote:> if I use
> sed 4d myfile.txt > /tmp/myfile.txt  'removes line 4

> What is the correct syntax?

    Correct for what? Your first line look all right.

Quote:> In debug mode I get the error

    What debug mode? Do you mean with "set -x"?

Quote:> sed ''\''9'\''d' myfile
> sed: -e expression #1, char 1: unknown command:`''

> It has to be in the way the argument is getting passed to sed right?

    Then how *are* you passing the argument? Are you adding single
    quotes that are not stripped by the shell? Which shell?

--
   Chris F.A. Johnson, author              <http://cfaj.freeshell.org>
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   ===== My code in this post, if any, assumes the POSIX locale
   ===== and is released under the GNU General Public Licence

 
 
 

One last line - one last time

Post by Todd » Sat, 24 Jun 2006 04:39:47




>> Thanks to all for the guidance, and I am one line away from success.

>> sed "$1"d myfile.txt > /tmp/myfile.txt

>> If I echo $1 I receive the correct parameter for example /4/

>     Where do the slashes come from? Please post the exact code you are
>     using, the exact command line you use to call it, and the exact
>     output.

>> if I use
>> sed 4d myfile.txt > /tmp/myfile.txt  'removes line 4

>> What is the correct syntax?

>     Correct for what? Your first line look all right.

>> In debug mode I get the error

>     What debug mode? Do you mean with "set -x"?

>> sed ''\''9'\''d' myfile
>> sed: -e expression #1, char 1: unknown command:`''

>> It has to be in the way the argument is getting passed to sed right?

>     Then how *are* you passing the argument? Are you adding single
>     quotes that are not stripped by the shell? Which shell?

Thanks for the response Chris; Here is my whole script. I think I have
tried every variation of the sed command with either 0 lines being
removed or all lines being removed,- but never just that one line! If
function del_line was simply {echo "$DELME"} then it echos simply /4/.
I have to be done with this tonight so please nobody hack my script to
death!

#!/bin/bash
# An attempt to create a todo shellscript
# butchered by Todd D. Patton
# $Id: todo,v 1.1 2006/06/22 03:46:31 ua04 Exp $

SCRIPTNAME=${0##*/}

function show_list() {
         cat -n .dolist
         }

function del_line() {
         echo "$delme"
         sed "$delme"d .dolist > /tmp/dolist
         mv /tmp/dolist ~/bin/.dolist
         }

function  show_help() {
         echo "Usage: todo -[hrl:] "
         echo " 'something'      (adds to da' list)"
         echo "  with no options (displays da' list)"
         echo " -h               (prints usage)"
         echo " -r               (removes line number specified)"

exit 0
     }

#***********************************************************************
set -x
if [ "$1" != "-r" -a "$1" != "-h" -a "$1" != "" -a "$1" != "--help" ]
then
         echo "$*" >> .dolist
fi


if [ $? -ne 0 ]
then
         echo "crap"
         exit 1;
fi
set -- $ARGS

DONE=false
while [ "$DONE" != "true" ]
do
        case "$1" in
        -h | --help)show_help   ;;
        -r)delme=$2;del_line    ;;
        --)DONE=true            ;;      
        esac
shift
done
show_list

 
 
 

One last line - one last time

Post by Chris F.A. Johnso » Sat, 24 Jun 2006 05:21:41





>>> Thanks to all for the guidance, and I am one line away from success.

>>> sed "$1"d myfile.txt > /tmp/myfile.txt

>>> If I echo $1 I receive the correct parameter for example /4/

>>     Where do the slashes come from? Please post the exact code you are
>>     using, the exact command line you use to call it, and the exact
>>     output.

>>> if I use
>>> sed 4d myfile.txt > /tmp/myfile.txt  'removes line 4

>>> What is the correct syntax?

>>     Correct for what? Your first line look all right.

>>> In debug mode I get the error

>>     What debug mode? Do you mean with "set -x"?

>>> sed ''\''9'\''d' myfile
>>> sed: -e expression #1, char 1: unknown command:`''

>>> It has to be in the way the argument is getting passed to sed right?

>>     Then how *are* you passing the argument? Are you adding single
>>     quotes that are not stripped by the shell? Which shell?

> Thanks for the response Chris; Here is my whole script.

   All I really needed was the part that is causing problems, and (as
   I asked before) the exact command line you used to call it, and the
   exact output (or contents of your .dolist file).

Quote:> I think I have tried every variation of the sed command with either
> 0 lines being removed or all lines being removed,- but never just
> that one line! If function del_line was simply {echo "$DELME"} then
> it echos simply /4/.

   It does? Where do the slashes come from?

   What is your command line when it gives you a problem?

Quote:> I have to be done with this tonight so please nobody hack my script to
> death!

> #!/bin/bash
> # An attempt to create a todo shellscript
> # butchered by Todd D. Patton
> # $Id: todo,v 1.1 2006/06/22 03:46:31 ua04 Exp $

> SCRIPTNAME=${0##*/}

> function show_list() {

   Don't use this bash-only hybrid. Use either the portable form of
   definition (preferably):

show_list() {

   Or the ksh style:

function show_list {

Quote:>          cat -n .dolist

   The -n option to cat is non-standard.

Quote:>          }

> function del_line() {
>          echo "$delme"
>          sed "$delme"d .dolist > /tmp/dolist
>          mv /tmp/dolist ~/bin/.dolist

   You should only perform the move if sed was successful:

sed "$delme"d .dolist > /tmp/dolist &&
mv /tmp/dolist ~/bin/.dolist

   Why are you sending the file to a different location? You use sed
   on ./.dolist, and then move the modified file to ~/bin/.dolist.
   (Why would you put a data file in ~/bin?)

   Define the file in a variable and use that for all references to
   it:

DOLIST=$HOME/.dolist

del_line() {
         echo "$delme"
         sed "$delme"d "$DOLIST" > /tmp/dolist
         mv /tmp/dolist "$DOLIST"

- Show quoted text -

}
> function  show_help() {
>          echo "Usage: todo -[hrl:] "
>          echo " 'something'      (adds to da' list)"
>          echo "  with no options (displays da' list)"
>          echo " -h               (prints usage)"
>          echo " -r               (removes line number specified)"

> exit 0
>      }

> #***********************************************************************
> set -x
> if [ "$1" != "-r" -a "$1" != "-h" -a "$1" != "" -a "$1" != "--help" ]
> then
>          echo "$*" >> .dolist
> fi



    You have been advised not to use getopt. You are making the script
    far more complicated than it need be.

- Show quoted text -

Quote:> if [ $? -ne 0 ]
> then
>          echo "crap"
>          exit 1;
> fi
> set -- $ARGS

> DONE=false
> while [ "$DONE" != "true" ]
> do
>    case "$1" in
>    -h | --help)show_help   ;;
>    -r)delme=$2;del_line    ;;
>    --)DONE=true            ;;      
>    esac
> shift
> done
> show_list

--
   Chris F.A. Johnson, author              <http://cfaj.freeshell.org>
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   ===== My code in this post, if any, assumes the POSIX locale
   ===== and is released under the GNU General Public Licence
 
 
 

One last line - one last time

Post by Todd » Sat, 24 Jun 2006 05:51:59






>>>> Thanks to all for the guidance, and I am one line away from success.

>>>> sed "$1"d myfile.txt > /tmp/myfile.txt

>>>> If I echo $1 I receive the correct parameter for example /4/
>>>     Where do the slashes come from? Please post the exact code you are
>>>     using, the exact command line you use to call it, and the exact
>>>     output.

>>>> if I use
>>>> sed 4d myfile.txt > /tmp/myfile.txt  'removes line 4

>>>> What is the correct syntax?
>>>     Correct for what? Your first line look all right.

>>>> In debug mode I get the error
>>>     What debug mode? Do you mean with "set -x"?

>>>> sed ''\''9'\''d' myfile
>>>> sed: -e expression #1, char 1: unknown command:`''

>>>> It has to be in the way the argument is getting passed to sed right?
>>>     Then how *are* you passing the argument? Are you adding single
>>>     quotes that are not stripped by the shell? Which shell?

>> Thanks for the response Chris; Here is my whole script.

>    All I really needed was the part that is causing problems, and (as
>    I asked before) the exact command line you used to call it, and the
>    exact output (or contents of your .dolist file).

>> I think I have tried every variation of the sed command with either
>> 0 lines being removed or all lines being removed,- but never just
>> that one line! If function del_line was simply {echo "$DELME"} then
>> it echos simply /4/.

>    It does? Where do the slashes come from?

        That's what I am wondering - now I am getting '4'
        running the command
        #todo -r 4 'when the del_line function is called, I currently                   receive
        '4' 'from the echo line but line 4 is not removed from the file.

        debugging with set -x shows that sed expression error.

- Show quoted text -

Quote:>    What is your command line when it gives you a problem?

>> I have to be done with this tonight so please nobody hack my script to
>> death!

>> #!/bin/bash
>> # An attempt to create a todo shellscript
>> # butchered by Todd D. Patton
>> # $Id: todo,v 1.1 2006/06/22 03:46:31 ua04 Exp $

>> SCRIPTNAME=${0##*/}

>> function show_list() {

>    Don't use this bash-only hybrid. Use either the portable form of
>    definition (preferably):

        I am following conventions posted by my professor
Quote:

> show_list() {

>    Or the ksh style:

> function show_list {

>>          cat -n .dolist

>    The -n option to cat is non-standard.

        It was "suggested" to use nl - which was posted in my first request for
help I found cat -n easier to comprehend. Or at least to me  -a non
programmer
Quote:>>          }

>> function del_line() {
>>          echo "$delme"
>>          sed "$delme"d .dolist > /tmp/dolist
>>          mv /tmp/dolist ~/bin/.dolist

>    You should only perform the move if sed was successful:

> sed "$delme"d .dolist > /tmp/dolist &&
> mv /tmp/dolist ~/bin/.dolist

        Good suggestion - thanks

Quote:>    Why are you sending the file to a different location? You use sed
>    on ./.dolist, and then move the modified file to ~/bin/.dolist.
>    (Why would you put a data file in ~/bin?)

        School computer using ssh. I am running this from ~/bin and wanted to
make sure the output goes to the same location. I should just add it to
my path.

Quote:>    Define the file in a variable and use that for all references to
>    it:

> DOLIST=$HOME/.dolist

        Even better

- Show quoted text -

> del_line() {
>          echo "$delme"
>          sed "$delme"d "$DOLIST" > /tmp/dolist
>          mv /tmp/dolist "$DOLIST"
> }

>> function  show_help() {
>>          echo "Usage: todo -[hrl:] "
>>          echo " 'something'      (adds to da' list)"
>>          echo "  with no options (displays da' list)"
>>          echo " -h               (prints usage)"
>>          echo " -r               (removes line number specified)"

>> exit 0
>>      }

>> #***********************************************************************
>> set -x
>> if [ "$1" != "-r" -a "$1" != "-h" -a "$1" != "" -a "$1" != "--help" ]
>> then
>>          echo "$*" >> .dolist
>> fi


>     You have been advised not to use getopt. You are making the script
>     far more complicated than it need be.

        Again -"suggested" by professor

- Show quoted text -

Quote:

>> if [ $? -ne 0 ]
>> then
>>          echo "crap"
>>          exit 1;
>> fi
>> set -- $ARGS

>> DONE=false
>> while [ "$DONE" != "true" ]
>> do
>>        case "$1" in
>>        -h | --help)show_help   ;;
>>        -r)delme=$2;del_line    ;;
>>        --)DONE=true            ;;      
>>        esac
>> shift
>> done
>> show_list

 
 
 

One last line - one last time

Post by Chris F.A. Johnso » Sat, 24 Jun 2006 06:09:25







>>>>> Thanks to all for the guidance, and I am one line away from success.

>>>>> sed "$1"d myfile.txt > /tmp/myfile.txt

>>>>> If I echo $1 I receive the correct parameter for example /4/
>>>>     Where do the slashes come from? Please post the exact code you are
>>>>     using, the exact command line you use to call it, and the exact
>>>>     output.

>>>>> if I use
>>>>> sed 4d myfile.txt > /tmp/myfile.txt  'removes line 4

>>>>> What is the correct syntax?
>>>>     Correct for what? Your first line look all right.

>>>>> In debug mode I get the error
>>>>     What debug mode? Do you mean with "set -x"?

>>>>> sed ''\''9'\''d' myfile
>>>>> sed: -e expression #1, char 1: unknown command:`''

>>>>> It has to be in the way the argument is getting passed to sed right?
>>>>     Then how *are* you passing the argument? Are you adding single
>>>>     quotes that are not stripped by the shell? Which shell?

>>> Thanks for the response Chris; Here is my whole script.

>>    All I really needed was the part that is causing problems, and (as
>>    I asked before) the exact command line you used to call it, and the
>>    exact output (or contents of your .dolist file).

>>> I think I have tried every variation of the sed command with either
>>> 0 lines being removed or all lines being removed,- but never just
>>> that one line! If function del_line was simply {echo "$DELME"} then
>>> it echos simply /4/.

>>    It does? Where do the slashes come from?
>    That's what I am wondering - now I am getting '4'
>    running the command
>    #todo -r 4 'when the del_line function is called, I currently                   receive
>    '4' 'from the echo line but line 4 is not removed from the file.

    Yes it *is* removed!

    You are not putting the file back where you found it.

Quote:>    debugging with set -x shows that sed expression error.

    What error?

- Show quoted text -

Quote:>>    What is your command line when it gives you a problem?

>>> I have to be done with this tonight so please nobody hack my script to
>>> death!

>>> #!/bin/bash
>>> # An attempt to create a todo shellscript
>>> # butchered by Todd D. Patton
>>> # $Id: todo,v 1.1 2006/06/22 03:46:31 ua04 Exp $

>>> SCRIPTNAME=${0##*/}

>>> function show_list() {

>>    Don't use this bash-only hybrid. Use either the portable form of
>>    definition (preferably):
>    I am following conventions posted by my professor

    Get a new professor, one who knows what he's talking about.

- Show quoted text -

Quote:>> show_list() {

>>    Or the ksh style:

>> function show_list {

>>>          cat -n .dolist

>>    The -n option to cat is non-standard.

>    It was "suggested" to use nl - which was posted in my first request for
> help I found cat -n easier to comprehend. Or at least to me  -a non
> programmer
>>>          }

>>> function del_line() {
>>>          echo "$delme"
>>>          sed "$delme"d .dolist > /tmp/dolist
>>>          mv /tmp/dolist ~/bin/.dolist

>>    You should only perform the move if sed was successful:

>> sed "$delme"d .dolist > /tmp/dolist &&
>> mv /tmp/dolist ~/bin/.dolist
>    Good suggestion - thanks

>>    Why are you sending the file to a different location? You use sed
>>    on ./.dolist, and then move the modified file to ~/bin/.dolist.
>>    (Why would you put a data file in ~/bin?)

>    School computer using ssh. I am running this from ~/bin and wanted to

   The location of the program has nothing to do with the location of
   data files.

Quote:> make sure the output goes to the same location.

    You are not using the same location, and that is the source of
    your problem. You are reading .dolist in the current directory
    (whatever that is), and putting the modified file in $HOME/bin.

Quote:> I should just add it to my path.

    Your PATH has nothing to do with data files.

- Show quoted text -

>>    Define the file in a variable and use that for all references to
>>    it:

>> DOLIST=$HOME/.dolist
>    Even better

>> del_line() {
>>          echo "$delme"
>>          sed "$delme"d "$DOLIST" > /tmp/dolist
>>          mv /tmp/dolist "$DOLIST"
>> }

>>> function  show_help() {
>>>          echo "Usage: todo -[hrl:] "
>>>          echo " 'something'      (adds to da' list)"
>>>          echo "  with no options (displays da' list)"
>>>          echo " -h               (prints usage)"
>>>          echo " -r               (removes line number specified)"

>>> exit 0
>>>      }

>>> #***********************************************************************
>>> set -x
>>> if [ "$1" != "-r" -a "$1" != "-h" -a "$1" != "" -a "$1" != "--help" ]
>>> then
>>>          echo "$*" >> .dolist
>>> fi


>>     You have been advised not to use getopt. You are making the script
>>     far more complicated than it need be.

>    Again -"suggested" by professor

   There's no need to copy a prof's bad habits.

- Show quoted text -

Quote:>>> if [ $? -ne 0 ]
>>> then
>>>          echo "crap"
>>>          exit 1;
>>> fi
>>> set -- $ARGS

>>> DONE=false
>>> while [ "$DONE" != "true" ]
>>> do
>>>    case "$1" in
>>>    -h | --help)show_help   ;;
>>>    -r)delme=$2;del_line    ;;
>>>    --)DONE=true            ;;      
>>>    esac
>>> shift
>>> done
>>> show_list

--
   Chris F.A. Johnson, author              <http://cfaj.freeshell.org>
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   ===== My code in this post, if any, assumes the POSIX locale
   ===== and is released under the GNU General Public Licence
 
 
 

1. Lines selection where the first character equals the last one

Hello,

Which expression could be used with sed to display the lines with
equal characters on the first and the last positions.
Is there any type of conditions (=, > < etc) which could be applied
within the sed expression? I suppose this could be done with awk, but
is there any possibility to do selection with the sed  or may be grep?

Thanks for any advice,
Evgeni.

2. i am looking for binaries smap, smapd, smail and gated stuff for intel

3. convert first last to F.I. last

4. a question of curiosity

5. what sed command to print the first line and the last line

6. 1.2.13 ok, 2.0x won't install. HDD errors. Slack '96

7. Cut the first and the last line

8. Unix - System Management services disk

9. Cut away first and last 3 lines

10. delete first/last line?

11. remove last and first three lines from a file

12. how to output all lines except last one of file?

13. Editing and reading the first and last line on a document.