Results 1 to 7 of 7
Hey I'm working on a script and need some help splitting a certain string into an array using awk's split function. I start off by reading the string into a ...
- 04-20-2009 #1Just Joined!
- Join Date
- Mar 2009
- Posts
- 31
splitting a string into an array using awk
Hey I'm working on a script and need some help splitting a certain string into an array using awk's split function. I start off by reading the string into a variable called str from a file. Once in the file I want to split each piece into an array. Each piece is separated by the phrase "replaceme" I've tried countless things but still cannot get it to work.
The syntax is
split(string,array,separator)My code is
awk '{split($str, array, "replaceme"}'but after I run that statement
echo $(#array)
the output is "0"
Any ideas on where I am going wrong and how to fix it?
Thanks,
John
- 04-21-2009 #2Linux User
- Join Date
- Aug 2006
- Posts
- 458
show the whole code you have. I think you are mixing shell variables with awk's
- 04-21-2009 #3Just Joined!
- Join Date
- Mar 2009
- Posts
- 31
#!/bin/bash
input=`cat inputfile | awk '{ print $2"replaceme" }'`
echo $input
echo $input | awk '{ split($0, arrg, "replaceme") }'
echo $(#arrg)
I'm thinking your are right about me mixing the variables. If this is the case, is there a way to pass the arrray out of awk?
- John
- 04-21-2009 #4Linux User
- Join Date
- Aug 2006
- Posts
- 458
show your input file and describe how you want the output to be.
- 04-21-2009 #5Just Joined!
- Join Date
- Mar 2009
- Posts
- 31
the input file is in this form
A file1
D ./folder/file
U ./folder2/infolder/file3
This is the general form. The U, D, M stand for whether the file was Added, Deleted, or Modified and then is followed by the file name including the partial path if it's not in the current directory. The number of files and what has been done to them is dynamic but it will always be in this general form. I would like the final form to be 2 arrays. The first containing the action to be done to the file. And the second array to contain the actual path to the file. Any ideas on how to do this?
Thanks,
John
- 04-22-2009 #6Linux User
- Join Date
- Aug 2006
- Posts
- 458
i suggest you use a tool that provides what you call dictionaries or hashes.
eg you can use associative arrays in awk to store your data. See my sig to learn awk
if you know Perl(hashes) or Python(dictionary), they have the same feature. look up the docs on them to know more.Code:awk ' { array[$2]=$1 } END{ for(i in array) { print array[i],i #do something } } ' file
- 04-22-2009 #7Just Joined!
- Join Date
- Mar 2009
- Posts
- 31
Is there anyway to read this file and store the values in 2 arrays and have those arrays accessible to the rest of my program controlled by bash instead of being stuck inside awk?
-John


Reply With Quote