Sponsored Content
Top Forums Shell Programming and Scripting Array Length Reports as Having Length when it is Empty? Post 302700497 by Corona688 on Thursday 13th of September 2012 01:10:06 PM
Old 09-13-2012
You're making the same mistakes again. This:
Code:
for X in 9000 lines of junk
do
        echo "single line" | awk '{ extract something from single line }'
done

is almost always wrong since you can do it all with awk '{ extract something } ' file-with-9000-lines-of-junk in one operation instead of 9,000.

If you must save all its output, then:

Code:
OUTPUT="$(ps auxww | grep [p]rocessname)"

# Don't run awk 9,000 times to process 9,000 lines.  Run it ONCE for everything.
PIDS="$(echo "$OUTPUT" | awk '{ print $2 }')"

# Get count in one operation
set -- $PIDS
echo "There are $# PID's"

for X in $PIDS
do
        echo "$X"
done

Note that set -- overwrites your $1 $2 ... commandline variables.

Also note that functions have their own, independent set of $1 $2 ... variables. set -- there does not overwrite the global ones.

Code:
As for arrays, I thought I remember reading that putting a command in "ARRAY=( $(...) )" would automatically split the output on the IFS and
save it into an array..? I'm guessing that's incorrect?

It splits it, yes. But as you've discovered it's possible to get blank elements. And usually there's no point in doing so -- string splitting works everywhere, not just in arrays, so why not cut out the middleman and do it directly?

Last edited by Corona688; 09-13-2012 at 02:16 PM..
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies

2. UNIX for Dummies Questions & Answers

Sed working on lines of small length and not large length

Hi , I have a peculiar case, where my sed command is working on a file which contains lines of small length. sed "s/XYZ:1/XYZ:3/g" abc.txt > xyz.txt when abc.txt contains lines of small length(currently around 80 chars) , this sed command is working fine. when abc.txt contains lines of... (3 Replies)
Discussion started by: thanuman
3 Replies

3. UNIX for Dummies Questions & Answers

Length of a Unix filepath max length

Hi Guys, Could anyone shed some light on the length of a Unix filepath max length pls ? thanks ! Wilson (3 Replies)
Discussion started by: wilsontan
3 Replies

4. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

5. Shell Programming and Scripting

Make variable length record a fixed length

Very, very new to unix scripting and have a unique situation. I have a file of records that contain 3 records types: (H)eader Records (D)etail Records (T)railer Records The Detail records are 82 bytes in length which is perfect. The Header and Trailer records sometimes are 82 bytes in... (3 Replies)
Discussion started by: jclanc8
3 Replies

6. Shell Programming and Scripting

changing a variable length text to a fixed length

Hi, Can anyone help with a effective solution ? I need to change a variable length text field (between 1 - 18 characters) to a fixed length text of 18 characters with the unused portion, at the end, filled with spaces. The text field is actually field 10 of a .csv file however I could cut... (7 Replies)
Discussion started by: dc18
7 Replies

7. Shell Programming and Scripting

Generate 100 Character Fixed Length Empty File

Hello Everyone, I'm running AIX 5.3 and need to generate a 100 character fixed length empty file from within a bash script that I am developing. I searched and was able to find: dd if=/dev/zero of=/test/path/file count=100 however my understanding is that this will generate a file of a... (10 Replies)
Discussion started by: jvt
10 Replies

8. Shell Programming and Scripting

Flat file-make field length equal to header length

Hello Everyone, I am stuck with one issue while working on abstract flat file which i have to use as input and load data to table. Input Data- ------ ------------------------ ---- ----------------- WFI001 Xxxxxx Control Work Item A Number of Records ------ ------------------------... (5 Replies)
Discussion started by: sonali.s.more
5 Replies

9. Shell Programming and Scripting

Convert variable length record to fixed length

Hi Team, I have an issue to split the file which is having special chracter(German Char) using awk command. I have a different length records in a file. I am separating the files based on the length using awk command. The command is working fine if the record is not having any... (7 Replies)
Discussion started by: Anthuvan
7 Replies
Net::Daemon::Test(3)                                    User Contributed Perl Documentation                                   Net::Daemon::Test(3)

NAME
Net::Daemon::Test - support functions for testing Net::Daemon servers SYNOPSIS
# This is the server, stored in the file "servertask". # # Create a subclass of Net::Daemon::Test, which in turn is # a subclass of Net::Daemon use Net::Daemon::Test (); package MyDaemon; @MyDaemon::ISA = qw(Net::Daemon::Test); sub Run { # Overwrite this and other methods, as you like. } my $self = Net::Daemon->new(\%attr, @options); eval { $self->Bind() }; if ($@) { die "Server cannot bind: $!"; } eval { $self->Run() }; if ($@) { die "Unexpected server termination: $@"; } # This is the client, the real test script, note we call the # "servertask" file below: # # Call the Child method to spawn a child. Don't forget to use # the timeout option. use Net::Daemon::Test (); my($handle, $port) = eval { Net::Daemon::Test->Child(5, # Number of subtests 'servertask', '--timeout', '20') }; if ($@) { print "not ok 1 $@ "; exit 0; } print "ok 1 "; # Real tests following here ... # Terminate the server $handle->Terminate(); DESCRIPTION
This module is a frame for creating test scripts of Net::Daemon based server packages, preferrably using Test::Harness, but that's your choice. A test consists of two parts: The client part and the server part. The test is executed by the child part which invokes the server part, by spawning a child process and invoking an external Perl script. (Of course we woultn't need this external file with fork(), but that's the best possibility to make the test scripts portable to Windows without requiring threads in the test script.) The server part is a usual Net::Daemon application, for example a script like dbiproxy. The only difference is that it derives from Net::Daemon::Test and not from Net::Daemon, the main difference is that the Bind method attempts to allocate a port automatically. Once a port is allocated, the number is stored in the file "ndtest.prt". After spawning the server process, the child will wait ten seconds (hopefully sufficient) for the creation of ndtest.prt. AVAILABLE METHODS
Server part Options Adds an option --timeout to Net::Daemon: The server's Run method will die after at most 20 seconds. Bind (Instance method) This is mainly the default Bind method, but it attempts to find and allocate a free port in two ways: First of all, it tries to call Bind with port 0, most systems will automatically choose a port in that case. If that seems to fail, ports 30000-30049 are tried. We hope, one of these will succeed. :-) Run (Instance method) Overwrites the Net::Daemon's method by adding a timeout. sub Run ($) { my $self = shift; $self->Run(); } Client part Child (Class method) Attempts to spawn a server process. The server process is expected to create the file 'ndtest.prt' with the port number. The method returns a process handle and a port number. The process handle offers a method Terminate that may later be used to stop the server process. AUTHOR AND COPYRIGHT
Net::Daemon is Copyright (C) 1998, Jochen Wiedmann Am Eisteich 9 72555 Metzingen Germany Phone: +49 7123 14887 Email: joe@ispsoft.de All rights reserved. You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file. SEE ALSO
Net::Daemon(3), Test::Harness(3) perl v5.12.1 2007-05-23 Net::Daemon::Test(3)
All times are GMT -4. The time now is 04:39 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy