LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 05-15-2009, 11:46 AM   #1
Dcrusoe
Member
 
Registered: Oct 2004
Location: San Diego
Distribution: Mandrake Linux 10.1
Posts: 30

Rep: Reputation: 15
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
 
Old 05-15-2009, 12:16 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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.
 
Old 05-15-2009, 01:35 PM   #3
Dcrusoe
Member
 
Registered: Oct 2004
Location: San Diego
Distribution: Mandrake Linux 10.1
Posts: 30

Original Poster
Rep: Reputation: 15
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
 
Old 05-15-2009, 01:41 PM   #4
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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
 
Old 05-15-2009, 01:56 PM   #5
Dcrusoe
Member
 
Registered: Oct 2004
Location: San Diego
Distribution: Mandrake Linux 10.1
Posts: 30

Original Poster
Rep: Reputation: 15
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....
 
Old 05-15-2009, 02:34 PM   #6
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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
 
Old 05-15-2009, 02:58 PM   #7
Dcrusoe
Member
 
Registered: Oct 2004
Location: San Diego
Distribution: Mandrake Linux 10.1
Posts: 30

Original Poster
Rep: Reputation: 15
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!
 
  


Reply



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
bash shell script find and edit fields in a file hchoonbeng Programming 9 10-29-2008 02:13 AM
My php form is not pulling the data. Just giving me the echo message for req. fields Smsu0301 Programming 10 10-11-2008 01:26 AM
Shell script problem. check file already exists sinister1 Linux - Server 8 11-20-2007 03:13 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
SH SCRIPT +pulling information from a text file chrisfirestar Linux - General 1 01-30-2004 07:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:16 AM.

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
Open Source Consulting | Domain Registration