•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 456,573 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,565 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 1747 | Replies: 7
![]() |
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
I want to get out put as folows:
to start with I have a input file (inputfile.txt) that contains following data:
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 ;-)
100 HE01 {number of line in this file} {file created on time stamp}
200 LTXS {number of line in this file} {file created on time stamp}
(if the file does not exist then it should say FILE NOT AVAILABLEto start with I have a input file (inputfile.txt) that contains following data:
100 HE01 file1.txt 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 ;-)
•
•
Join Date: Oct 2007
Posts: 306
Reputation:
Rep Power: 2
Solved Threads: 29
#!/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
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
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
•
•
Join Date: Apr 2006
Posts: 140
Reputation:
Rep Power: 3
Solved Threads: 26
•
•
•
•
#!/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
while read .... do .... done < inputfile.txt
•
•
Join Date: Oct 2007
Posts: 306
Reputation:
Rep Power: 2
Solved Threads: 29
Pick your poison, I guess - cat or redirect STDIN.
Thanks for the alternative suggestion
, Mike
Thanks for the alternative suggestion

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
•
•
Join Date: Oct 2007
Posts: 306
Reputation:
Rep Power: 2
Solved Threads: 29
Meant to write - cat or redirect output from file. I really have to stop posting on weekend mornings 
Again, thanks
, Mike

Again, thanks

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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:
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:
•
•
•
•
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
•
•
Join Date: Oct 2007
Posts: 306
Reputation:
Rep Power: 2
Solved Threads: 29
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

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
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
![]() |
•
•
•
•
•
•
•
•
DaniWeb Shell Scripting Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Shell Script for Load monitoring! (Shell Scripting)
- exiting the shell within a shell script (Shell Scripting)
- Shell Script Issue (Shell Scripting)
- Linux Shell Script needs help writing program (Shell Scripting)
- Linux Shell Script (Shell Scripting)
- Problem with variables in Windows shell script (Windows NT / 2000 / XP / 2003)
- Shell Script for Gnome 2.6 Print Manager. (Shell Scripting)
- How to su properly in a shell script (Shell Scripting)
Other Threads in the Shell Scripting Forum
- Previous Thread: Replace
- Next Thread: Shell program, help required.


Linear Mode