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


ADVERTISEMENT

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

General :: Return Part Of A String In Bash?

Mar 20, 2011

I would like to return the last part of a string in an array of strings in bash.

The array contains in each position the content below:

Code:
a.b.c
a.d.f
a
a.d

[Code].....

View 5 Replies View Related

Programming :: Split String Into An Array/list Of Lines In C++?

Jun 4, 2010

How can i split a string like this,

Code:
This is my first line.
This is my second line.
This is my third line.
into,

Code:
"This is my first line.", "This is my second line.", "This is my third line."
in C++.

i.e. split the code at every new line

View 11 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 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 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 :: 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

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 :: Accessing Arrays Position In A Split String?

Mar 10, 2011

I've a string "this.is.a.name", and I would like to put it in an array. But, I've like to print the output of the array as:

Code:
echo ${array[0]}
echo ${array[1]}
echo ${array[2]}
echo ${array[3]}
I've tried with

[Code]....

View 4 Replies View Related

General :: Split A Gzip File Based On Size And Have A Prefix Without Changing Extension?

Jan 18, 2010

have a gzip file ABC_000023232.gzipBCD_023232032.gzipI want to split these files into smaller files but keep the extension same because I am using this as a variable in a script

Code:
for i in `ls *.gzip`
split -b 500K $i $i

[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

Programming :: Split A String By Every Nth Characters?

Jan 17, 2011

splitting a string by every nth characters. I'm using Python 2.7.1 because I'm using older libraries, if that matters.

For example, if this is my input:

Quote:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ac elit nibh, vitae venenatis ligula. Vestibulum a varius turpis.

Splitting it by every 10th character should produce this list:

Quote:

[ "Lorem ipsu", "m dolor si", "t amet, co", "nsectetur ", "adipiscing", " elit. Sed", " ac elit n", "ibh, vitae", " venenatis", " ligula. V", "estibulum ", "a varius t", "urpis." ]

View 6 Replies View Related

Programming :: Split Fields In A Perl Array?

Dec 16, 2010

I have an array called @logons. How can I step thru the array and split the fields? This is what I have so far, but doesnt work. I got the feeling I the split statement syntax is incorrect.

Code:

print @logons;
foreach my $logons(@logons){
($userid, $ip) = split(',',$logons);

[code]....

Update: Appears the data in @logons has a column header from the mysql query which I used to populate it with. So that code which I was testing does indeed work.

View 10 Replies View Related

Ubuntu :: GREP - Return Only Search String?

Jan 13, 2011

I have a mail.log file, of which I want to redirect only the search strings of the sender from=<example.sender@exampledomain.com> and the size size=4537 to a file.

In every case the sender string starts as from=<> and the size string starts as size=

What would be the grep command to redirect only the two search strings to a .txt file?

View 2 Replies View Related

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 View Related

General :: Get Part Of A String In Bash ?

Mar 10, 2011

I've a string "this.is.a.name", and I would like to return "is.a.name". How can I do that in bash?

View 3 Replies View Related

Programming :: PHP MySQL Array Return Unexpected Data

Nov 8, 2010

I am trying to build an array with the column headers and the data like so:

PHP Code:
function query($query) {
$data = array();
$q = "$query";
$this->log->message("Manual query [$q], returning columns...");
$result = @mysqli_query($this->dbc, $q);
$this->selected_rows = mysqli_num_rows($result);

while($meta = mysqli_fetch_field($result)) {
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "meta [{$meta->name}]"; $data[$meta->name][] = $row[$meta->name]; } }
echo "DATA ["; print_r($data); echo "] <br />";
return $data; }

So I can select the data from the array like so:
PHP Code:
echo $data[col_name][row_num];

However when I dump the array I dont get the data I am expecting:
Code:
running select * from users:
meta [company_name]meta [company_name]
DATA [Array ( [company_name] => Array ( [0] => My company [1] => My details ) ) ]

View 6 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 :: Parse String In Bash Script?

Nov 8, 2009

I have to create a bash script that takes an arbitrary length number from the command line, and add up each individual digit

Ex:
server> myscript.sh 123
server> 1 + 2 + 3 = 6

The problem I'm having is pulling out each character.

Is there a way in bash I can parse the input string for each character? I can't figure out a way to do this.

View 4 Replies View Related

Ubuntu :: Split Big File Size Into Small File Size

Jun 10, 2010

Are there software that can split big file size into small file size in Linux?

View 1 Replies View Related

General :: Environmental Variable In Bash Command String?

Feb 9, 2011

I do this:

Code:

a@b:~$ export A=hi
a@b:~$ echo $A
hi
a@b:~$ bash -c "export A=blah; echo $A"
hi
a@b:~$

Why doesn't the bash command print the new value of $A? Is there a way to make it do so?

View 6 Replies View Related

General :: Evaluation Of Postfix String Using Bash Shell

Mar 15, 2011

I would like to evaluate a postfix string using bash shell script,but I do not know how to start.

View 1 Replies View Related

General :: Change Case Of A String (BASH Scripting)?

Jul 9, 2010

Is there any inbuilt functionality in Unix shell script so that i can able to convert lower case string input to an upper case? I dont want to use high level languages like java,python or perl for doing the job.

View 4 Replies View Related

General :: Pull String From A File Into A Variable Using Bash

Aug 6, 2011

I have a file (.tmpfile) and inside it is a string which i only know part of, the rest being a random group of characters... I would like to know how to pull the whole string out of the file and into a variable.

View 13 Replies View Related

Programming :: Getting String Value From Array?

Oct 29, 2009

I've got a problem...

a[0] = 22
echo $[a[0]]
echoes 22 - that works fine

[code]...

View 5 Replies View Related

Programming :: Debian Serial Port Read() Return Wrong Data?

Jul 6, 2010

I am implementing a simple serial protocol where my ARM9 board, running Linux is communicating to a slave peripheral board. The Master sends a 12 byte data stream and the peripheral board returns status in a 23 byte response. The serial port is opened in raw mode. It works perfectly on 44 reads; however, on the 45 read the data returned from the read() is incorrect. I've framed what's being sent on an oscope and it is correct.The coincidence is that 23 x 44 = 1012. It's as though the receive buffer is 1K and when I go past the boundary I get bad data. The read following the bad one is good again.I've tried flushing the buffer before reading but get the same result.Here's the port initialization code:

Code:
int initport(int fd)
{

[code]...

View 4 Replies View Related

General :: Check If Any Of The Parameters To A Bash Script Match A String?

Sep 8, 2010

I'm trying to write a script where I want to check if any of the parameters passed to a bash script match a string. The way I have it setup right now is if [ "$3" != "-disCopperBld" -a "$4" != "-disCopperBld" -a "$5" != "-disCopperBld" -a "$6" != "-disCopperBld"]but there might be a large number of parameters, so I was wondering if there is a better way to do this?EDIT:I tried this chunk of code out, and called the script with the option, -disableVenusBld, but it still prints out "Starting build". Am I doing something wrong?

while [ $# -ne 0 ]
do
arg="$1"

[code]....

View 3 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







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