0

I am new to shell scripting and what I need is to read from a file that contains a 2d array. Assume there is a file named test.dat which contains values as:

- Paris         London     Lisbon
- Manchester    Nurnberg   Istanbul
- Stockholm     Kopenhag   Berlin

What is the easiest way to select an element from this table in linux bash scripts? For example, the user inputs -r 2 -c 2 test.dat that implies to selecting the element at row[2] and column[2] (Nurnberg).

I have seen the read command and googled but most of the examples were about 1d array.

This one looks familiar but could not understand it exactly.

Community
  • 1
  • 1
iso_9001_
  • 2,655
  • 6
  • 31
  • 47

1 Answers1

1

awk is great for this:

$ awk 'NR==row{print $col}' row=2 col=2 file
Nurnberg
  • NR==row{} means: on number of record number row, do {} Number of record normally is the number of line.
  • {print $col} means: print the field number col.
  • row=2 col=2 is giving both parameters to awk.

Update

One more little question: How can I transform this into a sh file so that when I enter -r 2 -c 2 test.dat into prompt, I get to run the script so that it reads from the file and echoes the output? – iso_9001_.

For example:

#!/bin/bash

file=$1
row=$2
col=$3

awk 'NR==row{print $col}' row=$row col=$col $file

And you execute like:

./script a 3 2
Kopenhag
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • 1
    Great! I was suspecting awt would work :) Thank you very much – iso_9001_ Jan 15 '14 at 11:30
  • `awk` is sooooo powerful for this kind of things :) Glad it worked to you! – fedorqui Jan 15 '14 at 11:32
  • One more little question: How can I transform this into a sh file so that when I enter `-r 2 -c 2 test.dat` into prompt, I get to run the script so that it reads from the file and echoes the output? – iso_9001_ Jan 15 '14 at 14:31
  • You are really fast and have been very helpful :) thank you very much – iso_9001_ Jan 15 '14 at 14:40
  • Today, I have successfully managed to write the code as you suggested. So, thanks again. What I now lack is, I can manage to run the code as `./tablooku.sh tablom.dat 2 2` but when I try to parameterize "2 2" part as `./tablooku.sh tablom.dat -r 2 -c 2` I get my error message that says "Error: -rth row and -cth column does not exist". How can I parameterize the command? – iso_9001_ Jan 16 '14 at 13:14
  • Well, you do not have to indicate `-r 2 -c 2`. Instead, just `2 2`, like I comment in the last part of my answer: `./tablooku.sh tablom.dat 2 2` would be enough. – fedorqui Jan 16 '14 at 13:16
  • Yes, I know that it is enough. Besides I managed to run the code as `./tablooku.sh tablom.dat 2 2` through your instructions. But what I am curious is how to parameterize the input. Is there a simple way? – iso_9001_ Jan 16 '14 at 13:49
  • Ah, I see. Then you may consider using `getopts`. You can check http://wiki.bash-hackers.org/howto/getopts_tutorial and also questions like http://stackoverflow.com/q/402377/1983854 – fedorqui Jan 16 '14 at 13:52
  • @fedroqui Great.! :) Thanks, I will check these links now. – iso_9001_ Jan 16 '14 at 13:55