You are not logged in.

#1 2011-09-25 01:38:16

gromlok
Member
Registered: 2010-04-19
Posts: 154

null

null

Last edited by gromlok (2019-03-10 15:45:23)


.

Offline

#2 2011-09-25 01:58:36

igndenok
Member
From: Sidoarjo, Indonesia
Registered: 2010-06-07
Posts: 160

Re: null

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

#3 2011-09-25 02:56:47

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: null

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

#4 2011-09-25 03:51:57

gromlok
Member
Registered: 2010-04-19
Posts: 154

Re: null

null

Last edited by gromlok (2019-03-10 15:45:38)


.

Offline

#5 2011-09-25 04:01:18

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: null

gromlok wrote:

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.

gromlok wrote:

II) Is it OK to use inputfile=$1 and outputfile=$2 that way?

Sure. Different variable name, same value.

gromlok wrote:

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.

gromlok wrote:

IV) Puting "exit" at the end is redundant?

Entirely redundant if you're not adding an argument.

Offline

#6 2011-09-25 04:59:44

Stebalien
Member
Registered: 2010-04-27
Posts: 1,239
Website

Re: null

You can also use:

let count++

Steven [ web : git ]
GPG:  327B 20CE 21EA 68CF A7748675 7C92 3221 5899 410C

Offline

#7 2011-09-25 16:39:15

gromlok
Member
Registered: 2010-04-19
Posts: 154

Re: null

null

Last edited by gromlok (2019-03-10 15:45:49)


.

Offline

#8 2011-09-29 20:38:20

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: null

falconindy wrote:

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. lol


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

Board footer

Powered by FluxBB