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 ...
- 01-07-2011 #1Just 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.
studenten.txt is of the form:Code:$ echo studenten.txt 2 | pipe4 | xargs echo "Tweede regel:" Tweede regel: 00600248 Desmet Guillaume
The argument 2 means that the second line (in dutch: Tweede regel) need to be written to stdout and does not change.Code:$ cat studenten.txt ... 20053544 De Wolf Kristof 20053680 Lens Yoeri 20043436 Ronsyn Tim 20045050 Tchebakov Andreď 20045090 El-Cherkaoui Rajaa ...
Any suggestions?
Thanks!
- 01-07-2011 #2
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
- 01-07-2011 #3Just 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:
which gives meCode:#! /bin/bash sed 's/\([^ ]*\) 2/cat \1 | sed -n 2p/'
Now I want to execute this command, that's my problem.Code:cat studenten.txt | sed -n 2p
I know there's no need to put it in a script, but this was the assignment.
- 01-08-2011 #4Just 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
- 01-08-2011 #5Just Joined!
- Join Date
- Jan 2011
- Posts
- 4
- 01-08-2011 #6Just 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.
- 01-08-2011 #7Just Joined!
- Join Date
- Jan 2011
- Posts
- 4


Reply With Quote
