943,908 Members | Top Members by Rank

Ad:
Oct 25th, 2007
0

need help with writing shell script

Expand Post »
I want to get out put as folows:

Shell Scripting Syntax (Toggle Plain Text)
  1. 100 HE01 {number of line in this file} {file created on time stamp}
  2. 200 LTXS {number of line in this file} {file created on time stamp}
  3.  
  4. (if the file does not exist then it should say FILE NOT AVAILABLE

to start with I have a input file (inputfile.txt) that contains following data:

Shell Scripting Syntax (Toggle Plain Text)
  1. 100 HE01 file1.txt
  2. 200 LTXS file2.txt

and file1.txt & file2.txt are in the current directory. there is a chance that they file may not exist.

I tried for loop, wc -l, grep, cut but the out put is not very satis factory.

Don't hesitate to give me any go-to or pointer to finding the solution. I don't expect you to do my work ;-)
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
n2201 is offline Offline
6 posts
since Oct 2007
Oct 26th, 2007
0

Re: need help with writing shell script

#!/bin/ksh

cat inputfile.txt|while read v w x
do
if [ -e $x ]
then
num_lines=`wc -l $x|sed -e 's/ *//' -e 's/ .*$//'`
time_stamp=`ls -l $x|awk '{print $6 "_" $7 "_" $8}'`
echo "$v $w $x $num_lines $time_stamp"
fi
done

That will give you output like this (when both files exist - either one would just not do anything if they weren't there)

100 HE01 file1.txt 3 Oct_25_22:27
200 LTXS file2.txt 12 Oct_25_22:27

Hope that helps

, Mike
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007
Oct 26th, 2007
0

Re: need help with writing shell script

Thank you Mike,

This was perfect. This is exactly what I was looking for!

I guess I cannot thank you enough :-)

Nikhil
Reputation Points: 10
Solved Threads: 0
Newbie Poster
n2201 is offline Offline
6 posts
since Oct 2007
Oct 28th, 2007
0

Re: need help with writing shell script

Click to Expand / Collapse  Quote originally posted by eggi ...
#!/bin/ksh

cat inputfile.txt|while read v w x
do
if [ -e $x ]
then
num_lines=`wc -l $x|sed -e 's/ *//' -e 's/ .*$//'`
time_stamp=`ls -l $x|awk '{print $6 "_" $7 "_" $8}'`
echo "$v $w $x $num_lines $time_stamp"
fi
done
no need for cat
Shell Scripting Syntax (Toggle Plain Text)
  1. while read ....
  2. do
  3. ....
  4. done < inputfile.txt
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Oct 28th, 2007
0

Re: need help with writing shell script

Pick your poison, I guess - cat or redirect STDIN.

Thanks for the alternative suggestion

, Mike
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007
Oct 28th, 2007
0

Re: need help with writing shell script

Meant to write - cat or redirect output from file. I really have to stop posting on weekend mornings

Again, thanks

, Mike
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007
Nov 9th, 2007
0

Re: need help with writing shell script

Mike (eggi),

your suggestion is correct. I hit this bug with the cat option: http://osdir.com/ml/shells.bash.bugs.../msg00023.html

Some how if you cat and pipe then the varibales are all converted to local variables within the while loop. I had to use the redirect input to over come the issue: Here is how I did it finally:

Quote ...
while read v w x
do
if [ -e /usr/task/shared/$x ];
then
{ num_lines=`wc -l /usr/task/shared/$x|sed -e 's/ *//' -e 's/ .*$//'`
time_stamp=`ls -l /usr/task/shared/$x|awk '{print $6 " " $7 " " $8}'`
echo "$v $w $time_stamp $num_lines" >> outputfile.txt
let total+=num_lines
}
else
{ echo "$v $w FILE DOES NOT EXIST" >> outputfile.txt
}
fi
done < /usr/task/lst_New_Leads_Camp.txt
echo "---------------------------------------------" >> outputfile.txt
echo " Total leads: ${total} " >> outputfile.txt
echo "---------------------------------------------" >> outputfile.txt
Reputation Points: 10
Solved Threads: 0
Newbie Poster
n2201 is offline Offline
6 posts
since Oct 2007
Nov 9th, 2007
0

Re: need help with writing shell script

Cool - that's good to know. I've never hit that bug, but I'll be sure not to now

BTW, Credit GhostDog with the suggestion to redirect, I suggested using cat, so I don't deserve the thanks - but it's the thought, or gesture, that counts

Take it easy

, Mike
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Replace
Next Thread in Shell Scripting Forum Timeline: Shell program, help required.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC