select alternative lines from a file

select alternative lines from a file

Post by gopikrishnan.gunaseka.. » Sun, 12 Feb 2006 02:28:19



hi all,
 Im using the following script to select alternative lines from a file.
cat -n <filename> | awk '{ if ($1%2 =0) print $0)
 its output contains the line number but i dont want it, Anyone help me
in this regard

Thanks & Regards,
Gopi Krishnan

 
 
 

select alternative lines from a file

Post by Chris F.A. Johnso » Sun, 12 Feb 2006 02:51:20



> hi all,
>  Im using the following script to select alternative lines from a file.
> cat -n <filename> | awk '{ if ($1%2 =0) print $0)
>  its output contains the line number but i dont want it, Anyone help me
> in this regard

awk 'NR % 2 == 0 { print }' FILENAME

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

 
 
 

select alternative lines from a file

Post by Ed Morto » Sun, 12 Feb 2006 04:04:16




>>hi all,
>> Im using the following script to select alternative lines from a file.
>>cat -n <filename> | awk '{ if ($1%2 =0) print $0)
>> its output contains the line number but i dont want it, Anyone help me
>>in this regard

> awk 'NR % 2 == 0 { print }' FILENAME

No need for the "{ print }" as that's the default action. I'd go with
just this for odd numbers:

        awk 'NR%2' file

and this for even:

        awk '(NR+1)%2' file

Regards,

        Ed.

 
 
 

select alternative lines from a file

Post by Chris F.A. Johnso » Sun, 12 Feb 2006 04:27:07





>>>hi all,
>>> Im using the following script to select alternative lines from a file.
>>>cat -n <filename> | awk '{ if ($1%2 =0) print $0)
>>> its output contains the line number but i dont want it, Anyone help me
>>>in this regard

>> awk 'NR % 2 == 0 { print }' FILENAME

> No need for the "{ print }" as that's the default action.

   It's not necessary in this limited script, but it's more
   understandable, and easier to deal with when modifying the script
   later.

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

 
 
 

select alternative lines from a file

Post by Ed Morto » Sun, 12 Feb 2006 04:48:10






>>>>hi all,
>>>>Im using the following script to select alternative lines from a file.
>>>>cat -n <filename> | awk '{ if ($1%2 =0) print $0)
>>>>its output contains the line number but i dont want it, Anyone help me
>>>>in this regard

>>>awk 'NR % 2 == 0 { print }' FILENAME

>>No need for the "{ print }" as that's the default action.

>    It's not necessary in this limited script, but it's more
>    understandable, and easier to deal with when modifying the script
>    later.

Then you should make it "{ print $0 }" for those reasons. Personally I
think it's more useful for people to understand and take advantage of
the default behaviors, especially with small, throwaway scripts.

        Ed.

 
 
 

select alternative lines from a file

Post by Chris F.A. Johnso » Sun, 12 Feb 2006 07:16:58






>>>>awk 'NR % 2 == 0 { print }' FILENAME

>>>No need for the "{ print }" as that's the default action.

>>    It's not necessary in this limited script, but it's more
>>    understandable, and easier to deal with when modifying the script
>>    later.

> Then you should make it "{ print $0 }" for those reasons.

    Perhaps, but I do not need to change the print statement when I
    modify the script. If I leave it out, I have to change the script
    just to get it to do what it was doing, before I can add things.

Quote:> Personally I think it's more useful for people to understand and
> take advantage of the default behaviors, especially with small,
> throwaway scripts.

    When you are familiar with them, by all means use them; I don't
    think they're appropriate for someone who has to ask about how to
    accomplish a task in the first place. When the experts get into a
    "Mine's shorter than yours!" battle, that's different.

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

 
 
 

select alternative lines from a file

Post by Dan Merce » Sun, 12 Feb 2006 08:11:26


: hi all,
:  Im using the following script to select alternative lines from a file.
: cat -n <filename> | awk '{ if ($1%2 =0) print $0)
:  its output contains the line number but i dont want it, Anyone help me
: in this regard
:
: Thanks & Regards,
: Gopi Krishnan
:

To output even numbered lines

    awk '!(NR%2)' filename    # parentheses necessary since ! has precedence over %

To output odd numbered lines

    awk 'NR%2' filename    # parentheses necessary since ! has precedence over %

Dan Mercer

 
 
 

select alternative lines from a file

Post by Bill Marcu » Sun, 12 Feb 2006 03:30:43




> hi all,
>  Im using the following script to select alternative lines from a file.
> cat -n <filename> | awk '{ if ($1%2 =0) print $0)
>  its output contains the line number but i dont want it, Anyone help me
> in this regard

awk 'NR%2==0' filename

--
I told my kids, "Someday, you'll have kids of your own."  One of them said,
"So will you."
                -- Rodney Dangerfield

 
 
 

select alternative lines from a file

Post by Ed Morto » Mon, 13 Feb 2006 00:14:55




<snip>
>>Personally I think it's more useful for people to understand and
>>take advantage of the default behaviors, especially with small,
>>throwaway scripts.

>     When you are familiar with them, by all means use them; I don't
>     think they're appropriate for someone who has to ask about how to
>     accomplish a task in the first place. When the experts get into a
>     "Mine's shorter than yours!" battle, that's different.

This isn't about brevity. I'm glad the experts I know in various areas
showed me how to do things the way they do rather than the way they
think is good enough for me. At least, I hope they did! It's better to
learn the right way first rather than have to break a bad habit later.

        Ed.

 
 
 

select alternative lines from a file

Post by Chris F.A. Johnso » Mon, 13 Feb 2006 01:49:47





><snip>
>>>Personally I think it's more useful for people to understand and
>>>take advantage of the default behaviors, especially with small,
>>>throwaway scripts.

>>     When you are familiar with them, by all means use them; I don't
>>     think they're appropriate for someone who has to ask about how to
>>     accomplish a task in the first place. When the experts get into a
>>     "Mine's shorter than yours!" battle, that's different.

> This isn't about brevity. I'm glad the experts I know in various areas
> showed me how to do things the way they do rather than the way they
> think is good enough for me. At least, I hope they did! It's better to
> learn the right way first rather than have to break a bad habit later.

    The right way isn't necesarily the shortest or most idiosyncratic
    way.

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

 
 
 

1. select line from file, and then the next line a week later

Let's say I have a file of 5 email addresses. What if I wanted to email
one person on the list, in sequence, every friday.  So on the first
friday, I would email the first person. The following friday I would
email the second person, and so on....

Now I could cron it for friday, but how can I get the script to grab
line one the first time I run it, then grab line 2 the next time I run
it. Is this even possible?

I don't think 'case' can do this for me. I am guessing awk might be
able to, but I am no good at awk.

2. ipfwadm: how can I accept random ports for ftp data connections ?

3. How can I prepend comments into selected lines of a data file?

4. "inconsistent soname" errors from /sbin/ldconfig

5. selecting duplicate lines in file

6. REPORT: Oak OTI-087 & Cheapo monitor working...almost

7. randomly select a line from a file

8. Sun opens door to Windows

9. Selecting a random line from a text file

10. How do you remove selected lines from file?

11. select a specific line from an ascii file

12. deleting selected lines in a file

13. perl: copy selected line from one file to another