-1

I need a sed command that will allow me to search for and display the info for all games released in the 1980's and alphabetize those results. Is there a good way to do this with only using sed?

1   Wii Sports                    Wii  2006  Nintendo            41.36
2   Super Mario Bros.             NES  1985  Nintendo            29.08
3   Duck Hunt                     NES  1985  Nintendo            26.93
4   Tetris                        GB   1989  Nintendo            23.20
5   Mario Kart Wii                Wii  2008  Nintendo            15.91
6   Wii Sports Resort             Wii  2009  Nintendo            15.61
7   Kinect Adventures!            X360 2010  MS Game Studios     15.09
8   New Super Mario Bros. Wii     Wii  2009  Nintendo            14.53
9   Wii Play                      Wii  2007  Nintendo            13.96
10  Super Mario World             SNES 1991  Nintendo            12.78
11  New Super Mario Bros.         DS   2006  Nintendo            11.28
12  Pokémon Red/Green/Blue        GB   1998  Nintendo            11.27
13  Super Mario Land              GB   1989  Nintendo            10.83
14  Call of Duty: Black Ops       X360 2010  Activision           9.76
15  Mario Kart DS                 DS   2005  Nintendo             9.71
16  Super Mario Bros. 3           NES  1990  Nintendo             9.54
17  Grand Theft Auto:San Andreas  PS2  2004  Rockstar Games       9.43
18  Call of Duty: Modern Warfare  X360 2011  Activision           9.07
19  Grand Theft Auto V            X360 2013  Rockstar Games       9.0
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
e.o
  • 9
  • 3

2 Answers2

3

It would not be impossible to use sed to extract the wanted data, but it would be a needlessly complicated exercise as awk better suited for working with data that can be organized into records (lines) consisting of fields (columns). The difficulty lies in coming up with a regular expression for matching the 4th column, and then determining whether that is an integer in the correct range. Apart from being terrible at counting and handling integers as anything other than strings of individual digits, sed is lousy when it comes to sorting.

Assuming that the data is tab-delimited (and that "alphabetize" means "sorted in lexicographic order"):

awk -F '\t' '$4 >= 1980 && $4 < 1990' file | sort -k 2

This uses awk to extract the lines whose 4th tab-delimited field (the year) is a number between 1980 and 1989. The resulting lines are then sorted in lexicographical order on the second whitespace-delimited field (the title, and the rest of the line).

If the file is delimited by spaces, you may try using any sequence of two or more spaces as delimiter in place of tab:

awk -F ' {2,}' '$4 >= 1980 && $4 < 1990' file | sort -b -k 2

Note the -b option used with sort here. It is needed to ignore the leading blanks of the second field. It is not needed if the delimiters in the data are single tabs.

The result I'm getting with your data in the question is

3   Duck Hunt                     NES  1985  Nintendo            26.93
2   Super Mario Bros.             NES  1985  Nintendo            29.08
13  Super Mario Land              GB   1989  Nintendo            10.83
4   Tetris                        GB   1989  Nintendo            23.20
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
1

If you want to order the games in alphabetical order by name, you can do so directly with sort. Here I've picked # as a field delimiter because it does not exist in the dataset.

First though, you need to pick the games from the 1980s, so I've used grep for that:

grep -E '^.{39}198.' games | sort -t'#' -k1.5,1.34

Output from your example:

3   Duck Hunt                     NES  1985  Nintendo            26.93
2   Super Mario Bros.             NES  1985  Nintendo            29.08
13  Super Mario Land              GB   1989  Nintendo            10.83
4   Tetris                        GB   1989  Nintendo            23.20
roaima
  • 107,089
  • 14
  • 139
  • 261