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
ADVERTISEMENT
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
Feb 18, 2011
I have a large text file with three columns. I'm trying to write a PERL script that splits the file up based on the value of the 3rd column. So every time the third column reads 0, a new file is created and all the data up until the next 0 is found is written to that new file. This should happen over and over until the initial file has been entirely split up.
ex data:
000
2024
2243
2143
96962871
97972878
000
2034
3034
3352
So with the data above, the file would be split into two files:
data_1.txt would contain
000
2024
2243
2143
96962871
97972878
and data_2.txt would contain
230
2034
3034
3352
View 10 Replies
View Related
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
Oct 20, 2010
I know that this is a really simple thing to do but I just can't figure it out. How do I read a text file into an array in C++?
View 3 Replies
View Related
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
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
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
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
Mar 29, 2011
Here is the issue. I am reading in a outside text file and putting in the string on that line into a char array that is already allocated.
Code:
int main(int argc, char *argv[])
{
[code]....
View 1 Replies
View Related
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
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
Nov 8, 2010
I need to Read a path of a file witch is written in Text file i used this
Code:
FILENAME=$1
while read line
do
echo $line
done < $FILENAME
it worked and showed me the Line witch was written in my file but now my problem is how am gonna use that line as a path i mean for example if am gonna execute a linux command on that file like dpkg -i /path/to/the/file how am gonna export it from The $Line variable and use it after the command.
View 14 Replies
View Related
Apr 30, 2010
I have a text file which stores the list of files & dir, I want to get only file's extensions from this file & want to store it in another file.eg, below is the file's contents & from it I want to get the extensions sh, pl & h & want to store it in another file. Also I don't want directory list.
A scripts/services_restarter.sh
A scripts/svn post_commit scripts
A scripts/tmp/
[code]...
View 8 Replies
View Related
Feb 15, 2011
At the moment I got my md5sum checking working which I write to a text file and see below.
If the md5sum works it will write the output to check2.md5 test.txt: OK
If the md5sum fails it will write test.txt: FAILED
How do I write if statement to check the output whether or not the md5sum failed or not ?
check1="/home/ops/Desktop/test1/check1.md5"
check2="/home/ops/Desktop/test1/check2.md5"
cd /home/ops/Desktop/test1
md5sum test.txt > $check1
[Code]....
View 2 Replies
View Related
Apr 17, 2011
write such script (bash script). I have some text file with name filename.txt I must check if this file contains string "test-string-first", I must cut from this file string which follows string "keyword-string:" and till first white-space and save it to some variable.
For example. File: PHP Code: PHP Code:
Start 15022011 Eng 12-3-42
SN1232324422 11 test-string-first
SN322211 securities
HH keyword-string:123456321-net mark (11-22)
[Code].....
View 1 Replies
View Related
Mar 11, 2011
For example, I have a text file with data which lists numerical values from two separate individuals
Code:
Person A
100
200
300
400
500
600
700
800
900
1000
1100
1200
Person B
1200
1100
1000
900
800
700
600
500
400
300
200
100
How would I go about reading the values for each Person, then being able to perform mathematical equations for each Person (finding the sum for example)?
View 13 Replies
View Related
Nov 17, 2010
I have a script which checks on my jobs that run on some cluster.The script, "script.sh", takes as an input the job-id for the job to be checked. Sometimes I have 100s of jobs and I want to check them all (for successful completion.) I could put these job-ids into a text file, "job-id.txt", each id in its own line.For each job-id, the script would ask me few questions (with a yes or no answers) to see if I want to do some other checks for each job-id.I want to know how may I direct my job-ids from this text file into the script one job-id at a time.
View 14 Replies
View Related
Aug 11, 2009
Is there a way to process individual characters one-by-one from a text file in Bash, or is that hoping for a little too much from this lovable old clunker?
View 13 Replies
View Related
Jul 13, 2010
I am trying to think of a logic where my file contains some data I had to read and do some processing. Issue is that file contains data multiple times. For example:
:::::::::::
var1=value1
var2=value2
[code].....
I have to read first paragraph of variables and do some processing and then move on until the end of file. Variable names are same in whole file but for each paragraph the value is different. I can't think of a logic to attain this task. How can I do it? It should be a simple bash script, but I am not able to work out.
View 2 Replies
View Related
Nov 30, 2009
I've already used line split stuff to transform my data into something like this in a text file:
Code:
['1', '1', '3', '20.7505207']
['2', '1', '3', '23.0488319']
['3', '1', '3', '-1.5768747']
['4', '1', '3', '-26.4772491']
[code]....
How can I get this on a python program so I can manipulate it as an array?
View 3 Replies
View Related
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
May 2, 2010
i am on processing text tasks And i found that if you assign a text to a variable is chomp'ed automatically the newline
Code:
variable=$(cat file.txt)
The problem is i can only access the items/lines using:
Code:
for line in $variable
do
echo $line
# Other commands
done
how do i convert this to an indexed array. More importantly, how do i get access to individual $line[0], ..., $line[n] Another thing, if the file.txt, has lines with spaces it is a mess using the for...in..., but echoing prints line by line...o_0
View 11 Replies
View Related
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
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
Jul 7, 2011
bash 3.1.17(2) I'm trying do write a shell script which must operate on each line of an ASCII text file. So, all the code must be inside a loop, and inside the loop, the first thing should be to read the next line from the file. I have the bash read command. But it reads from stdin. Any way to make read from a file?
View 6 Replies
View Related
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
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
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
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