You are not logged in.
Pages: 1
null
Last edited by gromlok (2019-03-10 15:45:23)
.
Offline
I) Check this [ vs. [[
II) IIRC, you need something like this in your input file
string1\ string2\ string3
Instead this, because $line only hold 1st column
string1 string2 string3
Even after using '\ ' use "$line" instead $line.
PS: I didn't check the actual error, but this is from my personal experience when using `while read ...' (assuming there's space between strings in your string list)
III) Maybe using something like this
counter=$(expr $counter + 1)
Last edited by igndenok (2011-09-25 02:00:51)
Ask, and it shall be given you.
Seek, and ye shall find.
Knock, and it shall be opened unto you.
Offline
This is a rather suitable job for awk....
awk 'length($0) > 100 && $0 ~ /^5698/ { cnt++; print } END { printf "Number of Register: %d\n", cnt > "/dev/stderr" }' inputfile > outputfile
Other things to note about your shell implementation:
- You cannot invoke a bash script with 'sh' and expect it to be run as a bash script. Even when /bin/sh links to /bin/bash, the behavior of the shell is different (more POSIX like).
- Your redirection ($line > output.txt) doesn't work because you're trying to execute a command, not write to a file. use echo or printf, and please quote properly.
If you wanted this done in shell, its a little more verbose than the awk...
#!/bin/bash
exec 3>outputfile
while read -r line; do
(( ${#line} > 100 )) && [[ ${line:0:4} = 5968 ]] || continue
(( ++count ))
printf '%s\n' "$line" >>&3
done < inputfile
printf 'Number of registers: %s\n' "$count"
I'll just leave this here. It's far more suitable for beginners than TLDP, mostly because its factually correct and gives several examples of what not to do, which is just as important when writing shell.
Last edited by falconindy (2011-09-25 03:53:58)
Offline
null
Last edited by gromlok (2019-03-10 15:45:38)
.
Offline
I) In your code, why use:
done < inputfile
. I don't get it. With the "while" is not enough to read all the file.
Not really sure I understand this... 'read' in a while loop will read from stdin until it reaches EOF, or encounters a read error. The while loop as I wrote it will read the input file in its entirety as the file is replacing standard input for the duration of the loop.
II) Is it OK to use inputfile=$1 and outputfile=$2 that way?
Sure. Different variable name, same value.
III) I did not find why (( ++count )) works in shell! Using
let count = count + 1
works, but that way you did it is more elegant.
If (( ++count )) doesn't work, then you're still executing the script with /bin/sh.
IV) Puting "exit" at the end is redundant?
Entirely redundant if you're not adding an argument.
Offline
null
Last edited by gromlok (2019-03-10 15:45:49)
.
Offline
I'll just leave this here...
Absolutely one of the best resources on bash I've ever come across. Of course, after finding it I was forced to do a major overhaul on all of my scripts.
But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner
Offline
Pages: 1