select a specific line from an ascii file

select a specific line from an ascii file

Post by roger jaec » Sun, 24 Oct 1999 04:00:00



Hello everybody,

Maybe the problem is very easy to solve but not for me:

the problem is the following:

I have an ascii file that looks similar to the following one:

Jaeck Roger male
Jaeck Marco male
Jaeck Rene male
Jaeck Ruth female
and so on

how can I write a little shell function with a parameter, for example:
select_line file [paramter]

for example: select familiy 3

that returns
Jaeck Rene male

Thanks a lot for everybody's useful hint.
regards roger

 
 
 

select a specific line from an ascii file

Post by Martin Rams » Sun, 24 Oct 1999 04:00:00


On Sat, 23 Oct 1999 19:11:25 +0200,


>I have an ascii file that looks similar to the following one:

>Jaeck Roger male
>Jaeck Marco male
>Jaeck Rene male
>Jaeck Ruth female
>and so on

>how can I write a little shell function with a parameter, for example:
>select_line file [paramter]

>for example: select familiy 3

>that returns
>Jaeck Rene male

As usual, there are many ways to do this:

sed -n '3 p' family
        -n  = suppress default output
        3 p = in line 3, print

sed -n '3 p; 3 q' family
        A faster version which doesn't scan the rest of the file.
        3 q = in line 3, quit

awk 'NR == 3' family
awk 'NR == 3 { print }' family
        NR = ordinal number of the current record

awk 'NR == 3 { print; exit }' family
        A faster version which doesn't scan the rest of the file.

perl -ne 'print if $.==3' family
perl -ne 'if($.==3){ print }' family
        $. = current line number

perl -ne 'if($.==3){ print; exit }' family
        A faster version which doesn't scan the rest of the file.

tail +3 family | head -1
        Skip first two lines, then only select first of result.

This should be enough for a starter ... :)

Regards,
  Martin
--

PGP KeyID=0xE8EF4F75 FiPr=5244 5EF3 B0B1 3826  E4EC 8058 7B31 3AD7

 
 
 

select a specific line from an ascii file

Post by Charles Dem » Sun, 24 Oct 1999 04:00:00




>Hello everybody,

>Maybe the problem is very easy to solve but not for me:

>the problem is the following:

>I have an ascii file that looks similar to the following one:

>Jaeck Roger male
>Jaeck Marco male
>Jaeck Rene male
>Jaeck Ruth female
>and so on

>how can I write a little shell function with a parameter, for example:
>select_line file [paramter]

>for example: select familiy 3

>that returns
>Jaeck Rene male

#!/bin/sh
sed -n "$2 p" $1

is one way, put in more checking if you want it to be more bullet
proof.

another way is:

#!/bin/sh
awk 'NR==v' v=$2 $1

or

#/bin/sh
awk "NR==$2" $1

read the man pages

man sh
man sed
man awk

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.

 
 
 

select a specific line from an ascii file

Post by Ken Pizzi » Sun, 24 Oct 1999 04:00:00



>I have an ascii file that looks similar to the following one:

>Jaeck Roger male
>Jaeck Marco male
>Jaeck Rene male
>Jaeck Ruth female
>and so on

>how can I write a little shell function with a parameter, for example:
>select_line file [paramter]

>for example: select familiy 3

>that returns
>Jaeck Rene male

function select_line { sed "${2}q;d" "$1"; }

                --Ken Pizzini

 
 
 

select a specific line from an ascii file

Post by Friedrich Dominicu » Wed, 27 Oct 1999 04:00:00


There is a Scheme-Shell out there in which that looks like:
(define (print-line file line-number)
  (let ((a-port (open-input-file file)))
        (define (just-line counter)
          (let ((line (read-line a-port)))
                (if (= counter line-number)
                        (begin
                          (close-input-port a-port)
                          (format #t "~a ~%" line))
                        (just-line (+ 1 counter)))))
        (just-line 1)))

But there were other answers already. But hey i't s bit different ;-)

Regards
Friedrich

 
 
 

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. Free AltaVista access

3. filter lines with a specific expression at a specific position

4. ps -ef command

5. deleting lines in ascii text file

6. Staroffice 5.2

7. SED / Line / (Need to get data from searched line from specific char to char)

8. PAM authentication and rlogin

9. How to delete a specific line in a file?

10. Reading an ASCII file one line at a time in shell

11. Search and replace text in a file based on a specific line

12. bsh: How to read a specific line from a text file?

13. how to get a specific line number of a file