Count lines and use if then ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count lines and use if then ksh
# 1  
Old 05-23-2012
Count lines and use if then ksh

I try to count number of lines of a data.txt file and then if number of lines is greater than 1 then email me the file.
I could not find what is wrong with my code, hope you can point out the mistake i made

Code:
#! /bin/ksh

count =`cat /from/file/data.txt | wc -l`

if [[ $count -gt 1 ]]; then

mailx -s "Duplicate " myname@somewhere.com < /from/file/data.txt
fi

sabercats:~/tools/cgi-bin> test.ksh
=2 sabercats:~/tool/cgi-bin>

I only see =2 what is this? this is count =`cat /from/file/data.txt | wc -l`

How it got out there and no mail for me?
Thanks
# 2  
Old 05-23-2012
Hi
In the line

Code:
count =`cat /from/file/data.txt | wc -l`

remove the space between count and =.
# 3  
Old 05-23-2012
Further to this you can leave out the cat and the pipe
Code:
count=$(wc -l < /from/file/data.txt)

# 4  
Old 05-23-2012
I do something similar but diferently:

Code:
COUNT=`wc -l $FILE | awk '{print $1}`
if [ ${COUNT} == 1 ]
   then
     mailx -x "Duplicate" myname@somewhere.com < from/file/data.txt 
  fi
done

If you were going to do this inside a directory where you have a bunch of files, you could something similar to this:

Code:
for FILE in `ls *.txt`  # assuming you're only looking at txt files
do
  COUNT=`wc -l $FILE | awk '{print $1}'`
  if [ ${P1} == 1 ]
    then
      mailx statement
  fi
done

# 5  
Old 05-23-2012
Thanks, work great after i remove a space before =
This also look cool, count=$(wc -l < /from/file/data.txt)
Thanks for all of your advice,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

UNIX ksh - To print the PID number and repeat count

This question is asked in an interview today that I have to return output with each PID number and the count of each PID number logged today. Here is the script that I have written. Can you confirm if that would work or not. The interviewer didn't said if my answer is correct or not. Can someone... (5 Replies)
Discussion started by: Subodh Kumar
5 Replies

2. Shell Programming and Scripting

Count lines in section

I am tiring to cont numbers of line between the "!" in CISCO routers I have no problem to extract the input and change the empty line with ! ! 5 Cable5/0/1 U0 4 5 Cable5/0/1 U1 4 ! 5 Cable5/0/1 U2 4 ... (4 Replies)
Discussion started by: sharong
4 Replies

3. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

4. Shell Programming and Scripting

Count lines

Hello, I have a file with two columns like the following FILE1: chr1 61042 chr1 61153 chr1 61446 chr1 61457 chr1 61621 chr10 61646 chr10 61914 chr10 62024 chr10 62782 Alos, I have another file FILE2: (13 Replies)
Discussion started by: rkk
13 Replies

5. UNIX for Dummies Questions & Answers

How do I count how many times a specific word appear in a file (ksh)?

Hi Please can you help how do I count the number of specific characters or words that appear in a file? (8 Replies)
Discussion started by: fretagi
8 Replies

6. Shell Programming and Scripting

KSH: Split String into smaller substrings based on count

KSH HP-SOL-Lin Cannot use xAWK I have several strings that are quite long and i want to break them down into smaller substrings. What I have String = "word1 word2 word3 word4 .....wordx" What I want String1="word1 word2" String2="word 3 word4" String3="word4 word5" Stringx="wordx... (5 Replies)
Discussion started by: nitrobass24
5 Replies

7. Shell Programming and Scripting

Ksh Searching for a string within a file and keeping a count= get the following Sample01=: command

I have a script that goes through a 24 hr logfile, And i want to count the instances of a Test01 to 83 and output the sum of all the instances over 24hrs #/bin/ksh cat $parse_data | awk '/'$time$i'/ {for(x=0; x<=16; x++) {getline; print}print "--" }' > _hr.txt for... (2 Replies)
Discussion started by: k00061804
2 Replies

8. UNIX for Dummies Questions & Answers

Help with grep & count in ksh

Hello, I have input file with queue names No.esprd.Queue|No.esdev.Queue|||n|120|No_User||No_User| No.esdev.Queue|No.esdev.Queue|||n|120|No_User||No_User| I have to check if the input file contains word "esprd" and the number of times it occurs. I will have to do further... (22 Replies)
Discussion started by: Green_Star
22 Replies

9. Shell Programming and Scripting

Count certain lines

Hi! I have a file that looks like this: AAG ---------------------------------------------------------------------- Number of residues in the repeat = 3 AGA ---------------------------------------------------------------------- Number of residues in the repeat = 3 AGG ... (2 Replies)
Discussion started by: vanesa1230
2 Replies

10. UNIX for Dummies Questions & Answers

How to count lines - ignoring blank lines and commented lines

What is the command to count lines in a files, but ignore blank lines and commented lines? I have a file with 4 sections in it, and I want each section to be counted, not including the blank lines and comments... and then totalled at the end. Here is an example of what I would like my... (6 Replies)
Discussion started by: kthatch
6 Replies
Login or Register to Ask a Question