awk script to return the middle line number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script to return the middle line number
# 1  
Old 03-31-2011
awk script to return the middle line number

I need an awk script that returns the 1st field of the line in the middle of a file. For example, if a file I have has 6 lines, I want it to return the 1st field on line number 3. If a file has 7 lines, I want the script to return the 1st field on line number 4.

File1:
Code:
3 214
4 219
5 226
2 349138
1 349745
0 355906

File2:
Code:
3 214
4 219
6 220
5 226
2 349138
1 349745
0 355906

For File1, the script should simply return:
Code:
5

For File2, the script should simply return:
Code:
5

# 2  
Old 03-31-2011
Code:
awk 'NR>FNR&&FNR>=(NR-FNR*2){ print $1; exit }' file1 file1

# 3  
Old 03-31-2011
Ruby(1.9+)
Code:
$ ruby -0777 -ne 's=$_.split("\n"); puts (s.size%2==0)? s[(s.size/2)-1]:s[s.size/2] ' file

This User Gave Thanks to kurumi For This Post:
# 4  
Old 03-31-2011
Chubler_XL,

Sorry I'm not sure but maybe I didn't explain it clearly. I will only be doing this on one file at a time. I just listed 2 files as 2 separate examples. I tried using the code you put with one file and nothing showed up on the screen. Pardon my ignorance. Can you clarify?

Thanks!

---------- Post updated at 09:14 PM ---------- Previous update was at 09:12 PM ----------

Quote:
Originally Posted by kurumi
Ruby(1.9+)
Code:
$ ruby -0777 -ne 's=$_.split("\n"); puts (s.size%2==0)? s[(s.size/2)-1]:s[s.size/2] ' file

kurumi,

Thank you for the help, but I would like to keep it in awk or bash if possible.
# 5  
Old 03-31-2011
Yes the awk script requires the same file to be passed into it twice, first pass it to get total records 2nd prints the line.

Shame both your demo files produce the same output:
Code:
$ awk 'NR>FNR&&FNR>=(NR-FNR*2){ print $1; exit }' file1 file1
5
 
$ awk 'NR>FNR&&FNR>=(NR-FNR*2){ print $1; exit }' file2 file2
5

This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 03-31-2011
Try this.
Code:
awk '{arr[NR]=$1} END{if(NR%2==0)print arr[NR/2]; else print arr[(NR-1)/2]}' inputfile

This User Gave Thanks to tene For This Post:
# 7  
Old 03-31-2011
Quote:
Originally Posted by Chubler_XL
Yes the awk script requires the same file to be passed into it twice, first pass it to get total records 2nd prints the line.

Shame both your demo files produce the same output:
Code:
$ awk 'NR>FNR&&FNR>=(NR-FNR*2){ print $1; exit }' file1 file1
5
 
$ awk 'NR>FNR&&FNR>=(NR-FNR*2){ print $1; exit }' file2 file2
5

Oh Ok, I didn't understand that at first. It works! Thank you!

---------- Post updated at 09:50 PM ---------- Previous update was at 09:24 PM ----------

Quote:
Originally Posted by tene
Try this.
Code:
awk '{arr[NR]=$1} END{if(NR%2==0)print arr[NR/2]; else print arr[(NR-1)/2]}' inputfile

This also worked! Thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command to return only field with a number in it

What is an awk command to print only fields with a number in it?? Input file....... S,S,S,S,S,S,S,S,S 001S,S,S,S,S,S,S,S,S 00219S,23S,24S,43S,47S,S,S,S,S 00319S,10S,23S,41S,43S,47S,S,S,S 00423S,41S,43S,46S,47S,S,S,S,S 00510S,23S,24S,43S,46S,S,S,S,S 00610S,23S,43S,46S,47S,S,S,S,S... (2 Replies)
Discussion started by: garethsays
2 Replies

2. Shell Programming and Scripting

awk - Print column number that return value comes from

I have the following awk script that I am using to find the max value in the file and print results. awk 'BEGIN {MAX=-1E100} {for (x=2; x<=NF; x++) if ($x>MAX) {MAX = $x; C1 = $1}} END {print substr(C1,1,11), substr(C1,13,4), substr(C1,18,2), MAX}' ABC* Input (ABC*) ... (6 Replies)
Discussion started by: ncwxpanther
6 Replies

3. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

4. Shell Programming and Scripting

awk return number of entries

I have a string with the following information and want to return the number of entries enclosed by <> in awk <stdin>: N = 441 <0.369000018/0.569000006> <0.369000018/0.569000006> <0/1> (7 Replies)
Discussion started by: kristinu
7 Replies

5. Shell Programming and Scripting

How can i make the current shell return from the middle of a script reading?

I am using the popular bash shell. Under the current interactive shell, i run the script like: ". ./myscript.txt" . After the current shell has finish the script, the shell will continue to work as I did previously. Actually I want the shell can return from the middle of the scripts it is... (1 Reply)
Discussion started by: Bill Zhao
1 Replies

6. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

7. Shell Programming and Scripting

awk script: print line number n of another file

Hi, I wrote an awk script to analyse file A. I call the script with files A and B. File A has lines like: 000000033100001 000000036100001 000000039100001 The first 9 characters are interpreted as a line number; for each line number found I want to output this line number of file B. ... (13 Replies)
Discussion started by: kpg
13 Replies

8. Shell Programming and Scripting

Want to trap script error and return line number of failure

Hey all UNIX nerds- I've built a shell script which runs pretty well- only I want it to have much better error trapping. (Like the kind I could apply to every shell script I write). I'm not a UNIX genius, and could really use a bit of help. The original script goes something like this: 1... (3 Replies)
Discussion started by: stevekerver
3 Replies

9. Shell Programming and Scripting

Script to add a single line to middle of text file.

I've got a configuration file that is filled with xml text statements for example: <...../> <...../> <...../> <data id="java-options" value="-server -Djava.security.policy..../> <...../> <...../> <...../> I want to write a korn shell script that will go to this specific line and add a... (2 Replies)
Discussion started by: progkcp
2 Replies

10. Shell Programming and Scripting

awk insert character in the middle of a line

I'm trying to insert a single character at position 11 in everyline of a file. My input file looks like this: 456781 ~Y~12345 456782 ~N~12300 and I want my output to look like this: 45678~1 ~Y~12345 45678~2 ~N~12300 I tried the following awk code, but it's not working:... (3 Replies)
Discussion started by: mmarino
3 Replies
Login or Register to Ask a Question