Reading from a dynamically decided line number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading from a dynamically decided line number
# 1  
Old 06-27-2013
Reading from a dynamically decided line number

Hi All,

We get a file from our client for processing everyday. We have to start reading the file from the line after the line that says "version=".
My idea was to grep and find the line number(say 'n') of the line with "version=" and start reading from the (n+1)th line. Can anyone please guide me on how to achieve this
# 2  
Old 06-27-2013
Quote:
Originally Posted by jerome_rajan
Hi All,

We get a file from our client for processing everyday. We have to start reading the file from the line after the line that says "version=".
My idea was to grep and find the line number(say 'n') of the line with "version=" and start reading from the (n+1)th line. Can anyone please guide me on how to achieve this
You are on the right track.
First grep version= and set line number to variable called n.
Then use this variable and use awk for printing lines whose line number is more than n
Use NR for line number.

Regards,

pamu
# 3  
Old 06-27-2013
just did this


Code:
$n=`grep -n "Version=" filename | cut -d ":" -f1`
$echo $n
10681
$awk 'NR>$n' filename
awk: 0602-562 Field$() is not correct

Any idea why I got that error?
# 4  
Old 06-27-2013
Quote:
Originally Posted by jerome_rajan
just did this


Code:
$n=`grep -n "Version=" filename | cut -d ":" -f1`
$echo $n
10681
$awk 'NR>$n' filename
awk: 0602-562 Field$() is not correct

Any idea why I got that error?
Use -v in awk for using shell variable into awk.
This User Gave Thanks to pamu For This Post:
# 5  
Old 06-27-2013
You can also try this in awk :
Code:
awk '/version=/ { a=NR } { b[$0] = NR } END { for ( i in b ) if ( b[i] > a ) { print i } }  '

This will assume that string 'version=' will be only in one row of the file.
# 6  
Old 06-27-2013
Awesome! ThanksSmilie
# 7  
Old 06-27-2013
Another awk solution

Another one on similar lines

Code:
awk '{if($0 ~ /version=/) {start=1;next} if(start){print} }' filename

or

Code:
awk ' {if($0 ~ /version=/) LN=NR} {if ((LN!=0)&&(NR>LN)) {print }}' filename


Last edited by krishmaths; 06-27-2013 at 05:15 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Reading in 4th number in a line

I'd like to read a file (mydata.dat) line by line. The file consists of 5 columns filled with numbers, like so: 83.018 1.953 49.587 20550.000 353 83.213 1.953 49.195 20600.000 171 84.935 1.954 48.803 20650.000 920 For every read line (i.e. in every... (2 Replies)
Discussion started by: pina
2 Replies

2. Shell Programming and Scripting

Insert line dynamically in a file

Hi, I have a file which have several lines that ordered alphabetically. Each line is a tab-separated Something like: ABC_XX 12 BCC_XX 24 CDD_CC 12 :::::::::::: :::::::::::: PDD_EE 12 RXX_DD 24 :::::::::::: :::::::::::: Now i need to add the following lines PXX_FF 36 PYY_GG 36... (2 Replies)
Discussion started by: jakSun8
2 Replies

3. Shell Programming and Scripting

Write $line number into textfile and read from line number

Hello everyone, I don't really know anything about scripting, but I have to manage to make this script, out of necessity. #!/bin/bash while read -r line; do #I'm reading from a big wordlist instructions using $line done Is there a way to automatically write the $line number the script... (4 Replies)
Discussion started by: bobylapointe
4 Replies

4. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

5. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

6. Shell Programming and Scripting

Determine number of checklist in zenity dynamically...

Hi, In my Shell Script i am counting the duplicate IPs in LAN,...After counting i have to show in checklist in zenity which one to delete from the LAN........so initially i dont know no. of duplicate IPs in the LAN....Hence i can determine how many check list needed..... Duplicate IPs... (3 Replies)
Discussion started by: shivarajM
3 Replies

7. Shell Programming and Scripting

how to get the data from line number 1 to line number 100 of a file

Hi Everybody, I am trying to write a script that will get some perticuler data from a file and redirect to a file. My Question is, I have a Very huge file,In that file I have my required data is started from 25th line and it will ends in 100th line. I know the line numbers, I need to get all... (9 Replies)
Discussion started by: Anji
9 Replies

8. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies

9. Shell Programming and Scripting

Appending line number to each line and getting total number of lines

Hello, I need help in appending the line number of each line to the file and also to get the total number of lines. Can somebody please help me. I have a file say: abc def ccc ddd ffff The output should be: Instance1=abc Instance2=def Instance3=ccc Instance4=ddd Instance5=ffff ... (2 Replies)
Discussion started by: chiru_h
2 Replies
Login or Register to Ask a Question