Load variables into an array
this is a little difficult to explain in the subject line so if its confusing im srry. but basically I have a file that has some information in it.
Quote:
Quote:
Quote:
Quote:
|
Let's start by putting the code in CODE tags and indenting it to make it more readable:
Code:
#!/bin/bash Code:
#!/bin/bash |
You should try catkin's codes, your code is too complicated.
|
Quote:
Basically in my example it is already in order and it is in what I'm trying to accomplish ultimately. But the ultimate goal is to read each item into a variable, or should I say line I grep for, then be able to place those where I need. Honestly I realise my code again is caveman like but as I already said I'm trying to learn and understand. |
do you really want to do it in bash? using perl or awk will make it much easier.
|
Quote:
|
both can handle associative arrays, something like this:
var[name] = value "name" and "value" can be text and you can implement any kind of sort function |
Quote:
My suggestion could be adapted to load the data into variables. Don't know exactly what you want so this is only an idea to parse out what you might want from the lines: Code:
#!/bin/bash |
Quote:
|
To load a seqence into an array, simply use the arr+=() syntax. Every "word" inside the brackets will be added to the array as a new element.
Alternately you can use an incrementing counter variable. Code:
#!/bin/bash http://mywiki.wooledge.org/BashFAQ/0...al_newlines.21 You'll have to either ensure that the file is formatted properly first, or process the last variable value separately afterwards. |
Quote:
|
By the way, here's another option for you.
bash's new (v.4+) mapfile command, and process substitution, can be used to store the output lines of a command into an array. You can also use process substitution for things like running the array through sort. Code:
mapfile -t adapter < <( grep '^adapter' input.txt ) |
actually I realised I was making my process way to confusing and went much simpler. Basically removed the array idea and went to the basic layout you had and output the way I need. The reason I thought I needed an array is because I was putting the data in xml tags. Which I later realised was not very sound and caused lots of issue. Simply outputting $line from the read between the tags worked beautifully.
Lesson learned stick to simple stupid. |
All times are GMT -5. The time now is 01:55 PM. |