Find the answer to your Linux question:
Results 1 to 7 of 7
Hi guys I couldn't find anything on the forum related to this: One of the questions on an exam Informatics at Ghent University a few years ago consisted of writing ...
  1. #1
    Just Joined!
    Join Date
    Jan 2011
    Posts
    4

    pipes

    Hi guys

    I couldn't find anything on the forum related to this:

    One of the questions on an exam Informatics at Ghent University a few years ago consisted of writing one command (pipes, I/O-redirection allowed) that we have to put in a script named pipe4. As I have my exam monday, I'm preparing myself by making the old ones, but got stuck with this question.


    Code:
    $ echo studenten.txt 2 | pipe4 | xargs echo "Tweede regel:" 
       Tweede regel: 00600248 Desmet Guillaume
    studenten.txt is of the form:

    Code:
    $ cat studenten.txt
    ...
    20053544 De Wolf Kristof
    20053680 Lens Yoeri
    20043436 Ronsyn Tim
    20045050 Tchebakov Andreď
    20045090 El-Cherkaoui Rajaa
    ...
    The argument 2 means that the second line (in dutch: Tweede regel) need to be written to stdout and does not change.

    Any suggestions?
    Thanks!

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    So your pipe4 script receives as input a file and a number, and must output that number line of the file?

    What are you confused about? What isn't making sense to you? There are a number of ways to accomplish this: what are you doing so far?
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Posts
    4
    The script recieves a filename and always the number 2 (in dutch, "Second line" means "Tweede regel") as input.

    As a matter of fact, there are many solutions to achieve this, and I found a solution, but it's not a "one-line-command". So now I have

    pipe4:
    Code:
    #! /bin/bash
    sed 's/\([^ ]*\) 2/cat \1 | sed -n 2p/'
    which gives me
    Code:
    cat studenten.txt | sed -n 2p
    Now I want to execute this command, that's my problem.
    I know there's no need to put it in a script, but this was the assignment.

  4. #4
    Just Joined!
    Join Date
    Jan 2011
    Posts
    3
    Maybe, this would serve your needs:

    Code:
    [1607]user@host:/tmp$ cat studenten.txt 
    20053544 De Wolf Kristof
    20053680 Lens Yoeri
    20043436 Ronsyn Tim
    20045050 Tchebakov Andreď
    20045090 El-Cherkaoui Rajaa
    [1608]user@host:/tmp$ cat pipe4 
    read FILE RECORD && sed -n "${RECORD}p" "$FILE"
    [1609]user@host:/tmp$ echo studenten.txt 2 | ./pipe4 | xargs echo "Tweede regel:"
    Tweede regel: 20053680 Lens Yoeri
    [1610]user@host:/tmp$ ^2^3
    echo studenten.txt 3 | ./pipe4 | xargs echo "Tweede regel:"
    Tweede regel: 20043436 Ronsyn Tim
    [1611]user@host:/tmp$ exit

  5. #5
    Just Joined!
    Join Date
    Jan 2011
    Posts
    4
    Quote Originally Posted by miafl View Post
    Maybe, this would serve your needs:

    Code:
    [1607]user@host:/tmp$ cat studenten.txt 
    20053544 De Wolf Kristof
    20053680 Lens Yoeri
    20043436 Ronsyn Tim
    20045050 Tchebakov Andreď
    20045090 El-Cherkaoui Rajaa
    [1608]user@host:/tmp$ cat pipe4 
    read FILE RECORD && sed -n "${RECORD}p" "$FILE"
    [1609]user@host:/tmp$ echo studenten.txt 2 | ./pipe4 | xargs echo "Tweede regel:"
    Tweede regel: 20053680 Lens Yoeri
    [1610]user@host:/tmp$ ^2^3
    echo studenten.txt 3 | ./pipe4 | xargs echo "Tweede regel:"
    Tweede regel: 20043436 Ronsyn Tim
    [1611]user@host:/tmp$ exit
    Thanks! You really helped me out.
    After reading you're solution I've found the exact one:
    Code:
    sed "s/^\(.*\) \([^ ]*\)/\sed -n \2p \1/" | bash -

  6. #6
    Just Joined!
    Join Date
    Jan 2011
    Posts
    3
    Perhaps, I do not fully understand what exactly it is you're trying to achieve. But if executing a command from a string is what you need, you might also want to consider Bash's built-in eval function.

  7. #7
    Just Joined!
    Join Date
    Jan 2011
    Posts
    4
    Quote Originally Posted by miafl View Post
    Perhaps, I do not fully understand what exactly it is you're trying to achieve. But if executing a command from a string is what you need, you might also want to consider Bash's built-in eval function.
    eval makes things even simpeler, thanks again!

Posting Permissions

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