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.
awk
is great for this:NR==row{}
means: on number of record numberrow
, do{}
Number of record normally is the number of line.{print $col}
means: print the field numbercol
.row=2 col=2
is giving both parameters toawk
.Update
For example:
And you execute like: