Visit Our UNIX and Linux User Community


Help On Unix Script Count Line Of File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help On Unix Script Count Line Of File
# 1  
Old 02-08-2008
Help On Unix Script Count Line Of File

I have to compare the number of files of two files on a Shell Script:
I tryied with wc and wiith sed but I can not make an integer...

#!/bin/sh
.
.
n = ls -l | wc -l $file1
`echo "Line: "$n". ">>$LOGFILE`

xx = sed -n '$=' $file2
`echo "Line: "$xx". ">>$LOGFILE`

Aways ERRORRRRR!!!!!!!!!

Can enybody help me with this problem and explane to me how to use an output as an integer?

TK's alot
# 2  
Old 02-08-2008
Couple minor tweaks to your code

[my file names does have eleven lines]


> cat comp_val
#! /bin/bash

file1=names
LOGFILE=test.log


n=$(ls -l | wc -l $file1)
echo "Line: "$n". ">>$LOGFILE

xx=$(sed -n "$=" $file1)
echo "Line: "$xx". ">>$LOGFILE



> rm *.log
> comp_val
> cat *.log
Line: 11 names.
Line: 11.
>
# 3  
Old 02-11-2008
Tk you but still error...

Thenk's a lot for the response, but it still give me a:

...../Send..sh: syntax error at line 167: `n=$' unexpected

it looks like it doesn't like:
n=$(ls -| | wc -l $file)

any other suggestions, also because after I need to do some arithmethic operations with n, like:
n=$n-9


tk's again
# 4  
Old 02-11-2008
That is because you are using sh and trying to use bash specific constructs. This should work with sh:

Code:
$ cat ./comp_val
#!/bin/sh

file1=names
file2=${file1}.tmp
LOGFILE=test.log
cp $file1 $file2

n=`ls -l | wc -l $file1 |awk '{print $1}'`
echo "In $file2: Line: ${n}." > $LOGFILE

xx=`sed -n '$=' $file2`
echo "In $file1: Line: ${xx}. ">> $LOGFILE

echo "$n-2="`expr ${n} - 2` >> $LOGFILE

cat $LOGFILE
$
$
$ ./comp_val
In names.tmp: Line: 8.
In names: Line: 8.
8-2=6

HTH
# 5  
Old 02-11-2008
Tku Very Much!!!

It works perfect!!!

Thank you very much, it help me a lot!

Previous Thread | Next Thread
Test Your Knowledge in Computers #798
Difficulty: Medium
To convert from a base-10 integer to its base-2 (binary) equivalent, the number is divided by two and the remainder is the least-significant bit.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script UNIX to read text file line by line

i have a text file as belows, it includes 2 columns, 1st is the column name, 2nd is the file_name data_file.txt column_name file_name col1 file1 col2 file2 col3 file1 col4 file1 col5 file2 now, i would like to... (4 Replies)
Discussion started by: tester111
4 Replies

2. UNIX for Beginners Questions & Answers

UNIX script to check word count of each word in file

I am trying to figure out to find word count of each word from my file sample file hi how are you hi are you ok sample out put hi 1 how 1 are 1 you 1 hi 1 are 1 you 1 ok 1 wc -l filename is not helping , i think we will have to split the lines and count and then print and also... (4 Replies)
Discussion started by: mirwasim
4 Replies

3. Shell Programming and Scripting

To take line count from a file

Hi guys, I am having a file which where i need to take line count based on searching a particular string from a list say list_file.txt which occurs in 2nd column of my main file and to take the line count which doesnot exist in list file say list_file.txt for eg: my main file looks like this... (4 Replies)
Discussion started by: rohit_shinez
4 Replies

4. Shell Programming and Scripting

How to read a file, line by line in UNIX script?

#!/bin/bash file1="yxd" for file in `cat file3.txt`; do cat $file1 | awk '{split($1,dp,"."); lend=length(dp); if(lend>2) { dom=dp"."dp; split($2,ipl,","); for(i=1; i<=length(ipl); i++) { if(ipl~/:/) { if(ipl~/:/) i++; rip=ipl; i++; raq=ipl; rtq=0; si=i+4; for(;i<si;i++) rtq+=ipl; rsub="";... (2 Replies)
Discussion started by: veeruasu
2 Replies

5. UNIX for Dummies Questions & Answers

Script to count character present in a line

Suppose i have multiple line like below in a file. ASDFAFAGAHAHAHA AGAHAHAJGAFAGAH AHAHAKAHAHAHAKA I need a bash script to count a character and also Also count the number of character present in each line . suppose for line 1: A=x, S=y, D=x and so on and total character=15. where x y and z is... (3 Replies)
Discussion started by: XXLMMN
3 Replies

6. Shell Programming and Scripting

Want to write a script for mailing line count

Hi all, I have a directory in which files are formed daily at certain time.files have certain naming convention for ex .. File1TTT.DATddmmyyyyhrsminsec File2TTT.DATddmmyyyyhrsminsec I want to take their line count when they are formed and mail that line count. Please help how to... (2 Replies)
Discussion started by: ammbhhar
2 Replies

7. Shell Programming and Scripting

Shell script to count number of ~ from each line and compare with next line

Hi, I have created one shell script in which it will count number of "~" tilda charactors from each line of the file.But the problem is that i need to count each line count individually, that means. if line one contains 14 "~"s and line two contains 15 "~"s then it should give an error msg.each... (3 Replies)
Discussion started by: Ganesh Khandare
3 Replies

8. Shell Programming and Scripting

File Line Count

Hi, Came across a weird problem today. I was just trying to write this small script which would read the number of lines in a file. Depending on the count some further processing would be done. I used wc -l in order to get that done. But since it depends on the number of new line characters, if... (1 Reply)
Discussion started by: King Nothing
1 Replies

9. Shell Programming and Scripting

Get the line count from 2nd line of the file ?

Hi, I want to get the line count of the file from the 2nd line of the file ? The first line is header so want to skip that. Thanks. (8 Replies)
Discussion started by: smc3
8 Replies

10. UNIX for Dummies Questions & Answers

Want display a line with space in file by unix script

HI , I am having a file as -----------------a.out------------------- Hi I am unix developer using hp unix this is a test --------------------------------------- i need to read each line by a unix script of the file and to print in console with the space in the line as ... (9 Replies)
Discussion started by: arunkumar_mca
9 Replies

Featured Tech Videos