LinuxQuestions.org
Help answer threads with 0 replies.
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-21-2013, 02:14 AM   #1
fd99
LQ Newbie
 
Registered: May 2013
Posts: 9

Rep: Reputation: Disabled
Linux bash question


Hello,

I have following problem:

-I have a file containing several columns which are separated by a semicolon.

-The first column is numerical an in decreasing order

-I want to copy THE FIRST complete line, in which the first column satisfies a "less or equal than" condition to an other file.

How can this goal be reached? I used several awk or grep commands but I did not arrive to a solution.

Any help would highly be appreciated.

Best regards!
 
Old 05-21-2013, 02:44 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
HI,
can you show us what you tried?
 
Old 05-21-2013, 02:50 AM   #3
fd99
LQ Newbie
 
Registered: May 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
HI,
can you show us what you tried?
The initial string which worked was:

grep -m 1 "^$criteria" output.txt >> $SPATH

But this makes just a 1:1 comparison.

As soon as I insert a comparison command
e.g.:

grep -m 1 "^<$criteria" output.txt >> $SPATH

the string does not work anymore :/
 
Old 05-21-2013, 02:51 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Can you show use some example data and what you want to see.
 
Old 05-21-2013, 02:54 AM   #5
fd99
LQ Newbie
 
Registered: May 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
Can you show use some example data and what you want to see.
Sure.
Data:
1.35882590673424 0
1.35084758122717 5E-06
1.34268295888141 1E-05
1.3274715977362 1.5E-05
1.31225475065331 2E-05
1.29709920692472 2.5E-05
1.28193363539596 3E-05
1.26682070357858 3.5E-05
1.2517299445711 4E-05
1.23663918556361 4.5E-05
1.22165136555059 5E-05
1.20666289839408 5.5E-05
1.19167443123757 6E-05
1.17682845682672 6.5E-05
1.1619741431878 7E-05
1.14711982954889 7.50000000000001E-05
1.13240869093759 8.00000000000001E-05
1.1177254412216 8.50000000000001E-05
1.10304219150561 9.00000000000001E-05
1.08842268822075 9.50000000000001E-05
1.07395364551007 0.0001
1.05948460279939 0.000105
1.04501556008871 0.00011
1.03065536767132 0.000115
1.01645208010549 0.00012
1.00224879253967 0.000125
.988045504973844 0.00013
.973873409588848 0.000135

Desired output: first line in which the first column is less than 1.2:

1.19167443123757 6E-05

(the actual column separator is a space bar and not a semicolon as written in my first post)

Last edited by fd99; 05-21-2013 at 02:57 AM.
 
Old 05-21-2013, 02:59 AM   #6
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
I think in your case awk would be a better choice:

Code:
awk '$1<1.2{print;exit}' file1 >file2
 
Old 05-21-2013, 03:06 AM   #7
fd99
LQ Newbie
 
Registered: May 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
I think in your case awk would be a better choice:

Code:
awk '$1<1.2{print;exit}' file1 >file2
Wow, your awk command works superb!

It's clean and efficient

Thank you a lot for your kind support!
 
Old 05-21-2013, 03:47 AM   #8
fd99
LQ Newbie
 
Registered: May 2013
Posts: 9

Original Poster
Rep: Reputation: Disabled
[QUOTE=fd99;4955528]

Sorry to bother you again, but actually I was happy a bit to early. Actually it does not work and writes no output...
 
Old 05-21-2013, 04:21 AM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
it will write the result into file2 (if there was a line)
 
  


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
[SOLVED] Linux bash question fd99 Programming 4 05-15-2013 02:55 PM
Question about Bash script for Linux Linh Programming 9 04-21-2004 11:54 AM
linux question about bash, shell ?? albina Linux - General 3 05-15-2002 01:07 AM

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

All times are GMT -5. The time now is 07:50 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
Open Source Consulting | Domain Registration