Reading from a file with limit number of lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading from a file with limit number of lines
# 1  
Old 11-05-2012
Reading from a file with limit number of lines

Hi,

I am trying to pull data from a txt file which has 51 lines

Example
Code:
AK
AR
AL
AZ
CA
CO
CT

Now i want to send above data as input to script but i want to run them twice at a time in a nohup

like
nohup Script_name AK &

Nohup Script_name AR &

And to run the remaining two it should complete the nohup execution for AK & AR. I need a shell script guys..


Please help..
Thanks
# 2  
Old 11-05-2012
Code:
typeset -i counter=0
typeset prev_param
typeset curr_param

while read in
do
        counter=`expr $counter + 1`
        modulus=`expr $counter % 2`

        if [ $modulus -eq 0 ]
        then
                curr_param=$( echo $in )
                # nohup script $prev_param &
                # nohup script $curr_param &
                wait
        else
                prev_param=$( echo $in )
        fi
done < input_file

nolines=`wc -l input_file | awk ' { print $1 } '`
modulus=`expr $nolines % 2`

# Execute last input if the number of lines is ODD
if [ $modulus -ne 0 ]
then
        # nohup script $prev_param &
fi

I hope this helps.
This User Gave Thanks to Yoda For This Post:
# 3  
Old 11-05-2012
Thanks a lot bipinajith....

Is that we don't need to include "wait" commd....anhwhere...
# 4  
Old 11-05-2012
The skeleton of a different approach:
Code:
paste ... | while read ...; do
    nohup ... &
    nohup ... &
    wait
done

Regards,
Alister

---------- Post updated at 07:10 PM ---------- Previous update was at 07:01 PM ----------

Quote:
Originally Posted by bipinajith
Code:
wc -l input_file | awk ' { print $1 } '

When it's invoked without file operands, wc will not print a pathname. If you aren't interested in the filename and you're only working with one file, you can just redirect stdin.

Instead of
Code:
wc -l filename | awk '{print $1}'

you can use
Code:
wc -l < filename

Regards,
Alister
These 2 Users Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

BASH logging to file, limit lines

BASH Gurus: Anyone know how to append continuous output command appending to a file, but limit that file to no more than 20 lines? The program I have running is simply monitoring my UDP port 53 for incoming packets endlessly. I just need to keep this file from going over 20 lines. Once the file... (3 Replies)
Discussion started by: scorpius2k1
3 Replies

2. Shell Programming and Scripting

Egrep patterns in a file and limit number of matches to print for each pattern match

Hi I need to egrep patterns in a file and limit number of matches to print for each matched pattern. -m10 option is not working out in my sun solaris 5.10 Please guide me the options to achieve. if i do head -10 , i wont be getting all pattern match results as output since for a... (10 Replies)
Discussion started by: ananan
10 Replies

3. Shell Programming and Scripting

Reading in two lines at once from a text file

Hello everyone, I have thought about this for quite some time and know what I want to do but am having some trouble at it. I have a text file filled with numbers like this, there are more in the file obviously. Each number is separated by a space. 1 3 2 4 5 1 -1 1 0 -1 5The idea is... (7 Replies)
Discussion started by: tastybrownies
7 Replies

4. UNIX for Dummies Questions & Answers

Is there a limit in number of lines to be Copy pasted in VI editor ?

In my old shop, we only had AIX machines there (all of version 6.1 ). FTP ports were not open for these AIX machines because of some security thing. So, we can't ftp scripts in ASCII mode. When we wanted to copy huge scripts (shell scripts, sql scripts , ..etc) from our Windows based laptop... (6 Replies)
Discussion started by: kraljic
6 Replies

5. Shell Programming and Scripting

Regarding reading lines into a new file

Hi all, I jut use a loop to read lines from the user and redirect it to a file. echo "Enter the line" while read -r LINE do echo $LINE >> FILE if ;then break fi done input app... (1 Reply)
Discussion started by: Ananthdoss
1 Replies

6. Shell Programming and Scripting

skip lines while reading a file

Hi Experts, I am tryin to read a file and while doing so i need to skip the lines which start with a hash (#) char. I thought of using a goto command but a lot of guys on this site say its not the good way to program. Moreover I am using a ksh shell which deos not support goto command. ... (4 Replies)
Discussion started by: bankimmehta
4 Replies

7. Shell Programming and Scripting

Reading n number of lines after finding string

I am trying to search a file for a value: "Top 30 reject reasons" and want the next 30 lines after that and output in a text file. If I knew the line number, I can use a combination of head and tail commands to get my results, but this doesn't seem to work when I don't have a line number. I... (2 Replies)
Discussion started by: oriqin
2 Replies

8. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 Replies

9. Shell Programming and Scripting

Limit of number of lines for awk and sed.

Hi, I'm using awk and sed to extract some data out from a text file. The text file consists of data over a million (prolly millions) of lines. Question: Is there a limit of number of lines for awk and sed? Thanks in advance. (2 Replies)
Discussion started by: 60doses
2 Replies

10. UNIX for Dummies Questions & Answers

skip reading certain lines in a file

How can I exclude reading lines in a file that contains the following: filesystem:/home/pach/liv_patches 128005120 88456640 37270758 71% /home/patches That is, all lines that contain and begins with filesystem: should not be processed/read from a file (5 Replies)
Discussion started by: paulsew
5 Replies
Login or Register to Ask a Question