General :: Storing Array From Awk In Bash?

Nov 24, 2010

i'm using awk inside bash. i've got an array in awk called arrayinawk. everytime i call another awk command in bash i have to keep creating arrayinawk to work with it. is there anyway i can store arrayinawk in bash and just call the stored value next time i use awk?

View 14 Replies


ADVERTISEMENT

General :: Bash: Calculating In Array And Storing Result In Array?

Dec 2, 2010

I have an array called arrayini which stores numbers. I want to take log to the base 2 of each of the numbers in that array and put it in file called result. I've used the following code to do it.

Code:

size=${#arrayini[@]}
for ((i=0;i<size;i++))
do
echo "scale = 12; l(${arrayini[$i]})/l(2)" | bc -l
done >result

It works fine but its taking pretty long to calculate since I've got about 230,000 items in the array. So I decided to store the result into an array hoping that it'd be faster. I tried the following code. arrayresult is where I try and store the result. The code doesn't work because of the second last line.

Code:

unset arrayresult
size=${#arrayini[@]}
for ((i=0;i<size;i++))
do
arrayresult[$i]="scale = 12; l(${arrayini[$i]})/l(2)" | bc -l
done >FILE2

There is a syntax error clearly.

View 6 Replies View Related

General :: Bash + Reading Values (numbers) From A File And Storing Them Into An Array?

Nov 4, 2010

I have to read a couple of numbers from a random.txt file. In this .txt file there are random numbers. They are separated by a space. Example if you opened test.txt:

test.txt :1 6 1 3 6 8 10 2 4

I would like to read those numbers using CAT and store them into an array:

numlen=${#num[*]} - (must be like this because it is a part of a larger program)

View 5 Replies View Related

General :: Reading Numbers From Text File And Storing In Array?

May 26, 2010

I'm writing a bash script where I read a text file (containing a column of numbers) and store each line in an array. There seem to be some problems with the whole thing however, but only for some files and not others. Here's what I do:

Code:
#!/bin/bash
file=time_notOk.txt ### The file with a column of numbers
i=0 ### Array counter
### Read the file

[Code]....

View 3 Replies View Related

Software :: Bash - Indirect Array Reference To Array With Values Containing Spaces?

Jun 4, 2010

This _almost_ works. I just can't quite get it to honor the spaces.

Code:

#!/bin/bash
profiles=(
PROFILE_ONE
)
PROFILE_ONE=(setting1 "setting number 2")

[code].....

View 9 Replies View Related

General :: Bash: Reading File Into Array?

Dec 2, 2010

I have a file named file.txt with the following contents

Code:

19 man
24 house
44 dyam
90 random

I want to read the file into array and store each line in each index. I've tried using the following code.

Code:

dataarray=($( < file.txt ))

It stores each word in each index rather than each line in each index.

View 3 Replies View Related

General :: Bash Script - Two For Loops With Array?

May 1, 2011

I am trying to execute a .c program of mine through bash... Problem is it takes two arguments (files) and am trying to use "for" loops to do it. I use the for loops to take the files (same number in both directories) and put them into arrays. I use the command "ls -1" in the "for loop" so they are sorted in such a way that they are in the same time and date order upon when the program is executed.

Here is the code:

Code:

for a in `ls -1 /analyses/data1/*_prt.txt`;
do echo $a;
done

[code]....

HOWEVER, $a in this case does not change with the loop. Thus, the program doesn't work. I then tried:

Code:

for a in `ls -1 /analyses/data1/*_prt.txt`;
for sfc in `ls -1 /analyses/data2/*_ht.txt`;
do
./plot_data $a $b >> log_file.log;
done

But that produces the error: syntax error near unexpected token `for'. As a "do" statement must come after the for loop call.

View 13 Replies View Related

General :: Split A String Into Array In Bash?

Mar 17, 2011

how do I split a string into an array?In this string:"this is a story"how do I split it by the space?

View 8 Replies View Related

Programming :: Bash Array Add Function Example Using Indirect Array Reference As Function Argument?

Jun 20, 2010

I looked on the net for such function or example and didin't find anything, thus after having made one i guess it would be legitimate to drop it to see what others thinks of it.

#!/bin/bash
addelementtoarray()
{
local arrayname=$1

[code]....

View 10 Replies View Related

General :: Split A String Into Array In Bash Return Wrong Size?

Mar 21, 2011

I'm trying to split a string, to later iterate using a for loop like

Code:
for (( i=0; i<5; i++))

But, my script returns an array with the size 1.

Here's the script:

Code:
aver=$(grep "avg" A.txt | awk '{ print $2 }');
a=$(echo $aver | tr " " "
");

[Code]....

View 2 Replies View Related

General :: Bash Scripting - Executes A Few Perl Scripts - Create A New Array And Take The Hostname

Apr 4, 2011

I'm writing a bash script that executes a few perl scripts. One of the perl scripts that I need to execute requires two arguments with it. The arguments are stored in a txt file, each line contains a hostname and its corresponding IP address separated by a ":" (colon), the txt file looks like this below:

[Code]...

I'm not sure if it's the best way to accomplish this but here it goes. In the bash file, let's call it getHosts.sh, I create an array and assign each line of the file to an element in that array. I then think I need to create a new array where I take the hostname (which is before the ":") separate it from its IP address and place the IP address on a new line just below the hostname (this way I can reference to it like $hostNames[$x] would be the hostname, and $hostNames[$x+1] would be its IP address). So the new array would now look like this below:

[Code]...

View 4 Replies View Related

Programming :: Bash Array - More Than One Value Per Item?

Nov 28, 2010

Is it possible to have an array in Bash that can hold more than one value per item?

For example I would like an array like this:

Entry 1: apple, green
Entry 2: banana, yellow

And be able to call the fruit names and their colour in a list. Something like:

for fruit in "${array[@]}"
do
echo a $fruit is $colour
done

If that is possible, is there a limit to values per item? For example some entries in an array could be:

Entry 1: apple, green, round, pips, tree
Entry 2: banana, yellow, long, skin, tree

And I would like to pick out the values such as #3 being "round" and "long".

View 3 Replies View Related

Programming :: How To Copy Array In Bash

May 13, 2010

I have this code:

Code:
original_content=${content[@]};
echo ${#content[@]} # is 23
echo ${#original_content[@]} # is 1

I'd expect original_content should be copy of content. How to copy array?

View 2 Replies View Related

General :: Convert Full-disk RAID5 Array To Partition-based Array?

Dec 23, 2010

I have a RAID 5 array, md0, with three full-disk (non-partitioned) members, sdb, sdc, and sdd. My computer will hang during the AHCI BIOS if AHCI is enabled instead of IDE, if these drives are plugged in. I believe it may be because I'm using the whole disk, and the AHCI BIOS expects an MBR to be on the drive (I don't know why it would care).

Is there a way to convert the array to use members sdb1, sdc1 and sdd1, partitioned MBR with 0xFD RAID partitions?

View 1 Replies View Related

Programming :: Bash String Splitting Into Array?

Oct 14, 2010

I am using gnu bash 3.2I need to split the string into array like

a=this_is_whole_world.file # split [_.]
I need to split this on _ and . some thing like this
a[0]=this
a[1]=is
a[2]=whole
a[3]=world
a[4]=file

preferable using bash regex. if not sed is also ok.

View 2 Replies View Related

Programming :: Using An Array In Bash For Mutiple Paths?

Mar 29, 2011

I'm trying my hand at arrays in bash for a backup script. Now I not sure if this is the correct thing to do and just look at website and amended but does'nt work. Could someone tell me where I'm going wrong

LOG_DIR="/home/ops/Desktop/temp"
array[0]=3
array[1]=/home/ops/Desktop/dir1

[code]...

View 8 Replies View Related

Programming :: Bash - Read The Array Dynamically?

Sep 24, 2010

Code:

test=(1 2 3 4 5)
for car in ${test[@]}
do
echo "Element : $car"

[code]....

if variable $car equals 1, new element is added "6" to an array. But i don't know why when i am printing all elements of this array (echo "Element : $car") this element ("6") is not mentioned, but if i make a command which check an amount of contained elements by array it will be 6 elements.

View 14 Replies View Related

Programming :: Using Bash To Append A String To Array?

May 19, 2011

I have the following function that does not iterate through the array I want to be able to do some manipulation on each element in the array[@].it appears the below array has only one item in the array whereas i want the array to have 3 items hence the loop three times printing the message Any ideas why this is not happening ?

function foo() {
name =$1
array=( "$2" )

[code]...

View 5 Replies View Related

Programming :: BASH: Listing Contents Of .zip File Into An Array?

May 18, 2010

I want to list the contents of a zip file amd put each entry into an array. I've been doing

[code]....

View 4 Replies View Related

Programming :: Bash - Searching Strings For Array Elements?

Apr 30, 2010

...and returning the index of the found element in its array.

I have:

for ((i=0; i < ${#array1[@]}; i++)); do
# Read each line of the file test
if [[ $(eval "sed -n '$(($i+1))'p test") == *${array2[0]}* ]]
stuff

I want to find the index of the found substring in array2 and only if it isn't found, move on to the next element of array2. I don't know the size of array2 so that [0] has just got to go.

View 14 Replies View Related

Programming :: Bash: Split A Text File Into An Array?

Sep 18, 2010

I have a file (called twitterstatus.tmp) that looks like this:

Code:

<status>
<id>24854489768</id>
<text>Are we gonna ride the sun home?</text>
<id>55266987</id>

[code].....

How could I feed this into an array, with each element containing everything between the <status> </status> tags?

View 9 Replies View Related

Programming :: Bash To Loop Through Mysql Select Array?

May 26, 2011

I need to know how to assign a result from a select. I am clueless on the sytax. I am trying this in bash. Maybe I am not assigning the array right. It gives me the whole row in the echo instead of just field a. How do I get fieldA = a in the select. Note script was stripped for security on database info but the syntax is same.

Code:

#!/bin/sh
results="$(mysql --user ${DB_USER} -p${DB_PWD} ${DB_NAME} -Bse 'select a,b,c,d from tblMytable')"
for rows in "${results[@]}"
do
fieldA=${rows[0]};
echo ${fieldA};
done

View 2 Replies View Related

Programming :: Bash Unset Array Loops GARBAGE?

Jun 27, 2011

Code:

#loop until there are no more files
while [ ${#files[@]} -gt 0 ]; do
for num in `seq 1 ${#files[@]}`; do
wurd=`tail -4 ${files[$num-1]}`

[code].....

Everything works.. but when it has to unset.. it breaks.. it doesnt give an error though.. it just jams the program.

View 7 Replies View Related

Programming :: Bash - Reading Csv Delimited File To Array And For Further Manipulation?

Jan 6, 2010

I am trying to do this:

1. Read csv delimited file line1 and store all values in array

2. Use the values stored in the array and replace values in other text file with them

3. read line2 in the cvs file and repeat the process

4. Do above for all lines in the cvs file

for example:

file1.cvs content:

text1,text2,text3,"text 4"
a1,a2,3,"a 4"

file.txt content:

some text $array1$ some text
some text $array2$ some text

1. read line 1 - text1,text2,text3,"text 4" put each value in array X[] lines that contain spaces in cvs will have double quotes

2. read x[1] and replace value $array1$ (in file.txt) with x[1]read x[2] and replace value $array2$ (in file.txt) with x[2] and so on

Can above be accomplished in BASH and how?

View 1 Replies View Related

Programming :: Find A Substring/member In A String/array Using Bash?

Oct 13, 2010

This may be a basic bash array/string operation related question, but I couldn't find any direct answer. So here it goes:I have a lot of data sorted in various directories. All directories need same processing except for a special group of directories. I have a symbolic link of the script in discussion in each directory. I want the script to get the name of the current directory, check if that belongs to special group and do specific operations.So I get the name of the directory

Code:
mm=`basename `pwd``
Now the the group of directories that needs something different to be done, contains these

[code]...

View 4 Replies View Related

Programming :: Reading File Permissions Into Bash Array For Processing

Dec 1, 2010

I would like to read unix file permissions into a bash array for processing but tbh I have no idea how to do this. Then I will check for each individual access right l, d, x etc.

View 11 Replies View Related

Programming :: Creating Array From Command Output (Bash Shell Script)

Jan 26, 2011

I have a command that outputs n lines of text, and I want to place each line into an array element, but I can't seem to get the syntax correct

So my command is this:
cat $configfile | sed -n '/cluster:'$clustername'/,/cluster/ p' | awk /host/

Which produces many lines depending on the value of $clustername. I'd like to get each line as elements of an array.

View 5 Replies View Related

Programming :: Parsing Text Files In Bash / Cant Seem To Read The Array Back?

May 29, 2011

PI'm trying to write a script to list all open ports in the MINIUNPND chain in iptables and use the procotol, port and destination ip to open ports on another router using upnpc.Here is the output of iptables -L MINIUPNPD

Code:
>iptables -L MINIUPNPD
Chain MINIUPNPD (1 references)
target prot opt source destination
ACCEPT tcp -- anywhere 192.168.3.124 tcp dpt:19955
ACCEPT tcp -- anywhere 192.168.3.124 tcp dpt:20054
ACCEPT tcp -- anywhere 192.168.3.130 udp dpt:10654
ACCEPT tcp -- anywhere 192.168.3.121 tcp dpt:29955
code....

No matter what i do i cant seem to remove the first 4 characters from the MYPROT array to leave only the digits. Also i cant seem to read the array back???

I thought it would simply be a loop reading each line and passing the fields in variables, executing upnpc commands i need then moving to the next line of the file until it reached the EOF.

View 12 Replies View Related

Programming :: Writing Shell Snip To Import CSV Data Into Bash Array

Jan 5, 2011

I have been trying to write a simple snip of bash shell code to import from 1 to 100 records into a Bash array.

I have a CSV file that is structured like:
record1,item1,item2,item3,item4
record2,item1,item2,item3,item4
record3,item1,item2,item3,item4
record4,item1,item2,item3,item4

And would like to get this data into corresponding arrays as such:
$record1[item1-4]
$record2[item1-4]
$record3[item1-4]
$record4[item1-4]

View 9 Replies View Related

General :: Smartcards For Storing Gpg/ssh Keys?

Apr 14, 2010

I'm interested in storing my SSH keys and gpg keys on a smartcard for added security. However, I'm a bit uncertain on a few points, which are as follows:

How many keys can I get on a card? I assume both SSH and GPG can store keys on the card. Is there a limit to key size? I see a lot of cards saying they support 2048-bit keys, what about larger sizes? Hardware: can anyone recommend a card/reader combination that works well? I've done a fair amount of research and it seems PC/SC readers can be a bit iffy - is this your experience? Have I missed anything I should be asking? Are there any other hurdles?

I'm aware fsf europe give away cards with membership - I'm not sure I want to join, but... are these cards any good?

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved