LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-01-2010, 02:20 AM   #1
waseemfa
LQ Newbie
 
Registered: Sep 2010
Posts: 2

Rep: Reputation: 0
Scan through a file and enter the data in multiple files


Hi,

I have a file which contains the data i retrieved through prstat and an array that contains all the unique process ID's of that particular file. i want to compare each and every line in the file with each and every element of the array so that i can create multiple files for the multiple value in the array.

can any one help me in this regard?
 
Old 09-01-2010, 02:50 AM   #2
rikxik
Member
 
Registered: Dec 2007
Posts: 88

Rep: Reputation: 19
You need to put up some simple example of what you want to do. Your objective is not very clear to me. Also, please post what have you tried so far.
 
Old 09-01-2010, 02:53 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

You seem to have already (partially?) decided how you want to approach your problem, which might or might not be the way to go. Could you describe what the starting point is and what you want/need to accomplish? Your question is too open to give you any good advise/examples.

You do have input (prstat command) and you seem to have an array (awk, C, bash, perl, ... ? or is this just a flat file with information?).

Assuming that you actually have an array, what have you done this far and what seems not to work (which would also tell us which language you are using)?

You could also give some relevant examples (prstat is not installed on all distro's, what does the array/flat file look like and how should the end result look)?

Hope this helps.
 
Old 09-01-2010, 02:54 AM   #4
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 100Reputation: 100
Something like this:
Code:
while read line; do
    for (( i=1; i<=${array[@]}; i++ )) do
        ...
    done
done < file1.txt
 
Old 09-01-2010, 03:36 AM   #5
waseemfa
LQ Newbie
 
Registered: Sep 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Hi,

right now i have an array of ids that i will scan through a file and find out which ever elements first column matches the array element it should print the element on the screen

Right now i have tried the following code but it dosent works

for i in ${MYARRAY1[@]}
do
while read line ; do
MYARRAY[$index1]="$line"
cat temp.txt | grep ^" " | grep [0-9] | gawk '{if($1 == ' "$MYARRAY1[i]" ') {print;}}'

done<temp.txt

done

here array has all the unique elements in it and the file has data with the first column having all the ID's in it.

The goal is to match the ID with the element in the array and if both are equal then print the entire line of that particular array.
 
Old 09-01-2010, 04:13 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

As stated in both posts #2 and #3, you need to provide examples of the input you use.

The code snippet you provided does not make too much sense (how did the array get filled, how does $index1 fit in to this, why the need to use temp.txt twice).

Like I stated before; You seem to have already decided on a particular way of solving this (which might not be the best way to solve the actual problem). Your goal seems to be clear (print line if X matches Y), but without examples of the input we will have a hard time helping you.

- I assume the temp.txt holds the output of prstat (what does the output of prstat look like?).
- Where is the info coming from that is in the array?
 
Old 09-01-2010, 04:18 AM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
How did the IDs get into the array? There's always a chance
that an awk- or perl-only solution may be more efficient for
what you're trying to achieve.

If the IDs, for example, came from a separate file you could
always populate an array w/ the IDs in awk's BEGIN statement;
then just iterate over the array inside the actual '{}' work
section of the awk, script and compare each line's critical
field to the array value.



Cheers,
Tink
 
  


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
combine multiple files into one file? baddah Linux - Newbie 10 09-06-2011 12:22 PM
[SOLVED] Extract multiple lines of data from a text file. shawnamiller Programming 8 04-30-2010 12:46 PM
how do i scan a character from keyboard without the user having to press enter rasika Programming 6 02-11-2010 09:54 PM
gzip but keep the original file, and bzip2 - multiple files to one file? SirTristan Linux - Newbie 4 10-20-2009 04:37 AM
gnuplot - do multiple plots from data file with built-in commands Meson Linux - Software 2 07-30-2008 04:52 PM


All times are GMT -5. The time now is 09:32 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration