The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need to solve complex network problem soliberus IP Networking 4 02-24-2008 10:30 AM
Complex problem about nested for loops Silverlining Shell Programming and Scripting 4 10-18-2007 05:19 PM
problem with count uni_ajay_r Shell Programming and Scripting 5 11-06-2006 08:14 AM
How to count the record count in an EBCDIC file. oracle8 UNIX for Dummies Questions & Answers 1 07-26-2006 08:22 PM
Complex Pipeline/Redirection/Regular Expression problem netmaster UNIX for Dummies Questions & Answers 1 11-29-2005 12:55 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-22-2008
bbergstrom74 bbergstrom74 is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 3
Complex file count problem

Hi all!

I have a question regarding possibilities to do line counts.

SEARCH_VAR=TEX
rsh $REM_HOST -l $REM_USER "cd $REM_DIR; ls *$SEARCH_VAR* 2> /dev/null" | sort -n | awk 'BEGIN { FS = "-" } ; { print $1"\t"$0 }'

Will produce an output on the screen like this:

483 483-SOME-TEXT-1
643 643-SOME-TEXT-2
692 692-SOME-TEXT-3
723 723-SOME-TEXT-4

For the files I'm searching for. This is exactly what I want I also what I get at the moment. But if I search for something and no files are found it would be nice to have a message like 'No files match the search criteria'. I need both the output on the screen and the file count.

I guess I could do it like this:

First this to get the desired output on the screen:
rsh $REM_HOST -l $REM_USER "cd $REM_DIR; ls *$SEARCH_VAR* 2> /dev/null" | sort -n | awk 'BEGIN { FS = "-" } ; { print $1"\t"$0 }'

Then this to get the count of the files:
LINECOUNT=$(rsh $REM_HOST -l $REM_USER "cd $REM_DIR; ls *$SEARCH_VAR* 2> /dev/null | wc -l")

But that I don't like for two reasons. I have to do two remote shell sessions and also the code gets ugly...

Doing like this will get the file count printed on the screen but I need it in a variable that I can use to compare with later.
rsh $REM_HOST -l $REM_USER "cd $REM_DIR; ls *$SEARCH_VAR* 2> /dev/null" | sort -n | awk 'BEGIN { FS = "-" } ; { print $1"\t"$0 filecount++ } ; END { print filecount }'

Like:
if [[ $FILECOUNT = "0" ]]; then

Hopefully all in one go... Is this at all possible...? Using awk somehow? Assign the "filecount" in the awk script to a variable?

Oh, this is in Korn shell.

Thanks for any help!
  #2 (permalink)  
Old 01-22-2008
strobotta strobotta is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 10
Quote:
Originally Posted by bbergstrom74 View Post
rsh $REM_HOST -l $REM_USER "cd $REM_DIR; ls *$SEARCH_VAR* 2> /dev/null" | sort -n | awk 'BEGIN { FS = "-" } ; { print $1"\t"$0 }'
rather printing the output, redirect it to a file i.e. add to the line above:
Code:
> /tmp/file_count.$$
Then on Bash (sorry no Korn shell here available):

Code:
lines=`cat  /tmp/file_count.$$ | wc -l`

if [ $lines -eq 0 ]; then
  echo "mo match"
else
  cat /tmp/file_count.$$
  echo "$lines matches found"
fi
rm /tmp/file_count.$$
HTH
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:29 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0