How to read from specific line number in shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read from specific line number in shell script?
# 8  
Old 01-04-2012
Splitting the file for every 10000 lines will work perfectly. But i still have know how many files have been created? And have to get the filenames from the directory to write them out as a xml.

It would be good if can have all of them in the original file and create a xml file for each 10000 lines.

---------- Post updated at 10:19 AM ---------- Previous update was at 09:41 AM ----------

Filenames generated by the awk script looks like

Code:
file.txt.0
file.txt.1
file.txt.2

How to get the number of files generated after this? Or it would be simple if we can able to append a temporary string in front of the filename to capture the temp files and write them as xmls

Say
tempfile1.txt
tempfile2.txt
tempfile3.txt
# 9  
Old 01-04-2012
Code:
maxline=10000
awk -vm="$maxline" 'END{print "The number of file generated will be : " int(NR/m)+!!(NR%m)}' yourinitialfile.txt

# 10  
Old 01-04-2012
borrowing from stsgnb:
Code:

awk '!(NR%10000){close(f);c++;=FILENAME"."c}{print $0 > f }END{print "created " c " files"}' yourfile

# 11  
Old 01-04-2012
Thanks a lot.. How to assign this value to a shell variable?
# 12  
Old 01-04-2012
Oooops lol, thx versh99

By the way i just tested the code and got an error message (even adding the missing f=FILENAME...)

Code:
awk: cannot open "" for output (No such file or directory)

And this code can also lead to bad file calculation if $maxline is an exact multiple of the number of line of the initial file because c would be incremented once too more.

so maybe there are some more simple way to write it but at least, the following should work:
Code:
maxline=10000
awk -vm="$maxline" 'BEGIN{c=1}{f=FILENAME"."c}NR%m==0{print $0 > f;close(f);c++;next}{print $0 > f }END{print int(NR/m)+!!(NR%m) " files generated" }' yourfile


Last edited by ctsgnb; 01-04-2012 at 11:58 AM..
# 13  
Old 01-04-2012
We have declared the variable 'c' here but how it got assigned to 0.

Also what exactly the below code snippet will do? Just trying to understand.

Code:
{print $0 > f }

# 14  
Old 01-04-2012
Code:
{print $0 > f}

does just print the current scanned line into the file f

The trick it that we make f refering to a name which varies depending the Number of Records (NR) (which -in our case- is the same as the line number since the record separator (RS) is by default the newline (\n) character)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script UNIX to read text file line by line

i have a text file as belows, it includes 2 columns, 1st is the column name, 2nd is the file_name data_file.txt column_name file_name col1 file1 col2 file2 col3 file1 col4 file1 col5 file2 now, i would like to... (4 Replies)
Discussion started by: tester111
4 Replies

2. Shell Programming and Scripting

How to change a number on a specific line with cshell or shell?

Hello all, I need to change a number in a file by adding some residuals respectively To make it clear, I need to add 0.11 to the number between 24-28 (which is below the SECON) for all the lines starting with FRR1 or I need to add 0.13 to the number between 24-28 (which is below the... (9 Replies)
Discussion started by: miriammiriam
9 Replies

3. Shell Programming and Scripting

Shell script to read multiple options from file, line by line

Hi all I have spent half a day trying to create a shell script which reads a configuration file on a line by line basis. The idea of the file is that each will contain server information, such as IP address and various port numbers. The line could also be blank (The file is user created). Here... (1 Reply)
Discussion started by: haggismn
1 Replies

4. Shell Programming and Scripting

Shell script to read a text file line by line & process it...

Hi , I am trying to write an shell, which reads a text file (from a location) having a list of numbers of strictly 5 digits only ex: 33144 Now my script will check : 1) that each entry is only 5 digits & numeric only, no alphabets, & its not empty. 2)then it executes a shell script called... (8 Replies)
Discussion started by: new_to_shell
8 Replies

5. Shell Programming and Scripting

Using awk to read a specific line and a specific field on that line.

Say the input was as follows: Brat 20 x 1000 32rf Pour 15 p 1621 05pr Dart 10 z 1111 22xx My program prompts for an input, what I want is to use the input to locate a specific field. Like if I type in, "Pou" then it would return "Pour" and just "Pour" I currently have this line but it is... (6 Replies)
Discussion started by: Bungkai
6 Replies

6. Shell Programming and Scripting

How to read specific line of text from a Script and send email notification

Hi ! I am a newbie and never officially wrote a shell script before. The requirement for this script is : 1) Read a file called 'bpm.log' and identify if it has a specific text such as 'this is the text'. Its a static value and that is the only text we need to read. 2) If that... (2 Replies)
Discussion started by: atechcorp
2 Replies

7. 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

8. Shell Programming and Scripting

A script that read specific fileds from the 7th line in a file

Dear All, I need a unix script that will read the 7th line and especially these fileds from a file Mo speed 16, Mt speed 15 every 15 minutes starting from 00:00 to 23:45 on daily basis and put the result in a txt file and name it MT_MO_20090225.txt, please also note that the system date format... (2 Replies)
Discussion started by: samura
2 Replies

9. 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

10. Shell Programming and Scripting

Shell Script to read specific lines in a file

I have a file with contents as follows Record 1: Rejected - Error on table "DWO"."P2G_CUST_EVENTS". ORA-00001: unique constraint (DWO.CUST_EVENTS_PK) violated Record 5: Rejected - Error on table "DWO"."P2G_CUST_EVENTS". ORA-00001: unique constraint (DWO.CUST_EVENTS_PK) violated Record 6:... (5 Replies)
Discussion started by: varshanswamy
5 Replies
Login or Register to Ask a Question