Visit Our UNIX and Linux User Community


Nth of a file - bis


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Nth of a file - bis
# 1  
Old 10-27-2009
Nth of a file - bis

I've seen that it has been suggested to use awk to show e.g. the 50th line in a file

awk 'NR==50{print;exit}' file

What about if instead of a number I try to use a variable? e.g. COUNT

awk 'NR==$COUNT{print;exit}' file
awk 'NR==${COUNT}{print;exit}' file


I can't make it work,

Thanks,
# 2  
Old 10-27-2009
Code:
awk 'NR == C {print;exit}' C=$COUNT file

Or
Code:
awk -v C=$COUNT 'NR == C {print;exit}' file

Or
Code:
awk 'NR == '$COUNT' {print; exit}' file

Or with sed:

Code:
sed -n "${COUNT}p" file

Or
Code:
head -$COUNT | tail -1

(etc...)

Last edited by Scott; 10-27-2009 at 10:48 AM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #73
Difficulty: Easy
BSD (Berkeley Software Distribution) is an open-source, Unix-like operating system.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace nth to nth character?

Hi I got the following problem and I wonder if some could please help me out? I'd like to replace character 8 - 16 , 16 - 24 cat file ... (2 Replies)
Discussion started by: stinkefisch
2 Replies

2. Shell Programming and Scripting

Replace a value of Nth field of nth row

Using Awk, how can I achieve the following? I have set of record numbers, for which, I have to replace the nth field with some values, say spaces. Eg: Set of Records : 4,9,10,55,89,etc I have to change the 8th field of all the above set of records to spaces (10 spaces). Its a delimited... (1 Reply)
Discussion started by: deepakwins
1 Replies

3. Shell Programming and Scripting

Read a file from the nth line on

I have a script which reads from a job file and executed the scripts in the job file in sequence. #! /bin/ksh set -x while read line do $line.ksh if # mail the team fi done <"$file" The job file will be like abcd efgh ijkl mnop qrst This is working fine. I need to add... (2 Replies)
Discussion started by: nw2unx123
2 Replies

4. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

5. Shell Programming and Scripting

Using AWK to find top Nth values in Nth column

I have an awk script to find the maximum value of the 2nd column of a 2 column datafile, but I need to find the top 5 maximum values of the 2nd column. Here is the script that works for the maximum value. awk 'BEGIN { subjectmax=$1 ; max=0} $2 >= max {subjectmax=$1 ; max=$2} END {print... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

6. Shell Programming and Scripting

Reading a file after nth line

I know how to read a file line by line. But don't to how to skip to a line matching a criteria and then continue reading it till the end. This is a log file. The input is a timestamp. 1. Find the timestamp in the log file 2. Read the remaining lines one at a time till EOF. How can I do... (9 Replies)
Discussion started by: avinthm
9 Replies

7. Shell Programming and Scripting

replace nth value in xml file

Hi all, I have application.xml file with following content <dependency> <groupId>fr.orange.portail.ear</groupId> <artifactId>_AdminServicesEAR</artifactId> <version>1.0.0-20080521.085352-1</version> <type>ear</type> </dependency> <dependency> ... (4 Replies)
Discussion started by: subin_bala
4 Replies

8. Shell Programming and Scripting

Change nth Char in a file..

I wanted to Replace if 20th Char is space then Replace by X .. 12345678901234567890 AAAA HEXW PROGRM01 (Ended by 3 Spaces ) Followed by junk(can be spaces als) BBBB HEXW PROGRM01 A0121225001 (Ended by 3 Spaces)Followed by junk I have Tired some thing of this sort ... cat... (4 Replies)
Discussion started by: pbsrinivas
4 Replies

9. Shell Programming and Scripting

how to get nth file filelist

hi all, kindly help me in getting nth file from file list ls load*.txt > filelist.txt #filelist.txt contains list of files starts with load as filename now i am required to get one by one from filelist.txt for further processing, kindly let me know how to get it. thanks in advance -Bali (1 Reply)
Discussion started by: balireddy_77
1 Replies

Featured Tech Videos