Find x and print its record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find x and print its record
# 1  
Old 09-06-2011
Find x and print its record

Hi all,

I have a file containing two fields with 154 rows/records/lines (forgive me, my UNIX terminology is not quite up to par yet). I am trying to read from this list, find a value (lets say 0), then print the record/line/row that value falls on (In this case it would be record/line/row #27)? Here is an example of the type of file Im dealing with...

Code:
1023 311
899 311
1013 311
583 311
439 311
409 311
199 311
149 311
138 311
138 311
112 311
116 311
148 311
 82 311
 16 311
 17 311
 23 311
 14 311
  8 311
  9 311
  8 311
  4 311
 14 311
  2 311
  2 311
  2 311
  0 311
  1 311

I thought perhaps awk might do the trick, but I suspect there might be something better, and frankly, I havent a clue as to how to solve this problem. Any help would be greatly appreciated!

-SS

Last edited by radoulov; 09-07-2011 at 11:36 AM.. Reason: Code tags, please!
# 2  
Old 09-06-2011
Code:
awk '/0/' file

# 3  
Old 09-06-2011
Quote:
Originally Posted by StudentServitor
I am trying to read from this list, find a value (lets say 0), then print the record/line/row that value falls on...
Code:
awk -v x=0 '{for(i=0;++i<=NF;){if($i==x){print;next}}}' file

# 4  
Old 09-06-2011
Hi Danmero,

Thanks for your solution, it's is really close to what I need, but...I actually need the line number that the value 0 falls on, in my example it was line #27, the value 0 is is just the criterion if you will.

This list represent points in time (in total 154) within fMRI data, the values in field $1 represent deviations from baseline within those points in time, basically head movement by the subject. The value 0 is significant because it represents no such deviations from baseline. The line number that that 0 falls on is important because it represents a specific point within the temporal sequence of the fMRI scan in which the subject wasn't moving. I take that value (the line number) and input it into another program so that it can properly register the entire fMRI dataset. To further complicat things, this 0 value will be different for every subject and every fMRI scan.

Does that help to clarify my problem a bit?
# 5  
Old 09-07-2011
Let's try this one:
Code:
awk -v x=0 '$1==x{print NR}' file
27

This User Gave Thanks to danmero For This Post:
# 6  
Old 09-07-2011
Danmero your brilliant! Thank you so much for your help! This is exactly what I needed.

This solution was a lot simpler than I expected, I am rather little embarrassed I didnt come up with this myself. All the same, I really appreciate your help.

Best regards

SS
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Modifying text file records, find data in one place in the record and print it elsewhere

Hello, I have some text data that is in the form of multi-line records. Each record ends with the string $$$$ and the next record starts on the next line. RDKit 2D 15 14 0 0 0 0 0 0 0 0999 V2000 5.4596 2.1267 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 ... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

2. Shell Programming and Scripting

Find key pattern and print selected lines for each record

Hi, I need help on a complicated file that I am working on. I wanted to extract important info from a very huge file. It is space delimited file. I have hundred thousands of records in this file. An example content of the inputfile as below:- ## ID Ser402 Old; 23... (2 Replies)
Discussion started by: redse171
2 Replies

3. Shell Programming and Scripting

Help with print out line that have different record in specific column

Input file 1: - 7367 8198 - 8225 9383 + 9570 10353 Input file 2: - 2917 3667 - 3851 4250 + 4517 6302 + 6302 6740 + 6768 7524 + 7648 8170 + 8272 8896 + 8908 9915 - 10010 ... (18 Replies)
Discussion started by: perl_beginner
18 Replies

4. Shell Programming and Scripting

Find and remove duplicate record and print list

Gents, I needs to delete duplicate values and only get uniq values based in columns 2-27 Always we should keep the last record found... I need to store one clean file and other with the duplicate values removed. Input : S3033.0 7305.01 0 420123.8... (18 Replies)
Discussion started by: jiam912
18 Replies

5. Shell Programming and Scripting

AWK print initial record and double

I have an initial record 0.018 I would like a script that would for i=0;i<200;i++ print 0.018*1 0.018*2 0.018*3 0.018*4 ... 0.018*200 using newline. (7 Replies)
Discussion started by: chrisjorg
7 Replies

6. Shell Programming and Scripting

Print first and last line from multiline record

Hi - I'm new to working with multiline records and I'm going nuts trying to do something that seems simple. Input: Tue May 1 14:00 Header Record 1 is valid. Tue May 1 14:00 processing data to 25-Mar-2012 09:00:23.15 Tue May 1 14:03 Header Record 1 is valid. Tue May 1 14:03 processing data... (4 Replies)
Discussion started by: Catullus
4 Replies

7. UNIX for Dummies Questions & Answers

Print first, second, every nth, and last record

does anyone have an awk one-liner to: print the first line, the second line, then every Nth line, and the last line of a file. Thanks, Kenny. (5 Replies)
Discussion started by: kenneth.mcbride
5 Replies

8. Shell Programming and Scripting

Print all the fields of record using awk

Hi, i want to generate print statement using awk. i have 20+ and 30+ fields in each line Now its priting only first eight fields print statement as output not all. my record is as shown below filename ... (2 Replies)
Discussion started by: raghavendra.nsn
2 Replies

9. Shell Programming and Scripting

awk - print record with both string1 and string2

How do I use awk to find the records in a file that contains two specific strings? I have tried piping and using awk two times, but I don't know how to do it in one action. (2 Replies)
Discussion started by: locoroco
2 Replies

10. UNIX for Advanced & Expert Users

Print Full record and substring in that record

I have i got a requirement like below. I have input file which contains following fixed width records. 00000000000088500232007112007111 I need the full record and concatenated with ~ and characters from 1to 5 and concatenated with ~ and charactes from 10 to 15 The out put will be like... (1 Reply)
Discussion started by: ukatru
1 Replies
Login or Register to Ask a Question