How to create a command

How to create a command

Post by AnotheR By » Sat, 20 Dec 1997 04:00:00



        Could someone point me in the right direction here?  I do not know
        much, but I want to learn how to create a new command.  I have a
        unix directory with several files, and would like to create a
        command which would work like this:

        Read all the lines in the specified file (or all files) and if the
        string is found, then delete the whole line.  Keep doing this until
        all such lines in the specified files are deleted.

        The command would look like this:

            newcommand <string> <filename or wildcard>

        I don't know if this is a simple few lines of code or not, so I hope
        this isn't too much to ask.

 
 
 

How to create a command

Post by Wim Geurt » Sat, 20 Dec 1997 04:00:00



>         Could someone point me in the right direction here?  I do not know
>         much, but I want to learn how to create a new command.  I have a
>         unix directory with several files, and would like to create a
>         command which would work like this:

>         Read all the lines in the specified file (or all files) and if the
>         string is found, then delete the whole line.  Keep doing this until
>         all such lines in the specified files are deleted.

>         The command would look like this:

>             newcommand <string> <filename or wildcard>

>         I don't know if this is a simple few lines of code or not, so I hope
>         this isn't too much to ask.

if you have a perl compiler:

  perl -e 's/\n<string>\n/\n/g' -p -i.bak <filename or wildcard>

 
 
 

How to create a command

Post by Wim Geurt » Sat, 20 Dec 1997 04:00:00




> >         Could someone point me in the right direction here?  I do not know
> >         much, but I want to learn how to create a new command.  I have a
> >         unix directory with several files, and would like to create a
> >         command which would work like this:

> >         Read all the lines in the specified file (or all files) and if the
> >         string is found, then delete the whole line.  Keep doing this until
> >         all such lines in the specified files are deleted.

> >         The command would look like this:

> >             newcommand <string> <filename or wildcard>

> >         I don't know if this is a simple few lines of code or not, so I hope
> >         this isn't too much to ask.

> if you have a perl compiler:

>   perl -e 's/\n<string>\n/\n/g' -p -i.bak <filename or wildcard>

  almost: turn nongreedy option on... (sorry)

Wim

 
 
 

How to create a command

Post by Kurt J. Lanz » Sat, 20 Dec 1997 04:00:00



>         Could someone point me in the right direction here?  I do not know
>         much, but I want to learn how to create a new command.  I have a
>         unix directory with several files, and would like to create a
>         command which would work like this:

>         Read all the lines in the specified file (or all files) and if the
>         string is found, then delete the whole line.  Keep doing this until
>         all such lines in the specified files are deleted.

>         The command would look like this:

>             newcommand <string> <filename or wildcard>

>         I don't know if this is a simple few lines of code or not, so I hope
>         this isn't too much to ask.

A unix command can be created by creating  a file containing unix
commands and making  that file executable.
Check the man pages for "sh" (how the sell  works), "chmod" (making
executable), "sed" and "grep" (two commands which do what you want).
--
Kurt J. Lanza

 
 
 

How to create a command

Post by Mariusz Scislowsk » Sat, 20 Dec 1997 04:00:00




> >         Could someone point me in the right direction here?  I do not know
> >         much, but I want to learn how to create a new command.  I have a
> >         unix directory with several files, and would like to create a
> >         command which would work like this:

> >         Read all the lines in the specified file (or all files) and if the
> >         string is found, then delete the whole line.  Keep doing this until
> >         all such lines in the specified files are deleted.

> >         The command would look like this:

> >             newcommand <string> <filename or wildcard>

> >         I don't know if this is a simple few lines of code or not, so I hope
> >         this isn't too much to ask.

> if you have a perl compiler:

>   perl -e 's/\n<string>\n/\n/g' -p -i.bak <filename or wildcard>

or

cat <filename> | sed /<string>/d

 
 
 

How to create a command

Post by Dr. » Mon, 22 Dec 1997 04:00:00


:         Could someone point me in the right direction here?  I do not know
:         much, but I want to learn how to create a new command.  I have a
:         unix directory with several files, and would like to create a
:         command which would work like this:

:         Read all the lines in the specified file (or all files) and if the
:         string is found, then delete the whole line.  Keep doing this until
:         all such lines in the specified files are deleted.

:         The command would look like this:

:             newcommand <string> <filename or wildcard>
[snipped for berevity]

Well, I have such a utility that I use all the time. The actual program that
you're using internally is called 'sed' (read the man page):

== Start Code =========

#!/bin/ksh (or your favorite shell)

INSTRING="$1"
FILENAME="$2"
TEMPFILE="/tmp/${FILENAME}.tmp"

sed g/${INSTRING}/d > $TEMPFILE
mv $TEMPFILE $FILENAME

== End Code ===========

Note: This will not work with wild cards. If you want to feed in multiple files,
you have to use a loop (or write better code! *grin*). The looping would work
as:

for file in *.txt
do
        newcommand <string> $file
done

- Stephen

 
 
 

How to create a command

Post by Wim Geurt » Tue, 23 Dec 1997 04:00:00





> > >         Could someone point me in the right direction here?  I do not know
> > >         much, but I want to learn how to create a new command.  I have a
> > >         unix directory with several files, and would like to create a
> > >         command which would work like this:

> > >         Read all the lines in the specified file (or all files) and if the
> > >         string is found, then delete the whole line.  Keep doing this until
> > >         all such lines in the specified files are deleted.

> > >         The command would look like this:

> > >             newcommand <string> <filename or wildcard>

> > >         I don't know if this is a simple few lines of code or not, so I hope
> > >         this isn't too much to ask.

> > if you have a perl compiler:

> >   perl -e 's/\n<string>\n/\n/g' -p -i.bak <filename or wildcard>

> or

> cat <filename> | sed /<string>/d

  How would you redirect the output in the original (separate) files?

BTW, the one-liner I descibed is wrong; it should be:

perl -e 's/.*<string>.*\n//g' -p -i.bak <filename or wildcard>

("." does not match a newline)
It substitutes in the original files and makes a backup of every file that changed
(after the famous perl one-liners)

Wim

 
 
 

How to create a command

Post by Randal Schwart » Mon, 29 Dec 1997 04:00:00


This Week's Useless Use of Cat Award goes to...

Mariusz> cat <filename> | sed /<string>/d

And of course, if you've been following along for a week or two, you know
that this (BING!) is a Useless Use of Cat!

Rememeber, nearly all cases where you have:

        cat file | some_command and its args ...

you can rewrite it as:

        <file some_command and its args ...

and in some cases, such as this one, you can move the filename
to the arglist as in:

        some_command and its args ... file

Just another Useless Use of Usenet,
--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying

Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me

 
 
 

How to create a command

Post by Mariusz Scislowsk » Tue, 30 Dec 1997 04:00:00



> This Week's Useless Use of Cat Award goes to...


> Mariusz> cat <filename> | sed /<string>/d

Yeah, and the cat command is really going to absolutely smash your unix
box isn't it ?.
You should get a reward for a
"This week's useless post"

mind you, I should get another award for

"this week's useless reply "  

:)

Regards
Mariusz

 
 
 

1. NIS+ passwd command yeilds a Couldn't create a D-H key-pair error

Root on a NIS+ master server, does not have permission to execute the
passwd command on any other users. It says Permission Denied, and the
/var/adm/messages file gets the message

Couldn't create a D-H key-pair (len=192, ..)

Root can create the account information, and the credentials for new
users, and these can be seen in the cred and passwd tables. Only the
passwd command does not work.

Any ideas?

--
Posted via http://dbforums.com

2. LIVE AUDIO OF FOOTBALL GAME ON THE WEB

3. Creating commands from ls output

4. Parameter length - help required

5. How to create batch file that replaces standard Unix commands?

6. cdrom freeze on slackware 8.1

7. cdrw command to create audio cds - Segmentation Fault

8. Token Ring, NetWare and HELP!

9. Creating first two LINUX disks with UNIX DD command

10. to create a command that prints out filesystem

11. How can i create an specific command for vi-editor?

12. Command to Create a Socket File?

13. Creating script to add new users w/o using USERADD command