Sponsored Content
Full Discussion: Reading a file into an array
Top Forums UNIX for Dummies Questions & Answers Reading a file into an array Post 302336068 by SasankaBITS on Tuesday 21st of July 2009 06:16:31 AM
Old 07-21-2009
Reading a file into an array

I have a file that is a text file, how to get all the words into and array, i am able to get each line but not each word Smilie.

Here is what i searched and already found...https://www.unix.com/shell-programmin...nto-array.html.

This one reads a whole line into variable filename[*], i need each word to be in filename[*], How do i do this.
Thank you in advance
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash: Reading out rows of a file into a dynamic array and check first literal

Hello, i have a file "Movie.ini" looking e.g. like follows * MOVIE A bla bla MOVIE B blubb blubb MOVIE C I'd like to read the file "Movie.ini" with cat and grep and check whether it includes the string MOVIE only with a '*' at the beginnig. By doing "cat Movie.ini| grep MOVIE... (14 Replies)
Discussion started by: ABE2202
14 Replies

2. Shell Programming and Scripting

Reading from array

Hi, I have an array like below... Table="table1" Table="table2" Table="table3" Table="table4" ..... Table="tablen" I want to retireve the values from the array and need to pass this to a db2 command to create a view like below. the number of values in the array will vary ... (1 Reply)
Discussion started by: ratheeshjulk
1 Replies

3. Programming

Reading Scientific notation from file and storing in array

Hi, I am trying to read a set of numbers that are in scientific notation into a file so I can do some math on them, but when I display the array contents the numbers aren't the same as the numbers in the file. Could someone explain why? Thanks. int main() { double fArray; ... (3 Replies)
Discussion started by: Filter500
3 Replies

4. Shell Programming and Scripting

Reading from a file and assigning to an array in perl

I wrote a simply perl that searched a file for a particualr value and if it found it, rite it and the next three lines to a file. Now I have been asked to check those next three lines for a different value and only write those lines if it finds the second value. I was thinking the best way to... (1 Reply)
Discussion started by: billprice13
1 Replies

5. Shell Programming and Scripting

reading data from a file to an array

I need some help with this code below, i doesnt know why it will run twice with my function, but my function only got if else, any other way that can read line and put into array? while read line; do read -A array <<<$line n=${#array} for ((i=1;i<$n;i++)); do print... (1 Reply)
Discussion started by: gavin_L
1 Replies

6. Shell Programming and Scripting

Help with reading column in array

I have some version problem to use this code in my server while read line; do read -A array <<<$line <---------- the server dont read <<< n=${#array} for ((i=1;i<$n;i++)); do print "${array}" done func=${array} data1=${array} data2=${array} ... (1 Reply)
Discussion started by: gavin_L
1 Replies

7. Shell Programming and Scripting

Reading columns from a text file and to make an array for each column

Hi, I am not so familiar with bash scripting and would appreciate your help here. I have a text file 'input.txt' like this: 2 3 4 5 6 7 8 9 10 I want to store each column in an array like this a ={2 5 8}, b={3 6 9}, c={4 7 10} so that i can access any element, e.g b=6 for the later use. (1 Reply)
Discussion started by: Asif Siddique
1 Replies

8. Shell Programming and Scripting

Reading a file into array

Hi, I need to read a file into array and print them in a loop:- 1st file :-cat a.txt RC1 RC2 RC3 RC4 My Program:- #!/bin/ksh index=0 while do read cnt<a.txt print "cnt value is ${cnt} index=`expr $index + 1` done Code tags for code, please. (5 Replies)
Discussion started by: satishmallidi
5 Replies

9. Shell Programming and Scripting

Reading a file into an array

Hi I have a file with contents as below : server | ABC Issue : File System Missing XYZ Issue : Wrong Syntax PQR Issue : Old File to be removed Now I am looking for an o/p similar to server <tab> ABC Issue : File System Missing <tab> XYZ Issue : Wrong Syntax <tab>... (4 Replies)
Discussion started by: deo_kaustubh
4 Replies

10. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies
PREG_MATCH(3)								 1							     PREG_MATCH(3)

preg_match - Perform a regular expression match

SYNOPSIS
int preg_match (string $pattern, string $subject, [array &$matches], [int $flags], [int $offset]) DESCRIPTION
Searches $subject for a match to the regular expression given in $pattern. PARAMETERS
o $pattern - The pattern to search for, as a string. o $subject - The input string. o $matches - If $matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on. o $flags -$flags can be the following flag: o PREG_OFFSET_CAPTURE - If this flag is passed, for every occurring match the appendant string offset will also be returned. Note that this changes the value of $matches into an array where every element is an array consisting of the matched string at offset 0 and its string offset into $subject at offset 1. o $offset - Normally, the search starts from the beginning of the subject string. The optional parameter $offset can be used to specify the alternate place from which to start the search (in bytes). Note Using $offset is not equivalent to passing substr($subject, $offset) to preg_match(3) in place of the subject string, because $pattern can contain assertions such as ^, $ or (?<=x). Compare: <?php $subject = "abcdef"; $pattern = '/^def/'; preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3); print_r($matches); ?> The above example will output: Array ( ) while this example <?php $subject = "abcdef"; $pattern = '/^def/'; preg_match($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE); print_r($matches); ?> will produce Array ( [0] => Array ( [0] => def [1] => 0 ) ) RETURN VALUES
preg_match(3) returns 1 if the $pattern matches given $subject, 0 if it does not, or FALSE if an error occurred. Warning This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.3.6 | | | | | | | Returns FALSE if $offset is higher than $subject | | | length. | | | | | 5.2.2 | | | | | | | Named subpatterns now accept the syntax | | | (?<name>) and (?'name') as well as (?P<name>). | | | Previous versions accepted only (?P<name>). | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 Find the string of text "php" <?php // The "i" after the pattern delimiter indicates a case-insensitive search if (preg_match("/php/i", "PHP is the web scripting language of choice.")) { echo "A match was found."; } else { echo "A match was not found."; } ?> Example #2 Find the word "web" <?php /* The  in the pattern indicates a word boundary, so only the distinct * word "web" is matched, and not a word partial like "webbing" or "cobweb" */ if (preg_match("/web/i", "PHP is the web scripting language of choice.")) { echo "A match was found."; } else { echo "A match was not found."; } if (preg_match("/web/i", "PHP is the website scripting language of choice.")) { echo "A match was found."; } else { echo "A match was not found."; } ?> Example #3 Getting the domain name out of a URL <?php // get host name from URL preg_match('@^(?:http://)?([^/]+)@i', "http://www.php.net/index.html", $matches); $host = $matches[1]; // get last two segments of host name preg_match('/[^.]+.[^.]+$/', $host, $matches); echo "domain name is: {$matches[0]} "; ?> The above example will output: domain name is: php.net Example #4 Using named subpattern <?php $str = 'foobar: 2008'; preg_match('/(?P<name>w+): (?P<digit>d+)/', $str, $matches); /* This also works in PHP 5.2.2 (PCRE 7.0) and later, however * the above form is recommended for backwards compatibility */ // preg_match('/(?<name>w+): (?<digit>d+)/', $str, $matches); print_r($matches); ?> The above example will output: Array ( [0] => foobar: 2008 [name] => foobar [1] => foobar [digit] => 2008 [2] => 2008 ) NOTES
Tip Do not use preg_match(3) if you only want to check if one string is contained in another string. Use strpos(3) or strstr(3) instead as they will be faster. SEE ALSO
PCRE Patterns, preg_quote(3), preg_match_all(3), preg_replace(3), preg_split(3), preg_last_error(3). PHP Documentation Group PREG_MATCH(3)
All times are GMT -4. The time now is 11:39 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy