LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-14-2005, 03:49 PM   #1
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Rep: Reputation: 30
Multi-line return from grep into an array?


I was playing with a bash script earlier, that cat'd a file and grep'd it for something like "A|B". This returned a multiple number of lines, so that the output from grep was

line A.1
line A.2
line B.1
line B.2
etc.
which obviously contains a new line after each line.

Right now I'm redirecting the result of the cat | grep to a file, and then cat | while read'ing that file in order to separately access each of the lines. There's *got* to be a way to do this without the external file..I just can't figure out how to somehow shoehorn the return of grep into a bash array or anything else iterate-able.

To let you know what I've tried:
- setting IFS to newline, but it seems that only cat pays attention to IFS...echo doesn't. And I can only cat a file, not a variable (that I've seen, anyway).
- I also gave piping the result to awk 'BEGIN {FS="
"} ...but there I got stuck, both because bash complains about that syntax and missing ending quotes and all that, but also because I've only seen that when followed immediately by {print $1 $2 $3} ..and I have no way of knowing how many lines will be returned.

Any pointers in the right direction would be appreciated!
 
Old 06-14-2005, 04:11 PM   #2
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
Hope I got this correct. You have the result of something and you want get it line by line in a variable.
Instead of redirecting it to file try piping it to a while read loop.

First of all there's no need to
Code:
cat <filename> |grep <pattern>
.
Just
Code:
grep <pattern> <filename>
it's quicker. piping always costs.

SO try something like
Code:
grep <pattern> <filename> | while read a; do ...... ; done
The variable $a will get the input line by line
 
Old 06-14-2005, 04:11 PM   #3
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
You should be able to use:
IFS="
"
for $line in `grep STRING FILE`;do
echo Line: $line
done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
grep detecting carriage return, how ? Grafbak Programming 22 01-13-2010 01:19 PM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
How to return values into an array using awk Helene Programming 1 05-01-2004 10:05 PM
C can 't return a character array Linh Programming 5 06-18-2003 04:58 AM
Cannot pass and return a character array Linh Programming 1 06-12-2003 04:17 PM


All times are GMT -5. The time now is 05:03 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration