Bash Scripting - How to grep a file into an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Scripting - How to grep a file into an array
# 1  
Old 09-17-2010
Bash Scripting - How to grep a file into an array

I have figured out how to grep the file like this:
Code:
echo `grep $(date +'%Y-%m-%d') Cos-01.csv | cut -d',' -f1`

The above line does echo the correct information from the lines in which my search criteria is found.
Now I am trying to get that information (Yes, just one column of every line) into an array.

I would like each line to have a place in the array automatically.

EXAMPLE:
Code:
MyArray[0]=First Line
MyArray[1]=Second line etc.

I have been trying everything that I could think of and I can't get my array populated.
Is there a way to do the above? grep line by line into an array without a loop?

I wish to thank everyone in advance for their assistance.[/HTML]

---------- Post updated at 12:07 PM ---------- Previous update was at 11:42 AM ----------

I have tried doing this:

Code:
MyArray=(`grep $(date +'%Y-%m-%d') Cos-01.csv | cut -d',' -f1`)
echo {$MyArray[1]}

It seems that it is just writing each line into the variable MyArray and the last line is the one that is left. I know that I am missing something really simple but I can't seem to figure it out.

Last edited by Scott; 09-17-2010 at 02:34 PM.. Reason: Please use code tags
# 2  
Old 09-17-2010
Check this link: Arrays

Try this:
Code:
MyArray=(`grep $(date +'%Y-%m-%d') Cos-01.csv | cut -d',' -f1`)
echo {$MyArray[*]}

This User Gave Thanks to felipe.vinturin For This Post:
# 3  
Old 09-17-2010
Thank you very much felipe.vinturin. The only thing that changed was it still lists only the last line that was grep'd but with a * in the brackets instead of a 1.

I also thank you for the link, I have been there and that is what got me up to the point that I am at now. I will keep looking over the link that you posted to see if I can figure it out while I wait for other replies.
# 4  
Old 09-17-2010
If you could post part of your file, the result you have now and the expected result, a sample of your problem, then it would be easier to help you! =o)
This User Gave Thanks to felipe.vinturin For This Post:
# 5  
Old 09-17-2010
Contents of the Cos-01.csv file:

Code:
Adam, 2010-09-17 13:16:21, 2010-09-17 13:16:25,  7,  54, No , Yrd,   , -80,        6,        0,   0.  0.  0.  0,   7, Paid, 
John, 2010-09-17 13:16:21, 2010-09-17 13:16:26,  2,  54, No , Yrd,   , -69,       13,        0,   0.  0.  0.  0,   7, Paid, 
Brad, 2010-09-17 13:16:22, 2010-09-17 13:16:26,  9,  54, No, Yrd,    , -64,       15,       15,   0.  0.  0.  0,   4, Paid,

Expected Output
Code:
Adam <--would be in MyArray[0]
John <--would be in MyArray[1]
Brad <--would be in MyArray[2]



---------- Post updated at 12:35 PM ---------- Previous update was at 12:33 PM ----------

One thing that I did leave out is that I did declare my array:

Code:
declare -a MyArray

I don't know if that was taken into account.

---------- Post updated at 12:37 PM ---------- Previous update was at 12:35 PM ----------

The end goal is to have each column in their own arrays.

Code:
First column = MyArray
Second column = Date
etc

Moderator's Comments:
Mod Comment Please use code tags

Last edited by Scott; 09-17-2010 at 02:48 PM..
# 6  
Old 09-17-2010
It works fine for me:
Code:
# cat TestFile.txt
Adam, 2010-09-17 13:16:21, 2010-09-17 13:16:25, 7, 54, No , Yrd, , -80, 6, 0, 0. 0. 0. 0, 7, Paid,
John, 2010-09-17 13:16:21, 2010-09-17 13:16:26, 2, 54, No , Yrd, , -69, 13, 0, 0. 0. 0. 0, 7, Paid,
Brad, 2010-09-17 13:16:22, 2010-09-17 13:16:26, 9, 54, No, Yrd, , -64, 15, 15, 0. 0. 0. 0, 4, Paid,
# MyArray=(`grep $(date +'%Y-%m-%d') TestFile.txt | cut -d',' -f1`)
# echo ${MyArray[*]}
Adam John Brad

There was a typo in my last post.
Code:
# It was:
echo {$MyArray[*]}

# Must be:
echo ${MyArray[*]}

This User Gave Thanks to felipe.vinturin For This Post:
# 7  
Old 09-17-2010
Thank you very much felipe.vinturin. It does work perfectly. I was so close yet so far away.
It is amazing how a couple of () and a $ in the wrong place will break even the best scripts. Smilie

I did notice that someone made my posts all nice and neat. I would like to thank them also. This was my first post and I will do better in my future posts.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert String to an Array using shell scripting in JSON file.

This is the sample json I have pasted here. I want all the IP address strings to be converted into an array. For example "10.38.32.202" has to be converted to everywhere in the JSON. There are multiple IPs in a JSON I am pasting one sample object from the JSON. But the IPs already in an Array... (11 Replies)
Discussion started by: vinshas1
11 Replies

2. UNIX for Beginners Questions & Answers

Bash to append array value to file before copying

The bash stores each uniqueid in an array and then passes them to %q to get the unique path. That seems to work what I am having trouble with is renaming each .png with the unique value in %q. I thought it was working but upon closer inspection, a .png file is being sent to scp.... but only 1 and... (21 Replies)
Discussion started by: cmccabe
21 Replies

3. OS X (Apple)

Create a bash array from a flat file of whitespaces only.

Hi guys and gals... MacBook Pro. OSX 10.13.2, default bash terminal. I have a flat file 1920 bytes in size of whitespaces only. I need to put every single whitespace character into a bash array cell. Below are two methods that work, but both are seriously ugly. The first one requires that I... (7 Replies)
Discussion started by: wisecracker
7 Replies

4. UNIX for Beginners Questions & Answers

Unset array element and save to file in Bash

#!/bin/bash X=(2H 4S 10D QC JD 9H 8S) How do I unset the 10D from this array and save it to a file? Please use CODE tags as required by forum rules! (5 Replies)
Discussion started by: cogiz
5 Replies

5. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

6. Shell Programming and Scripting

Comparison of two file using bash scripting

I need following code in linux bash: Step 1: run a command and create file1 run a command and create file2 compare file1 and 2 if any difference go to step 1 (keep trying 4 times, if no success, abort program) else do nothing and continue program. now I'm... (2 Replies)
Discussion started by: kashif.live
2 Replies

7. UNIX for Dummies Questions & Answers

Passing values from file into array in Bash

Hi, I'm trying to write a bash script that takes a file and passes each line from the file into an array with elements separated by column. For example: Sample file "file1.txt": 1 name1 a first 2 name2 b second 3 name3 c third and have arrays such as: line1 = ( "1" "name1" "a"... (3 Replies)
Discussion started by: ShiGua
3 Replies

8. Shell Programming and Scripting

Most reliable way to store file contents in an array in bash

Hi Guys, I have a file which has numbers in it separated by newlines as follows: 1.113 1.456 0.556 0.021 -0.541 -0.444 I am using the following code to store these in an array in bash: FILE14=data.txt ARRAY14=(`awk '{print}' $FILE14`) (6 Replies)
Discussion started by: npatwardhan
6 Replies

9. Shell Programming and Scripting

[bash] redirect (save) array to a file

Hi, I'm trying to write a function that redirects the contents of an array to a file. The array contains the lines of a data file with white space. The function seems to preserve all white space when redirected except that it seems to ignore newlines. As a consequence, the elements of the... (7 Replies)
Discussion started by: ASGR
7 Replies

10. Shell Programming and Scripting

Bash: Zeilen aus Datei mit cat und grep in dynamisches Array schreiben

Hallo, ich habe eine Datei "Kino.ini" die z.B. wie folgt aussieht * KINOFILM A bla bla KINOFILM B blubb blubb KINOFILM C Ich möchte nun die Datei "Kino.ini" per cat und grep auslesen und testen ob der String KINOFILM nur mit einem '*' am Anfang vorkommt. In dieser Beispieldatei... (3 Replies)
Discussion started by: ABE2202
3 Replies
Login or Register to Ask a Question