altered the script here like this:
Code:
#!/bin/bash 499999500000 real 0m10.552s user 0m10.253s sys 0m0.236s 499999500000 real 0m8.684s user 0m8.413s sys 0m0.234s That's nearly 2 seconds; While the dereferencing saved 2 seconds; The difference got halved; Changing the for loop to the old style reduced the time equally in both: 499999500000 real 0m7.705s user 0m7.433s sys 0m0.176s 499999500000 real 0m5.640s user 0m5.542s sys 0m0.089s The difference stays 2 seconds in an iteration of 1 million simple calculations; (( ))-style calculations are also built-in shell and in my belief generally a much stronger method. Things get really weird when you add a little stuff: Code:
#!/bin/bash |
Quote:
A sub shell is a fork of the main shell, so it inherits the variables, but there is no easy way to copy its variables back to the main shell. It depends on the implementation: Traditional Unix shells make it a sub shell when the block's input/output is redirected or piped. Your work-around then is: Code:
value=0 Ksh and zsh handle the last part of a pipe in the main shell, so even this one works: Code:
value=0 |
Problem easy solved by using kshesll
#!/bin/ksh
# Using kshell so that the variable value is seen outside while loop value=0; while read line do value=`expr $value + 1`; echo $value; done < "myfile" echo $value; |
A few notes on that last post: I think the fipo was written by engraving letters in stone and somehow they've been able to digitize this.
Next thing: use code tags, like shown below. Backticks are soooo 2011... use $() instead That'd make your script look like this (changed a few more things - but essentially the same) Code:
#!/bin/ksh |
the script works well in my pc
e...
are you sure that the script you posted show 0 at the end.. for i copy the code in my pc and it works as expected. |
If you need to get whitespace at the beginning or end of the lines
you can locally modify IFS. Here is a small "wc" clone example: Code:
cat baz Code:
a=0; b=0; c=0; Code:
3 4 23 |
Quote:
Quote:
Code:
#!/bin/bash |
line count for arbitrary file
[QUOTE='Darren[UoW];711668']I am using the following code to read line by line from a file, however my problem is that $value=0 at the end of the loop possibly because bash has created a subshell for the loop or something similar. How can I solve this.
. value=0; while read line do value=`expr $value + 1`; echo $value; done < "myfile" echo $value; ----------------------- try this: wc -l file-in-question | cut --delimiter=" " -f1 - It will return the first token (the line count) only. To automate, one method is to put above wc command in a chmod 777 script named something like CountLns.scr and change file-in-question to $1 and put CountLns.scr thefile in a loop that feeds it files. examples: CountLns.scr BirthdayPartyInvites.txt ==one shot for f in *.txt; do CountLns.scr $f; done ==process current directory for f in `find . -name "*.txt" -print`; CountLns.scr $f; done ==process current branch. ...You can sum the script's output, redirect it to another file, or whatever suits you. . Normally wc -l would return number of lines followed by file name. Using CountLns.scr BirthdayPartyInvites.txt will return just 100 if that is the number of lines in that file. . (removed greater than sign) sh, wc and cut are stock aids in Linux. don't forget to have first line of script start with #!/bin/bash (or sh in place of bash if that is your flavor) Norseman01 |
Quote:
I useually use something like: NLins=`wc $1 | cut - -d" " -f2` --but you can put it in a wrapper to reduce typing. #!/bin/bash # Name: whatever you want to call it # Purpose: Count Number of Lines in a (txt) file. # by: Your_Name or Initials go here # Date: should put at least month/year here # Requireds: if any, they go here # chmod 777 Whatever you named it # LineCount=`wc $1 | cut - -d" " -f2` echo $LineCount # end of file but you can just type the "LineCount...." in wherever you please. That includes from the command line. (In which case substitute the actual filename, complete with path if needed, in place of $1) bash chmod, wc and cut are supplied verbs in Linux. (see also man pages for bash, chmod, wc and cut.) Norseman01 |
Quote:
EDIT: this thread is 11 years old at this point. I don't know why people keep posting. It's confusing. Can a mod lock this thread? |
Quote:
--jeremy |
Bash, read a file line-by-line
#!bin/bash
## #$1 is the file name to read ## for i in `cat $1`; do echo $i done |
Quote:
The bottom line: end value is not right. try this: #!/bin/bash # lnno=0 #echo $lnno #use if line number increases AFTER processing. while read aline do lnno=$(( $lnno + 1 )) # this line before or after processing # processing # can make a difference. so make sure # more processing # all commands are in sync just as the # and more processing # same as with base 0 or base 1 in counting. echo $lnno " " $aline # optional location just here as an example. done <afile.txt echo $lnno # should repeat last line number inside loop # end of file ----- Sample output of use with a text file. Binary files are best not printed direct. Convert binaries to Hex or something before printing. 1 2 3 I was reading through the Complete CSS Guide and came to the Page Layout 4 Properties section. The comments on 'layer' are not definitive. It left 5 me confused. What was the author trying to say? ... 23 Computer Aided Drafting makes extensive use of LAYERS. Foundation, 24 floor, walls, wiring, piping, etc go on their respective LAYERS and for 25 good reasons. 26 27 A single element on a 2D plane is an element, not a layer. ... ... etc etc now one can at least use the line numbers for conversation about a specific line in a text file. Norseman01 |
All times are GMT -5. The time now is 10:41 PM. |