Read a file using awk for a given no of lines.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read a file using awk for a given no of lines.
# 1  
Old 08-27-2012
Read a file using awk for a given no of lines.

Hi,

how do i read a file using awk for a given no of line?

e.g
1. read only first 50 line.
2. read starting from line 20 to line 60..

thanks in advance.

-alva
# 2  
Old 08-27-2012
Small two line shell script that takes two optional parameters: number of lines to print ($1) and the starting line number ($2) Reads from stdin.

Code:
#!/usr/bin/env ksh
awk -v startl=${2:-1} -v nlines=${1:-10} ' NR - (startl-1) > nlines { exit( 0 ); } NR >= startl '

Lots of ways to modify this to suit your needs.

EDIT: should mention, if it's not obvious, the default starting line is 1, and default number of lines to print is 10.
This User Gave Thanks to agama For This Post:
# 3  
Old 08-28-2012
Hi Agama,

thank you for your prompt response.

-alva.


i have a followup question, how to incorporate a filter(using unix timestamp) in that script?

eg.

input file structure : timestamp , data1, data2, data3.....

1.read only lines starting from todate to 10 days later. (aug 19-28)

-alva
# 4  
Old 08-28-2012
Depends on the version of awk. What is output when you run: awk --version and what operating system are you using?
# 5  
Old 08-28-2012
Code:
GNU Awk 3.1.5

RHEL5
# 6  
Old 08-28-2012
Assuming that version of gawk has mktime()....

Forgot to ask, and you didn't specify the format of the timestamp. Assuming it is in the first two tokens of each input line, and is of the format yyyy-mm-dd hh:mm:ss then try this:

Code:
awk -v startt="${1:-2012-8-20  00:00:00}" -v ndays=${2:-10} '
    BEGIN {
        gsub( "[-:]", " ", startt );
        sts = mktime( startt );
        ets = sts + (ndays * 86400 );
    }

    # assuming timestamp is yyyy-mm-dd hh:mm:ss in the first two columns
    {
        ctd = $1;    # ensure that the original input isnt changed -- save and strip undesired characters
        ctt = $2;
        gsub( "[-:]", " ", ctd );
        gsub( "[-:]", " ", ctt );

        cts = mktime( ctd " " ctt );
        if( cts >= sts )
        {
            if( cts <= ets )
                print;
            else
                exit( 0 );
        }
    }
' input-file

EDIT: If embedded into a script, you can invoke with starting date string, and number of days to print:

Code:
print_from_log  "2012-08-10 00:00:00" 10

This User Gave Thanks to agama For This Post:
# 7  
Old 08-28-2012
hi agama,

sorry for the confusion. what i meant by time stamp was the unix Epoch.

here is the sample data in the input file.

Code:
1344324384:LOTSTART        0000JPHMES9624-NEW LOTS CREATED-SOURCE LOT 3A MD03AUG16
1344324385:LOTSTART        0000JPHMES9624-NEW LOTS CREATED-SOURCE LOT 3A MD03AUG31 
1344324386:LOTSTART        0000JPHMES9624-NEW LOTS CREATED-SOURCE LOT 3A 1C800017
....
...
..

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk or a combination of commands to read and calculate nth lines from pattern

Two numerical lines, found by either header line, need to be added and the total placed in a new-header section. Also the total should should be rounded or cut to a two decimal anynumber.XX format with the AB string added on the end. For example: The numerical lines from headers 2 and 3 are... (3 Replies)
Discussion started by: jessandr
3 Replies

2. Shell Programming and Scripting

Help with ksh-to read ip file & append lines to another file based on pattern match

Hi, I need help with this- input.txt : L B white X Y white A B brown M Y black Read this input file and if 3rd column is "white", then add specific lines to another file insert.txt. If 3rd column is brown, add different set of lines to insert.txt, and so on. For example, the given... (6 Replies)
Discussion started by: prashob123
6 Replies

3. Shell Programming and Scripting

Read few last lines of a file

Hi, I have a txt file in below format - START 1 2 3 4 END START 5 6 7 8 END START 9 10 END (8 Replies)
Discussion started by: bhupinder08
8 Replies

4. Shell Programming and Scripting

AWK getline command to read more then two lines

Hello, Am trying to print three lines in a single line using AWK getline command. Below is the command am trying and its displaying only two lines in a single line. Command: awk '{getline n; printf($0,t);next}' Can you please help me ? Thanks, Prince (1 Reply)
Discussion started by: prince1987
1 Replies

5. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

6. Shell Programming and Scripting

[AWK] read lines with \x00 symbol

I want to read a large (~1-4Gb) txt file with fields separated by "," and line separator "\n". Unfortunately, file contains \x00 (zero ASCII) symbols AWK treats them as end of line + it ignores reminder of the line after the \x00. As a simple example: echo "\0060\0061\000\0060\0063" | nawk... (6 Replies)
Discussion started by: Murfury
6 Replies

7. Shell Programming and Scripting

read some lines from file!!!

any idea please!!! I want to pick up all lines of "state" and "desc" from x files: ... # state blah blah blah blah ... .. # desc blah blah blah .... Thx Andy (7 Replies)
Discussion started by: andy2000
7 Replies

8. UNIX for Dummies Questions & Answers

Read lines from file

i have a problem on my bourne shell script. I want to read line by line and then stop when the line does not have letters or is an empty string. But i encounter an error at "while ". The error nessage is "test.sh: test: unknown operator line". Can anyone help me on this thanks :) (2 Replies)
Discussion started by: sagolo
2 Replies

9. UNIX for Dummies Questions & Answers

how to read lines one by one from a file

I have one file in which some commands have written line line i have to read lines from this file(file name passed as avariable) and then i have to execute these commands.. how can i do it? (5 Replies)
Discussion started by: bihani4u
5 Replies

10. Programming

How to read specific lines in a bulk file using C file Programming

Please Help me I have a Bulk file containing Hex data I want to read specific lines from that bulk file by ID number. example ID DATE Time data 14 2005/09/28 07:40:08.546 0 5 078B1C 01916C 0FE59C 004B54 0A9670 0D04ED 05B6B4 0E2223... (10 Replies)
Discussion started by: rajan_ka1
10 Replies
Login or Register to Ask a Question