Sponsored Content
Operating Systems OS X (Apple) Create a bash array from a flat file of whitespaces only. Post 303012538 by wisecracker on Monday 5th of February 2018 03:59:52 PM
Old 02-05-2018
Quote:
Originally Posted by Don Cragun
I assume that you're also aware that if you want an array containing 16 elements each of which has the value <space> you cant just use:
Code:
#!/bin/bash
ARRAY=(" " " " " " " " " " " " " " " " " " " " " " " " " " " " " " " ")
echo "Number of elements in ARRAY: ${#ARRAY[@]}"
for ((i=0; i<${#ARRAY[@]}; i++))
do      printf "'%s'%d\n" "${ARRAY[$i]}" "$i"
done

which produces the output:
Code:
Number of elements in ARRAY: 16
' '0
' '1
' '2
' '3
' '4
' '5
' '6
' '7
' '8
' '9
' '10
' '11
' '12
' '13
' '14
' '15

and also produces exactly the same results if run by a 1993 or later version of ksh instead of bash.
Yes, that was why I used my original 'hexdump' method as that placed just a single <space> character into each cell.
However RudiC's solution is the one I will use.
The way he creates the array indexing is cool. No amount of Googling gave me that kind of result.
Even my method didn't show up in Google and that is pretty much bullet proof, plus, I knew it would work, but 1920 calls to it would be well OTT.
There is a pointer now though for Google... ;o)
I won't mark as solved yet as others might have bash or maybe even dash solutions too.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

create array holding characters from sring then echo array.

Hi, I wish to store $string1 in $string1array a character in each array element. Then i wish to echo the entire array to the screen so that it reads as the normal string again. I have been trying with the code below but does not work. Please help... To put string into array: ... (5 Replies)
Discussion started by: rorey_breaker
5 Replies

2. Shell Programming and Scripting

csv file to array, match field1, replace in flat file field1 to field2

Hello, i am bit stuck with making script for automatic procedure. Case: Two files. One is flat file, other is csv file. csv file has two column/fields with comma delimited data. Here is what i need (explained way) CSV file: field1 | field2 "hello","byebye" "hello2","byebye2"... (23 Replies)
Discussion started by: frankie_konin
23 Replies

3. Shell Programming and Scripting

How do I create an array from a file using every 3rd line

A file contains the following information shown below. Every ceName has 2 consecutive lines that have to be evaluated, using awk, sed, cut (any common unix tools). Input file: ceName: Node-1 processName: tzMgmt Status: PROCESS_NOT_RUNNING ceName: Node-2 processName: tzMgmt Status:... (15 Replies)
Discussion started by: BRH
15 Replies

4. Infrastructure Monitoring

Possible performance improvement (Bash and flat file)

Hello, I am pretty new to shell scripts and I recently wrote one that seems to do what it should but I am exploring the possibility of improving its performance and would appreciate some help. Here is what it does - Its meant to monitor a bunch of systems (reads in IPs one at a time from a flat... (9 Replies)
Discussion started by: prafulnama
9 Replies

5. Solaris

Configure disk array in RAID5 and create file system

I'm new to forums, it's my first time posting. I have a sun v490 server. I just installed solaris 10.6, on the local drives. I'm being asked to do the following: For Oracle install I need “oracle” user that belong to “dba” and “oinstall” groups. File system /u01/app/oracle, 10GB (if... (6 Replies)
Discussion started by: Kjons76
6 Replies

6. Shell Programming and Scripting

how to create flat file delimited by "\002"

I need to create a flat file with columns delimited by "\002" (octal 2) I tried using the simple echo. name="Adam Smith" age=40 address="1 main st" city="New York" echo ${name}"\002"${age}"\002"${address}"\002"${city} > mytmp but it creates a delimiter with different octal... (4 Replies)
Discussion started by: injey
4 Replies

7. Shell Programming and Scripting

perl: Read array from a flat file

Hello Guru's I want to read an array into a flatfile Please let me know how to do the same So far this the below code use strict; use warnings; open (my $data , '<', $ARGV)|| die "could not open $ARGV:\n$!"; my @array=(<$data>); my @sorted=sort... (8 Replies)
Discussion started by: Pratik4891
8 Replies

8. UNIX for Dummies Questions & Answers

Read a flat file, evaluate and create output. UNIX SCRIPT.

Hi all, I have a flat file as below; 470423495|1||TSA-A000073800||1|||1 471423495|1||TSA-A000073800||5|||5 472423495|1||TSA-A000073800||2|||7 473423495|1||TSA-A000073800||3|||3 I like to create a Unix script. The script have to valuate the last two columns, if the values are... (4 Replies)
Discussion started by: mrreds
4 Replies

9. Shell Programming and Scripting

Create a flat file and directory structure

Hi All, is there any work around to generate the file and directory structure like below at left side at Output? and exclude all file except .abc .txt Current Directory structure |-------------files |---------------Share |-----------------dir1 |-----------------dir2... (11 Replies)
Discussion started by: heros
11 Replies

10. Shell Programming and Scripting

Construct Array from String seperated by different whitespaces

My string variable which gets the output from the result of a database query has values as below: line="2019-09-11 15:17:55 CR1234 anonymous Deployed DR_only Back_APP" I wish to construct an array (my_array) which should have entries as below. Note: 1. The first... (6 Replies)
Discussion started by: mohtashims
6 Replies
MultiType(3pm)						User Contributed Perl Documentation					    MultiType(3pm)

NAME
Object::MultiType - Perl Objects as Hash, Array, Scalar, Code and Glob at the same time. SYNOPSIS
use Object::MultiType ; my $scalar = 'abc' ; my @array = qw(x y z); my %hash = (A => 1 , B => 2) ; my $obj = Object::MultiType->new( scalar => $scalar , array => @array , hash => \%hash , code => sub{ return("I'm a sub ref!") ; } glob => *STDOUT , ) ; print "Me as scalar: $obj " ; my $array_1 = $obj->[1] ; print "$array_1 " ; my $hash_B = $obj->{B} ; print "$hash_B " ; my $hash = $$obj->hash ; foreach my $Key (sort keys %$hash ) { print "$Key = $$hash{$Key} " ; } &$obj(args) ; DESCRIPTION
This module return an object that works like a Hash, Array, Scalar, Code and Glob object at the same time. The usual way is to call it from your module at new(): package FOO ; use Object::MultiType ; use vars qw(@ISA) ; @ISA = qw(Object::MultiType) ; ## Is good to 'Object::MultiType' be the last in @ISA! sub new { my $class = shift ; my $this = Object::MultiType->new() ; bless($this,$class) ; } METHODS
** See the methods of the Saver too. new Arguments: bool The boolean reference. Default: undef boolcode|boolsub Set the sub/function (CODE reference) that will return/generate the boolean value. scalar The SCALAR reference. If not sent a null SCALAR will be created. scalarcode|scalarsub Set the sub/function (CODE reference) that will return/generate the scalar data of the object. array The ARRAY reference. If not sent a null ARRAY will be created. hash The HASH reference. If not sent a null HASH will be created. code The CODE reference. If not sent a null sub{} will be created. With this your object can be used as a sub reference: my $multi = Object::MultiType->new( code => sub { print "Args: @_ " ;} ) ; &$multi(); Note that the first argument sent to the SUB is the object ($multi). glob The GLOB (HANDLE) reference. If not sent a null GLOB will be created. ** Note that you can't use the default (null) GLOB created when you don't paste this argument! Since all the objects will share it, and was there just to avoid erros! tiearray Package name to create a TIEARRAY. The argument $$this is sent to tie(). tie() is called as: tie(@array,$args{tiearray},$$this) ; Note that is hard to implement the tie methods for PUSH, POP, SHIFT, UNSHIFT, SPLICE... Unless you make just an alias to another array through the tie methods. ** See tiehash too. tiehash Package name to create a TIEHASH. The argument $$this is sent to tie(). tie() is called as: tie(%hash,$args{tiehash},$$this) ; ** $$this (the Saver) is sent, and not $this, to avoid the break of DESTROY (auto reference). ** $$this is a reference to the Saver object that save the SCALAR, ARRAY, HASH, CODE and GLOB. sub TIEHASH { my $class = shift ; my $multi = shift ; ## $$this my $scalarref = $multi->scalar ; ## ${*$multi} my $arrayref = $multi->array ; ## @{*$multi} my $hashref = $multi->hash ; ## \%{*$multi} my $this = { s => $scalarref , a => $arrayref , h => $hashref } ; bless($this,$class) ; } tiehandle Make the object works like a tied glob (TIEHANDLE). If used with glob will tie() it. If glob is not sent a NULL GLOB is used: my $multi = Object::MultiType->new( glob => *MYOUT , ## 'glob' is Optional. tiehandle => 'TieHandlePack' , ) ; tieonuse The reference is only tied when it's used! So, the HASH, ARRAY or GLOB (handle) are only tied if/when they are accessed. nodefault If set to true tell to not create the default references inside the Saver, and it will have only the references paste (scalar, array, hash, code, glob). ** This is good to save memory. is_saver Return 0. Good to see if what you have is the Saver or the MultiType object. SAVER
The MultiType object has a Saver object (Object::MultiType::Saver), that save all the different data type (references). This saver can be accessed from the main object: my $multi = Object::MultiType->new() ; my $saver = $$multi ; print $saver->scalar ; If you want to save attributes in your Object and you use tiehash, you can't set attributes directly in the MultiType object!: sub new { my $class = shift ; my $this = Object::MultiType->new(tiehash => 'TieHashPack') ; ## Dont do that! This will call the STORE() at TIEHASH, and not save it in the object: $this->{flagx} = 1 ; bless($this,$class) ; } So, if you use tiehash and want to save attributes (outside tie) use that: ## This save the attribute inside the Saver: $$this->{flagx} = 1 ; Note that this set an attribute in the saver, and it has their own attributes! ## $saver = $$this ; $saver->{s} ## the sacalar ref. $saver->{a} ## the array ref. $saver->{h} ## the hash ref. $saver->{c} ## the code ref. $saver->{g} ## the glob ref. ** See "Direct access to the data types". DESTROY
When the object is DESTROIED, the Saver inside it is cleanned, so the tied objects can be DESTROIED automatically too. Direct access to the data types To access directly the reference of the different data types (SCALAR, ARRAY, HASH, CODE & GLOB) use: my $multi = Object::MultiType->new() ; my $saver = $$multi ; my $scalarref = $saver->scalar ; ## $saver->{s} my $arrayref = $saver->array ; ## $saver->{a} my $hashref = $saver->hash ; ## $saver->{h} my $coderef = $saver->code ; ## $saver->{c} my $globeref = $saver->glob ; ## $saver->{g} ## You can access the Saver directly from the main object: $$multi->hash ; Setting the data: $saver->set_bool( 1 ) ; $saver->set_scalar( 'xyz' ) ; $saver->set_array( [qw(x y z)] ) ; $saver->set_hash( {X => 1} ) ; $saver->set_code( sub{ print "XYZ " ; } ) ; $saver->set_glob( *STDOUT ) ; As SCALAR You can use it as SCALAR when you put it inside quotes or make a copy of it: my $multi = Object::MultiType->new( scalar => 'Foo' ) ; ## Quote: print "Me as scalar: $multi " ; ## Copy: my $str = $multi ; $str .= '_x' ; ## Copy made when you change it! Until that $str works like $multi. print "$str " ; using the argument scalarsub you can use a function that will generate the scalar data, in the place of a reference to a SCALAR: my $multi = Object::MultiType->new(scalarsub => sub{ return 'generated data' ;} ) ; print "My scalar have $multi! " ; As ARRAY You can use it as ARRAY directly from the object: my $multi = Object::MultiType->new( array => [qw(FOO BAR)] ) ; my $array_0 = $multi->[0] ; $multi->[1] = 'foo' ; As HASH You can use it as HASH directly from the object: my $multi = Object::MultiType->new( hash => {key => 'foo'} ) ; my $k = $multi->{key} ; $multi->{foo} = 'bar' ; With TIE To use your ARRAY and HASH part tied, you can paste the reference already tied of the HASH or ARRAY, or use the arguments tiehash and tiearray at new(): ## Using the reference: my %hash ; tie(%hash,'TieHash') ; my $multi = Object::MultiType->new(hash => \%hash) ; ## Or using directly the argument: my $multi = Object::MultiType->new(tiehash => 'TieHashPack') ; Note that using tiehash or tiearray is better, since your tied HASH or ARRAY can see the object Saver and the other data type of it. See the method new() and their arguments. Here's an example of a TieHash package that is called from Object::MultiType->new(): ## The call inside Object::MultiType->new(): tie(%hash,$args{tiehash},$$this) ; ## The package: package TieHash ; sub TIEHASH { my $class = shift ; my $Saver = shift ; ## Object::MultiType paste as $$this (only the Saver) to avoid break of DESTROY! ## $this = Object::MultiType >> $$this = Object::MultiType::Saver my $scalarref = $Saver->scalar ; my $arrayref = $Saver->array ; ## Note that $Saver->hash will return the tied hash, and is not needed here! ## my $hashref = $Saver->hash ; ## Saving the references inside the TIE object: my $this = { scalar => $scalarref , array => $arrayref , hash => {} } ; bless($this,$class) ; } sub FETCH { my $this = shift ; return( 'key' ) ;} sub NEXTKEY { my $this = shift ; return( 'key' ) ;} sub STORE { my $this = shift ; $this->{hash}{$_[0]} = $_[1] } sub DELETE { my $this = shift ; delete $this->{hash}{$_[0]} } sub CLEAR { my $this = shift ; $this->{hash} = {} ;} sub EXISTS { my $this = shift ; defined $this->{hash}{$_[0]} ;} sub FIRSTKEY { my $this = shift ; (sort keys %{$this->{hash}} )[0] } sub DESTROY {} Using tiehash, you need to save the attributes in the Saver, or you call the tie(). $$this->{flagx} = 1 ; Object::MultiType::Saver This is a litte package where the Saver objects are created. It will save the data types (SCALAR, ARRAY, HASH, CODE & GLOB) of the main objects (Object::MultiType). METHODS: is_saver Return 1. Good to see if what you have is the Saver or the MultiType object. bool Return the BOOL reference inside the Saver. scalar Return the SCALAR reference inside the Saver. array Return the ARRAY reference inside the Saver. hash Return the HASH reference inside the Saver. code Return the CODE/sub reference inside the Saver. glob Return the GLOB/HANDLE reference inside the Saver. set_bool Set the boolean reference inside the Saver. set_scalar Set the SCALAR reference inside the Saver. set_array Set the ARRAY reference inside the Saver. set_hash Set the HASH reference inside the Saver. set_code Set the CODE/sub reference inside the Saver. set_glob Set the GLOB/HANDLE reference inside the Saver. clean Clean all the references saved in the Saver. SEE ALSO
overload, perltie, Scalar::Util. This module/class was created for XML::Smart. AUTHOR
Graciliano M. P. <gm@virtuasites.com.br> I will appreciate any type of feedback (include your opinions and/or suggestions). ;-P COPYRIGHT
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.10.0 2004-12-21 MultiType(3pm)
All times are GMT -4. The time now is 02:23 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy