Bash script to read a file from particular line till required line and process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script to read a file from particular line till required line and process
# 1  
Old 11-03-2014
Bash script to read a file from particular line till required line and process

Hi All,

Am trying to write wrapper shell/bash script on a utility tool for which i need to pass 2 files as arugment to execute utility tool.

Wraper script am trying is to do with above metion 2 files.

utility tool accepts :
a. userinfo file : which contains username
b. item file : which contains items to be processed by mentioned users from userinfo file.

Below script works fine for userfile and itemfile

Am thinking of writing a wrapper script which call this script but in the wapper script it should execute utility tool from specified lines to perticular line from userfile and even for itemfile.

for ex : say userfile has 100 users and itemfile has 1000 items. so, i should call the wrapper script with passing argument saying run script for users from line 20-45 from userfile and say item from line 250-400 from itemfile.

basically it should run the utility tool from specified user number to a specfied item numbers.

Code:
#!/bin/bash

#echo "***********************************************"

count=1
userFile=$1

if [ -f $userFile ]; then
 echo "userfile exist"
else
 echo "user file $userFile do not exit, exiting !!!"
 exit 1
fi

for user in ` cat $userFile ` ; do
./dmstool -S rms -h 10.212.45.67 -f /tmp/itemfile -m -u $user
   #sleep 5
    echo "Completed - user $count"
   (( count=$count+1 ))
done

Any help will be much appreciated.

Thanks

Last edited by Optimus81; 11-03-2014 at 05:51 PM.. Reason: code tag added
# 2  
Old 11-03-2014
I guess this is it Smilie WARNING: There is no error checking at all!

Code:
#!/bin/bash

ufile=$1 #user file name
ulf=$2 #user line from
ult=$3 #user line to

ifile=$4 #item file name
ilf=$5 #item line from
ilt=$6 #item line to

for user in `awk -vULF=$ulf -vULT=$ult 'NR>=ULF && NR<=ULT' $ufile` ; do
 for item in `awk -vILF=$ilf -vILT=$ilt 'NR>=ILF && NR<=ILT' $ifile` ; do
  echo $user @ $item
 done
done

Demonstration:
Code:
$ for i in {1..100}; do echo user$i >>userlist; done
$ for i in {1..100}; do echo item$i >>itemlist; done
$
$ ./script.sh userlist 23 30 itemlist 60 65
user23 @ item60
user23 @ item61
user23 @ item62
user23 @ item63
user23 @ item64
user23 @ item65
user24 @ item60
user24 @ item61
user24 @ item62
user24 @ item63
user24 @ item64
user24 @ item65
user25 @ item60
user25 @ item61
user25 @ item62
user25 @ item63
user25 @ item64
user25 @ item65
user26 @ item60
user26 @ item61
user26 @ item62
user26 @ item63
user26 @ item64
user26 @ item65
user27 @ item60
user27 @ item61
user27 @ item62
user27 @ item63
user27 @ item64
user27 @ item65
user28 @ item60
user28 @ item61
user28 @ item62
user28 @ item63
user28 @ item64
user28 @ item65
user29 @ item60
user29 @ item61
user29 @ item62
user29 @ item63
user29 @ item64
user29 @ item65
user30 @ item60
user30 @ item61
user30 @ item62
user30 @ item63
user30 @ item64
user30 @ item65
$

# 3  
Old 11-07-2014
Thanks junior-helper for the help.

Sorry i was struck with imp release.

Now am back.

I tried what you said but it doesn't do anything, script just run and comeback.

Code:
admin@HP1 # cat script.sh
#!/usr/bin/bash

ufile=$1 #user file name
#ulf=$2 #user line from
ulf=25 #user line from
ult=30 #user line to

ifile=$4 #item file name
ilf=$5 #item line from
ilt=$6 #item line to

for user in `awk -vULF=$ulf -vULT=$ult 'NR>=ULF && NR<=ULT' $ufile` ; do
 for item in `awk -vILF=$ilf -vILT=$ilt 'NR>=ILF && NR<=ILT' $ifile` ; do
  echo $user @ $item
 done
done

I have both itemlsit and userlist file with 100 lines data in each

admin@HP1 # wc -l userlist itemlist
100 userlist
100 itemlist
200 total

Am i missing something here ?

My OS is :
uname -a
SunOS HP1 5.10 Generic_144489-09 i86pc i386 i86pc

Please let me know
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

With script bash, read file line per line starting at the end

Hello, I'm works on Ubuntu server My goal : I would like to read file line per line, but i want to started at the end of file. Currently, I use instructions : while read line; do COMMAND done < /var/log/apache2/access.log But, the first line, i don't want this. The file is long... (5 Replies)
Discussion started by: Fuziion
5 Replies

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

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

4. Shell Programming and Scripting

Read file line by line and process the line to generate another file

Hi, i have file which contains data as below(Only sample shown, it may contain more data similar to the one shown here) i need to read this file line by line and generate an output file like the one below i.e based on N value the number of MSISDNs will vary, if N=1 then the following... (14 Replies)
Discussion started by: aemunathan
14 Replies

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

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

7. UNIX for Advanced & Expert Users

Urgent Help required : awk/sed help to find pattern and delete till end of line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (1 Reply)
Discussion started by: rajan_san
1 Replies

8. UNIX for Dummies Questions & Answers

how to read a file till it encounters a blank line

Hi , I want to read a file starting with "*" up to till it encounters a blank line and to redirect this output to a different file.Plz suggest how to write a script for this. e.g:- * PK Sent Email (11.23) CALYPSO 1243215 9116457 NEW TRAD FAILED Nov 23 2007 9:34AM OASYS: DPS: SINGCORP invalid... (1 Reply)
Discussion started by: adityam
1 Replies

9. Shell Programming and Scripting

how to read a file till it encounters a blank line

Hi , I want to read a file starting with "*" up to till it encounters a blank line and to redirect this output to a different file.Plz suggest how to write a script for this. e.g:- * PK Sent Email (11.23) CALYPSO 1243215 9116457 NEW TRAD FAILED Nov 23 2007 9:34AM OASYS: DPS: SINGCORP invalid... (1 Reply)
Discussion started by: adityam
1 Replies

10. Post Here to Contact Site Administrators and Moderators

how to read a file till it encounters a blank line

Hi , I want to read a file starting with "*" up to till it encounters a blank line and to redirect this output to a different file.Plz suggest how to write a script for this. e.g:- * PK Sent Email (11.23) CALYPSO 1243215 9116457 NEW TRAD FAILED Nov 23 2007 9:34AM OASYS: DPS:... (0 Replies)
Discussion started by: adityam
0 Replies
Login or Register to Ask a Question