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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
05-15-2009, 11:46 AM
|
#1
|
Member
Registered: Oct 2004
Location: San Diego
Distribution: Mandrake Linux 10.1
Posts: 30
Rep:
|
Problem with shell script - pulling two fields from a file
Hello all,
Long time lurker and learner, first time poster. I'm having a problem figuring out how to pull two fields from a file. Normally this is simple I know but in this case the fields are disparate such that I am not having any luck figuring this out. in the example below I need the numbers in the second field and then the last number in the line with the work HIDDEN. I need them in that order. File also has headers in it which I can by pass but am unable to get the data out in the correct order. Any ideas would be greatly appreciated with much gratitude!
3 23601 0.00
0.00 98 A04-2 0.0100000 9999999999.00 27952.55
HIDDEN 0.0000000 25000.00 25000.00 25000.00
4 15636 0.00
0.00 98 A04-3 0.0100000 9999999999.00 5149.90
HIDDEN 0.0000000 5000.00 5000.00 5000.00
5 23616 0.00
0.00 98 A04-4 0.0100000 9999999999.00 5243.40
HIDDEN 0.0000000 5000.00 5000.00 5000.00
6 16138 0.00
16140 0.00
16141 0.00
16142 0.00
16145 0.00
16134 0.00
16135 0.00
16136 0.00
16143 0.00
0.00 98 A05 0.0100000 9999999999.00 20919.60
HIDDEN 0.0000000 20000.00 20000.00 20000.00
7 12802 0.00
12803 0.00
12804 0.00
12805 0.00
12806 0.00
12807 0.00
12808 0.00
0.00 98 B02 0.0050000 9999999999.00 4428.05
HIDDEN 0.0000000 4000.00 4000.00 4000.00
|
|
|
05-15-2009, 12:16 PM
|
#2
|
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326
|
can you please post an example of the output you want. i cant figure it out by your description.
seems like a job for awk grep cut...
Last edited by schneidz; 05-15-2009 at 12:20 PM.
|
|
|
05-15-2009, 01:35 PM
|
#3
|
Member
Registered: Oct 2004
Location: San Diego
Distribution: Mandrake Linux 10.1
Posts: 30
Original Poster
Rep:
|
RE: Problem with shell script - pulling two fields from a fil
Example below
<==============Before===================
5 23616 0.00
0.00 98 A04-4 0.0100000 9999999999.00 5243.40
HIDDEN 0.0000000 5000.00 5000.00 5000.00
6 16138 0.00
16140 0.00
16141 0.00
16142 0.00
16145 0.00
16134 0.00
16135 0.00
16136 0.00
16143 0.00
0.00 98 A05 0.0100000 9999999999.00 20919.60
HIDDEN 0.0000000 20000.00 20000.00 20000.00
7 12802 0.00
12803 0.00
12804 0.00
12805 0.00
12806 0.00
12807 0.00
12808 0.00
0.00 98 B02 0.0050000 9999999999.00 4428.05
HIDDEN 0.0000000 4000.00 4000.00 4000.00
<====================After==============================>
23616
5000.00
16138
16140
16141
16142
16145
16134
16135
16136
16143
20000.00
12802
12803
12804
12805
12806
12807
12808
4000.00
|
|
|
05-15-2009, 01:41 PM
|
#4
|
Senior Member
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288
Rep:
|
So,
- records are broken up by blank lines
- The second field in the first line is needed
- Then each of the first fields in the next multiple lines
- You don't want anything from the line before the one starting with "HIDDEN"
- You then want the last field in the line starting with "HIDDEN"
Is this correct?
Forrest
|
|
|
05-15-2009, 01:56 PM
|
#5
|
Member
Registered: Oct 2004
Location: San Diego
Distribution: Mandrake Linux 10.1
Posts: 30
Original Poster
Rep:
|
RE: Problem with shell script - pulling two fields from a file
Yes sir that be correct.
I was trying to use awk to pull the hidden value but I don't seem to be using it correctly at all. the field I want actually starts in the 82nd position in the file so I was trying to do something like:
but it isn't working and I then mess up my other grep for the other sets of numbers, and then I get lost....
|
|
|
05-15-2009, 02:34 PM
|
#6
|
Senior Member
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288
Rep:
|
This does it for that particular data stream. Don't know if it will work with all of your data though.
Code:
awk '{ if ($1 == "HIDDEN") { print $NF } else { if ( NF == 3 ) { print $2 } else { if ( $1 == "0.00" ) { print "" } else {print $1 } } } }' inputfile.txt
HTH
Forrest
|
|
|
05-15-2009, 02:58 PM
|
#7
|
Member
Registered: Oct 2004
Location: San Diego
Distribution: Mandrake Linux 10.1
Posts: 30
Original Poster
Rep:
|
RE: Problem with shell script - pulling two fields from a file
That does it! Thank you very much, I can pull the other field and strip the headers (27 pages)but I couldn't grab the one field, I'll go back and look closely at what you wrote and try to use the example to pull the other field as well just to see if I understand it. Thank you again!
|
|
|
All times are GMT -5. The time now is 03:43 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|