Results 1 to 5 of 5
This is all in ksh.
I am trying to extract variables from stdout and assign them to an array.
So I have a block of text:
Code:
Display (ABC) Test
...
- 08-28-2008 #1Just Joined!
- Join Date
- May 2008
- Posts
- 14
Grep/awk text for keyword
This is all in ksh.
I am trying to extract variables from stdout and assign them to an array.
So I have a block of text:
What I want to do is grab 'ABC' and 'XYZ' and put them in an array.Code:Display (ABC) Test Display (XYZ) Test2
So far I am using <command> | grep Display
I seem to remember that there is an easy way to do this with awk. Any suggestions?
- 08-28-2008 #2Just Joined!
- Join Date
- Feb 2006
- Posts
- 7
U can just try out with this, if this is within a text file
cat x | grep ABC | awk '{ print $Num }'
where x-stands for filename, ABC-stands for string or word u want to grep and $Num-stands for "ABC" word count in a file.
OR
grep ABC or XYZ filename | awk '{ print $Num }'
where ABC or XYZ -stands for the word u wanted to grep, filename-stnads for file which contains that word and Num-stands for ABC or XYZ word count
By
Basu
- 08-30-2008 #3Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
I'm not sure I understand you - does Num contain the field number (set in the calling shell)? If so, the single quotes around the awk argument will prevent it from being substituted.
I'd use sed for this problem:
I'll leave you to figure out how to load them into an array.Code:sed -n 's/Display.*(\([^)]*\)).*/\1/p' filename
- 08-30-2008 #4Linux User
- Join Date
- Aug 2006
- Posts
- 458
depending on what you want to do with those variables, you can do processing inside awk itself
or if you want to export those variables out to your shell scriptCode:# awk 'BEGIN{FS="[()]"}{print $2}' file ABC XYZ
Code:# result=$(awk 'BEGIN{FS="[()]"}{print $2}' file) # echo $result ABC XYZ # set -- $result # echo $1 ABC # echo $2 XYZ
- 08-30-2008 #5
awk also has the split() function which will process your text into an array and return the number of elements.


Reply With Quote
