Read a file from the nth line on


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read a file from the nth line on
# 1  
Old 03-20-2013
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.

Code:
#! /bin/ksh
set -x
while read line
do
$line.ksh
if [ $? -ne 0 ]
    # mail the team
fi
done <"$file"

The job file will be like
Code:
abcd
efgh
ijkl
mnop
qrst

This is working fine. I need to add the functionality, if the job fails in ijkl, when i run the job second time after fixing the issues with job ijkl. I need my script to run from ijkl.

If everything ran fine. It needs to start from the first job abcd.

Any idea how to go about it?

Last edited by zaxxon; 03-20-2013 at 04:36 PM.. Reason: code tags
# 2  
Old 03-20-2013
several ideas, if you know it failed on the nth line, your can use the tail command to chop off the first lines (calculate the number using wc -l on the whole file to find the total number of lines).

Or you can have some logic in the loop not to take an action if a loop index value is less (or equal depending on implementation) than the last fail value (which can be taken from the index of a previous run). When there is no previous fail the same code will with with 0 or 1.

Mike
# 3  
Old 03-20-2013
Assuming script does not run to completion if a job fails, something like the following might work:

At top of script, put:
Code:
touch jobs_done.txt

In the loop, before each job runs, put test condition:
Code:
grep -q $line jobs_done.txt
if [ $? -ne 0 ]; then continue; fi

In the loop, after job runs, put following line:
Code:
echo $line >> jobs_done.txt

At end of script, put:
Code:
rm jobs_done.txt

If script runs to completion, even if job fails, then the basic idea may still work, but 1) don't delete jobs_done.txt at the end, 2) add some code (FAIL or SUCCESS) to the jobs_done.txt file and also grep for that code to determine what to do.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

2. Post Here to Contact Site Administrators and Moderators

How to read the nth character from the line.?

I have Index Line and I tried to get the 9th character from the file and to check the character is "|" or not. Shell Scripting. Sample Index file. "91799489|K8E|188.004.A.917994892.1099R.c.01.pdf|2013|10/15/2014|002|B|C|C"... (3 Replies)
Discussion started by: pavand
3 Replies

3. UNIX for Beginners Questions & Answers

Insert a line of text on nth line of a file

Hi All, I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise My intention is to insert a line of text after 13th line of every file inside a particular directory. While trying to do it for a single file , i am using sed sed '3 i this is the 4th line' filename sed: command garbled: 3... (5 Replies)
Discussion started by: gotamp
5 Replies

4. Shell Programming and Scripting

Print nth line in a file

Bash/Oracle Linux 6.4 A basic requirement. How can I get nth line of a file printed ? Can I use grep in this case ? Example: In the below file, 12th line is "Kernel parameter check passed for rmem_max" . I just want the 12 line to be printed. # cat sometext.txt Kernel version check... (2 Replies)
Discussion started by: John K
2 Replies

5. Shell Programming and Scripting

How to read nth word from delimited text file?

Hi i am new in scripting how i can get 2 elements from first line of delimited txt file in shell scripts. AA~101010~0~AB~8000~ABC0~ BB~101011~0~BC~8000~ABC~ CC~101012~0~CD~8000~ABC0~ DD~101013~0~AB~8000~ABC~ AA~101014~0~BC~8000~ABC0~ CC~101015~0~CD~8000~ABC~ can anyone plse help?... (3 Replies)
Discussion started by: sushine11
3 Replies

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

7. Shell Programming and Scripting

Read file from nth line to specific character

Hi, I want to read the file from nth line (where n is an integer) to until I encounter @ char. Can any one please help me how to do this? Thanks. (3 Replies)
Discussion started by: laalesh
3 Replies

8. Shell Programming and Scripting

How to start reading from the nth line till the last line of a file.

Hi, For my reuirement, I have to read a file from the 2nd line till the last line<EOF>. Say, I have a file as test.txt, which as a header record in the first line followed by records in rest of the lines. for i in `cat test.txt` { echo $i } While doing the above loop, I have read... (5 Replies)
Discussion started by: machomaddy
5 Replies

9. Shell Programming and Scripting

Read last word of nth line

Hi people; i want to read the last word of the 14th line of my file1.txt. Here is the EXACT 14th line of the file. 250 SectorPortnum=3,AuxPortInUngo=2,PortDeviceGroup=1,PortDeviceSet=1,PorDevice=1 20 >>> Set. i have to get the word Set. how can i call it and also how... (3 Replies)
Discussion started by: gc_sw
3 Replies

10. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies
Login or Register to Ask a Question