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
Hash::Merge(3)						User Contributed Perl Documentation					    Hash::Merge(3)

NAME
Hash::Merge - Merges arbitrarily deep hashes into a single hash SYNOPSIS
use Hash::Merge qw( merge ); my %a = ( 'foo' => 1, 'bar' => [ qw( a b e ) ], 'querty' => { 'bob' => 'alice' }, ); my %b = ( 'foo' => 2, 'bar' => [ qw(c d) ], 'querty' => { 'ted' => 'margeret' }, ); my %c = %{ merge( \%a, \%b ) }; Hash::Merge::set_behavior( 'RIGHT_PRECEDENT' ); # This is the same as above Hash::Merge::specify_behavior( { 'SCALAR' => { 'SCALAR' => sub { $_[1] }, 'ARRAY' => sub { [ $_[0], @{$_[1]} ] }, 'HASH' => sub { $_[1] }, }, 'ARRAY => { 'SCALAR' => sub { $_[1] }, 'ARRAY' => sub { [ @{$_[0]}, @{$_[1]} ] }, 'HASH' => sub { $_[1] }, }, 'HASH' => { 'SCALAR' => sub { $_[1] }, 'ARRAY' => sub { [ values %{$_[0]}, @{$_[1]} ] }, 'HASH' => sub { Hash::Merge::_merge_hashes( $_[0], $_[1] ) }, }, }, 'My Behavior', ); # Also there is OO interface. my $merge = Hash::Merge->new( 'LEFT_PRECEDENT' ); my %c = %{ $merge->merge( \%a, \%b ) }; # All behavioral changes (e.g. $merge->set_behavior(...)), called on an object remain specific to that object # The legacy "Global Setting" behavior is respected only when new called as a non-OO function. DESCRIPTION
Hash::Merge merges two arbitrarily deep hashes into a single hash. That is, at any level, it will add non-conflicting key-value pairs from one hash to the other, and follows a set of specific rules when there are key value conflicts (as outlined below). The hash is followed recursively, so that deeply nested hashes that are at the same level will be merged when the parent hashes are merged. Please note that self-referencing hashes, or recursive references, are not handled well by this method. Values in hashes are considered to be either ARRAY references, HASH references, or otherwise are treated as SCALARs. By default, the data passed to the merge function will be cloned using the Clone module; however, if necessary, this behavior can be changed to use as many of the original values as possible. (See "set_clone_behavior"). Because there are a number of possible ways that one may want to merge values when keys are conflicting, Hash::Merge provides several preset methods for your convenience, as well as a way to define you own. These are (currently): Left Precedence This is the default behavior. The values buried in the left hash will never be lost; any values that can be added from the right hash will be attempted. my $merge = Hash::Merge->new(); my $merge = Hash::Merge->new('LEFT_PRECEDENT'); $merge->set_set_behavior('LEFT_PRECEDENT') Hash::Merge::set_set_behavior('LEFT_PRECEDENT') Right Precedence Same as Left Precedence, but with the right hash values never being lost my $merge = Hash::Merge->new('RIGHT_PRECEDENT'); $merge->set_set_behavior('RIGHT_PRECEDENT') Hash::Merge::set_set_behavior('RIGHT_PRECEDENT') Storage Precedence If conflicting keys have two different storage mediums, the 'bigger' medium will win; arrays are preferred over scalars, hashes over either. The other medium will try to be fitted in the other, but if this isn't possible, the data is dropped. my $merge = Hash::Merge->new('STORAGE_PRECEDENT'); $merge->set_set_behavior('STORAGE_PRECEDENT') Hash::Merge::set_set_behavior('STORAGE_PRECEDENT') Retainment Precedence No data will be lost; scalars will be joined with arrays, and scalars and arrays will be 'hashified' to fit them into a hash. my $merge = Hash::Merge->new('RETAINMENT_PRECEDENT'); $merge->set_set_behavior('RETAINMENT_PRECEDENT') Hash::Merge::set_set_behavior('RETAINMENT_PRECEDENT') Specific descriptions of how these work are detailed below. merge ( <hashref>, <hashref> ) Merges two hashes given the rules specified. Returns a reference to the new hash. _hashify( <scalar>|<arrayref> ) -- INTERNAL FUNCTION Returns a reference to a hash created from the scalar or array reference, where, for the scalar value, or each item in the array, there is a key and it's value equal to that specific value. Example, if you pass scalar '3', the hash will be { 3 => 3 }. _merge_hashes( <hashref>, <hashref> ) -- INTERNAL FUNCTION Actually does the key-by-key evaluation of two hashes and returns the new merged hash. Note that this recursively calls "merge". set_clone_behavior( <scalar> ) Sets how the data cloning is handled by Hash::Merge. If this is true, then data will be cloned; if false, then original data will be used whenever possible. By default, cloning is on (set to true). get_clone_behavior( ) Returns the current behavior for data cloning. set_behavior( <scalar> ) Specify which built-in behavior for merging that is desired. The scalar must be one of those given below. get_behavior( ) Returns the behavior that is currently in use by Hash::Merge. specify_behavior( <hashref>, [<name>] ) Specify a custom merge behavior for Hash::Merge. This must be a hashref defined with (at least) 3 keys, SCALAR, ARRAY, and HASH; each of those keys must have another hashref with (at least) the same 3 keys defined. Furthermore, the values in those hashes must be coderefs. These will be called with two arguments, the left and right values for the merge. Your coderef should return either a scalar or an array or hash reference as per your planned behavior. If necessary, use the functions _hashify and _merge_hashes as helper functions for these. For example, if you want to add the left SCALAR to the right ARRAY, you can have your behavior specification include: %spec = ( ...SCALAR => { ARRAY => sub { [ $_[0], @$_[1] ] }, ... } } ); Note that you can import _hashify and _merge_hashes into your program's namespace with the 'custom' tag. BUILT-IN BEHAVIORS Here is the specifics on how the current internal behaviors are called, and what each does. Assume that the left value is given as $a, and the right as $b (these are either scalars or appropriate references) LEFT TYPE RIGHT TYPE LEFT_PRECEDENT RIGHT_PRECEDENT SCALAR SCALAR $a $b SCALAR ARRAY $a ( $a, @$b ) SCALAR HASH $a %$b ARRAY SCALAR ( @$a, $b ) $b ARRAY ARRAY ( @$a, @$b ) ( @$a, @$b ) ARRAY HASH ( @$a, values %$b ) %$b HASH SCALAR %$a $b HASH ARRAY %$a ( values %$a, @$b ) HASH HASH merge( %$a, %$b ) merge( %$a, %$b ) LEFT TYPE RIGHT TYPE STORAGE_PRECEDENT RETAINMENT_PRECEDENT SCALAR SCALAR $a ( $a ,$b ) SCALAR ARRAY ( $a, @$b ) ( $a, @$b ) SCALAR HASH %$b merge( hashify( $a ), %$b ) ARRAY SCALAR ( @$a, $b ) ( @$a, $b ) ARRAY ARRAY ( @$a, @$b ) ( @$a, @$b ) ARRAY HASH %$b merge( hashify( @$a ), %$b ) HASH SCALAR %$a merge( %$a, hashify( $b ) ) HASH ARRAY %$a merge( %$a, hashify( @$b ) ) HASH HASH merge( %$a, %$b ) merge( %$a, %$b ) (*) note that merge calls _merge_hashes, hashify calls _hashify. CAVEATS
This will not handle self-referencing/recursion within hashes well. Plans for a future version include incorporate deep recursion protection. As of Feb 16, 2002, ActiveState Perl's PPM of Clone.pm is only at 0.09. This version does not support the cloning of scalars if passed to the function. This is fixed by 0.10 (and currently, Clone.pm is at 0.13). So while most other users can upgrade their Clone.pm appropriately (and I could put this as a requirement into the Makefile.PL), those using ActiveState would lose out on the ability to use this module. (Clone.pm is not pure perl, so it's not simply a matter of moving the newer file into place). Thus, for the time being, a check is done at the start of loading of this module to see if a newer version of clone is around. Then, all cloning calls have been wrapped in the internal _my_clone function to block any scalar clones if Clone.pm is too old. However, this also prevents the cloning of anything that isn't a hash or array under the same conditions. Once ActiveState updates their Clone, I'll remove this wrapper. AUTHOR
Michael K. Neylon <mneylon-pm@masemware.com> COPYRIGHT
Copyright (c) 2001,2002 Michael K. Neylon. All rights reserved. This library is free software. You can redistribute it and/or modify it under the same terms as Perl itself. perl v5.18.2 2013-11-03 Hash::Merge(3)
All times are GMT -4. The time now is 05:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy