Visit Our UNIX and Linux User Community


Wierd issue using wc -l in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Wierd issue using wc -l in a script
# 1  
Old 08-04-2009
Question Wierd issue using wc -l in a script

Hi experts,
This is what Im doing...
Code:
 file_cnt=`cat abc_ | wc -l`
head -$file_cnt abc > abc_2
 
if [ "$?" != "0" ]; then
   error "failed on rename abc_ > abc_2"
   exit 1
fi

 
try_run "rm -f abc_"
try_run "mv abc_2 abc_"

This is what the output on screen, I get on executing---->
+ + wc -l
+ cat abc_
file_cnt= 236
+ head - 236 abc_
-: No such file or directory
236: No such file or directory
+ [ 1 != 0 ]
+ error failed on rename abc_ > abc_2
+ exit 1


I aint able to understand why i get the space between the dash and 236
head - 236 abc_

Please suggest... Many Thanks
# 2  
Old 08-04-2009
Looks like a typo to me:
Code:
head -$file_cnt abc > abc_2

should read:

Code:
head -$file_cnt abc_ > abc_2

Although that means all the lines in abc_ will end up on abc_2?

What would be better is to define:
FILE=abc_
at the top of the script and the use ${FILE} from then on.
# 3  
Old 08-04-2009
Ignoring the typo abc_ .
This should fix it.

Code:
file_cnt=`cat abc | wc -l | sed -e "s/ //g"`

I have seen this issue in various unixes.
Had you stated which Operating System and Shell you were using the answer would be more specific.
# 4  
Old 08-04-2009
Quote:
Originally Posted by methyl
Ignoring the typo abc_ .
This should fix it.

Code:
file_cnt=`cat abc | wc -l | sed -e "s/ //g"`

I have seen this issue in various unixes.
Had you stated which Operating System and Shell you were using the answer would be more specific.

That works perfectly... Smilie thanks a lot....

First the shell is ksh

Now please let me tell you why I need to do this....
This is what is happening......
dev$ wc -l abc_
236 abc_
dev$ head -236 abc_ > abc_2
dev$ wc -l abc_2
229 abc_2
dev$

have no clue on why this is happening.....

---------- Post updated at 02:55 AM ---------- Previous update was at 02:53 AM ----------

Quote:
Originally Posted by TonyFullerMalv
Looks like a typo to me:
Code:
head -$file_cnt abc > abc_2

should read:

Code:
head -$file_cnt abc_ > abc_2

Although that means all the lines in abc_ will end up on abc_2?

What would be better is to define:
FILE=abc_
at the top of the script and the use ${FILE} from then on.
Sure Tony.. Thanks a lot...
i just post the reason why i need to do this... if you can please take a look on it.. and suggest me ?
# 5  
Old 08-04-2009
It is a trivial bug in some versions the "wc" program.
# 6  
Old 08-04-2009
Here's another detail that may help.

These two commands are equivalent:
head -5 file
head -n 5 file

So if you use -n $file_cnt instead of -$file_cnt the extra space would have no effect.

Previous Thread | Next Thread
Test Your Knowledge in Computers #1006
Difficulty: Medium
In calculus and most other branches of mathematics beyond practical geometry, angles are universally measured in degrees.
True or False?

10 More Discussions You Might Find Interesting

1. Linux

Wierd cursor behavior in Linux

I Have a COBOL application running over iscobol platform in Linux server. we run Red Hat Enterprise Linux WS release 4 (Nahant Update 4) Kernel \r on an \m and the command "uname -a" gave me that: Linux trilinux 2.6.9-42.ELsmp #1 SMP Wed Jul 12 23:27:17 EDT 2006 i686 i686 i386 GNU/Linux ... (1 Reply)
Discussion started by: TheReverend
1 Replies

2. UNIX for Dummies Questions & Answers

Env setup script givin wierd results

I have a script that setsup the environmental variables and then goes on to ftp a file . The scripts which does is as follows The script runs fine when i run it in my home dir and goes on to ftp the file.. But when the same file is run as a batch job in Control-M(a job scheduling software... (1 Reply)
Discussion started by: hareeshkumaru
1 Replies

3. Programming

Wierd C program. Help Needed

Hi, Please see this: When i make a declaration as: char *i, j, *k; and then do sprintf( k, "print.sh %s", i ); the program works fine. But when i change the declaration to: char *i, *k; and then do sprintf( k, "print.sh %s", i ); I get a segmentation fault at the 'sprintf'... (16 Replies)
Discussion started by: karthikb23
16 Replies

4. AIX

Wierd thing about FSs and VGs

Hello It appears that on a regular basis, perhaps when weekly rebooting happens, not sure yet, my odm becomes out of sync. When doing a smitty file system list by volume group, the FS type is displayed as ??? on several FSs. Always in the same VG. I know how to fix this problem, thing is,... (4 Replies)
Discussion started by: mhenryj
4 Replies

5. Shell Programming and Scripting

Wierd results with awk

Hey, I'm trying to use awk for some simple file manipulations but i'm getting soem wierd results. So i want to open up a file which looks like this: @relation 'autoMpg' @attribute a numeric @attribute b numeric @attribute c numeric @data -1.170815,0.257522,0.016416... (2 Replies)
Discussion started by: amatheny
2 Replies

6. UNIX for Dummies Questions & Answers

Wierd networking issue

I have Debian Etch release as a fresh install on a PIII to be a router/firewall. I've configured networking, and utilized Shorewall to set up iptables scripting. I've installed dhcp3, both client and server, to pull an ip from my broadband cable modem, and dish out ip's to a switch for other... (2 Replies)
Discussion started by: pflink
2 Replies

7. UNIX for Dummies Questions & Answers

Wierd boot-up sequence

Hi, I have two machines, one is a E3500 and the other one is a V490; I face a strange issue with both of them. The boot up sequence gives the output of the devalias command. Has anybody faced this?? Please do let me know what needs to be done o resolve this. Regards, NP (1 Reply)
Discussion started by: nitinp82
1 Replies

8. Solaris

wierd sparc 5

Hi! I own a sparc 5 and i seem to have a strange problem. When its off, it starts by itself... Sounds a bit strange? Iknow. Does anyone know whats causing this?? Could it be the network card? or is it someting in ENV or some other configuration?? //dOzY (5 Replies)
Discussion started by: dozy
5 Replies

9. Programming

Wierd pipe problem

I have encountered a strange problem dealing with pipes and forking. The program basicaly does this: cat file | tbl | eqn | groff Now, I have a parent process that forks children that that exec the stuff that they should. The pipes defined in the parent are the ones used. The chain goes... (1 Reply)
Discussion started by: denoir
1 Replies

10. UNIX for Dummies Questions & Answers

Wierd Message????

I am getting this message when I run my script. $ runscript.sh Not connected to any service! Here is the beginning of the script: # 1 - failure # # variable declaration FILEDATE=`date +"%Y%m%d"` Not connected to any service! Right after the FILEDATE gets loaded I get that... (6 Replies)
Discussion started by: lesstjm
6 Replies

Featured Tech Videos