1

I have two text files, F1a.txt and F1b.txt, with essentially the same data: the driver's surname and their nationality. For example:

Hamilton       GBR
Alonso         SPA

I have concatenated these two files into F1_drivers.txt. How do I do the following?

  1. Using pipes, display the drivers in F1a.txt and F1b.txt that are British, sorted alphabetically.

  2. Output a long listing of files in the current directory, written to a file named listing.

  3. Switch to a new directory and append the long listing of files in that directory to listing.

  4. Grep for the string GER in all files beginning with F1 and ending in .txt.

  5. Do #4, but pipe the output to cat to create a file containing the output, named grepout.txt

I have been given basic info to help, but don't know where to go.

  • This should not have been edited from the original, as the original stated the OP was new to the shell. Whoever removed that content, caused the downvote cascade :) – eyoung100 Sep 29 '14 at 18:41
  • I suggest a roll-back. – Torger597 Sep 29 '14 at 20:34
  • While I appreciate that Mods like Michael are working to clean up Answers/Questions, a lot of information was removed from the question. – Torger597 Sep 29 '14 at 20:36
  • 1
    @Torger597 nothing was removed that affects the contents of the question. All that Michael removed was irrelevant background information that is not needed to understand the problem at hand. – terdon Sep 29 '14 at 22:29
2
  1. cat F1a.txt F1b.txt | grep British | sort -d
    We use cat to read the contents of files on a *nix system.
    We then use a pipe, the character |, to feed the output from cat into grep.
    We then use the command grep to search the input from cat for the word British, and grep filters the output to show the proper lines.
    We then use another pipe to feed the output from grep into sort, which, as you can probably guess, sorts its input. We use the -d argument to sort it alphabetically.
  2. ls -al > listing
    To do this one, we need to use the shell I/O redirection operator of '>'.
    When you use it, followed by a file, what BASH does is as follows:
    It takes the STDOUT or standard output (command line or tool output),
    and if a file exists with that path, it overwrites it with the new data from STDOUT, otherwise it creates the file and writes the data from STDOUT to it.
    We take ls, which gives us the working directory's information, and feed it the -al argument, which essentially tells it to display -allfiles in the -longlisting format.
    We then use the > redirection operator to place the output of ls into the file, listing.
  3. cd a/directory/
    ls -al >> /path/to/listing
    cd switches directories. What I mean by cd a/directory/ is to pick a random directory and use it with the command, not literally cd to a/directory/, unless that's what you really want to do.
    We then reuse the previous ls command - remember, -allfiles, -longformat - and we use a different shell redirection command. This time, we use >>, which takes STDOUT and appends it to the file specified afterwards, and we send it to the file listing, where ever you created it.
  4. cat F1*.txt | grep GER
    On this one, we're doing the same thing as on number one - using cat to read data from your files, and using grep to search through it. However, we're using yet another new shell operator, this time an asterisk, commonly referred to as a wildcard. What F1*.txt tells the shell to do is essentially this:
    In the current directory, as we haven't specified another - look through all the files. If they start with F1, have anything afterwards, and end with .txt, then return that filename. When we do this, cat is given multiple files, and it does its job - it concatenates the contents of the files specified into STDOUT.
    We then feed all the information in STDOUT through the pipe into grep, like we did earlier, and we tell it to search for GER.
  5. cat F1*.txt | grep GER | cat > grepout.txt What we can then do, with this, is give cat the output of grep as input, again, through a pipe - and then save cat's output, via the > operator like we did earlier - to finish off the whole she-bang.
  • man pages may be nice to reference ;D – Torger597 Sep 29 '14 at 18:23
  • 2
    No offense @Torger597, while this answer is correct, and since you could use the Reputation, would you mind explaining why you chose to do what you did, since he kindly said he was new, I believe a textual explanation would help. If you can do that, you've got my upvote and you may catch a few others. – eyoung100 Sep 29 '14 at 18:27
  • @eyoung100 Will do, in about an hour. Busy working, if you understand. – Torger597 Sep 29 '14 at 19:19
  • I do :) I'm working too... – eyoung100 Sep 29 '14 at 19:21
  • I think I explained everything properly for a beginner. – Torger597 Sep 29 '14 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.