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?
# 15  
Old 01-04-2012
Hi ctsgnb,

If there a way that we can append a temporary string to the newly created files. So that we will be able to differentiate between the original file. So this will work fine, even without knowing about total number of files generated.
# 16  
Old 01-04-2012
Yes :

Code:
echo "temporary string" >> newly_created_file

Code:
for i in origfile.*
do
echo "temporary string" >> $i
done

This User Gave Thanks to ctsgnb For This Post:
# 17  
Old 01-04-2012
sorry for the copy/paste snafu:
Code:
nawk 'NR%10000==1 && ++c{close(f);f=FILENAME "."c}{print $0 > f }END{print "created " c " files"}' myFile

This User Gave Thanks to vgersh99 For This Post:
# 18  
Old 01-04-2012
...borrowed from versh99 Smilie

just putting the line number into a variable

Code:
maxline=10000
awk -vm="$maxline" 'NR%m==1 && ++c{close(f);f=FILENAME "."c}{print $0 > f }END{print "created " c " files"}' yourfile.txt

# 19  
Old 01-04-2012
I also think that split might be the best option.

If you create a temporary directory and make that the current location, you could then use things like:-
  • split -l 10000 $filename
  • wc -l *
  • ls -l
If the creted filenames are awkward, you can use options in split to set the beggining and ending of the filename to suit better. Check your manual pages to get your OS specific flags to use.

You can fairly easily then read around them. If you were looking for a specific line number, then the sed to get a specific line reference by variable $line is:
Code:
sed -n ${line}p $filename

The p is required, but is not part of the variable, hence the {} to clearly identify it.



i hope that this helps.



Robin
Liverpool/Blackburn
UK
# 20  
Old 01-05-2012
Thanks to all for helping me out here.
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