john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

File to array line to array get property substring by equals

!/bin/bash

declare -a myarray # http://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html , bash 4+ readarray myarray < file_pathname # includes the newlines readarray -t myarray < file_pathname # do not include newlines in the array items echo ${#myarray[@]} # should be the same number as wc -l file_pathname echo ${myarray[@]} # all array contents

posix shell /bin/sh doesn't really support arrays

almost!

i=0;

while read t; do

#TOKENS[$i]=$t #i=expr $i + 1 #echo $t

done < apitokens.txt

echo $i

LAST='' SECOND='' while read t; do SECOND=$LAST LAST=$t done < apitokens.txt echo "$SECOND , $LAST"

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

!/bin/bash

2012-06-08 johnpfeiffer

read file into array and get values based on identifiers (split by = )

split by =

grab the first token on the left

is it the IDENTIFIER

if so, then the token on the right is the VALUE

function lineContains {

echo DEBUG "$#" parameters passed

if [ "$#" -ne 2 ]; then echo "line parameter missing" exit 0 else STRING=$1 KEYWORD=$2

OLDIFS=$IFS
IFS='='

IFS='=' read -a tokenArray <<< "$1"
temp=${tokenArray[0]}
if [ "$KEYWORD" == "$temp" ];
then
  echo "${tokenArray[1]}"
fi

ALTERNATE UNSUCCESSFUL METHOD

for word in $STRING

do

if [ "$word" == "$KEYWORD" ];

then

echo "FOUND $word equals $KEYWORD"

fi

done

IFS=$OLDIFS

fi return $VALUE }

MAIN

IDOID="storagegateway.storageGatewayOid" IDKEY="storagegateway.storageGatewaySignedHash" IDTYPE="storagegateway.storageGatewayType" IDDOWNLOAD="storagegateway.downloadWeblinkPrefixURL" IDUPLOAD="storagegateway.downloadWeblinkPrefixURL"

CONVERT FILE INTO ARRAY

i=0; file=$1 while IFS= read -r LINE do ARRAY[$i]=$LINE ((i++)) done <"$file"

echo "processed $i lines into ${#ARRAY[@]} lines in the array"

SEE IF ARRAY CONTAINS THE VALUES REQUIRED

for (( k = 0 ; k < ${#ARRAY[@]} ; k++ )) do

RESULT=$(lineContains "${ARRAY[$k]}" "$IDOID") if [ "$RESULT" != "" ]; then echo "$RESULT" fi

RESULT=$(lineContains "${ARRAY[$k]}" "$IDUPLOAD") if [ "$RESULT" != "" ]; then echo "$RESULT" fi

done

exit 0


  • « heroku continued custom domain new relic monitor ping keepalive
  • Tomcat7 install ssl static content ROOT libtcnative source »

Published

Mar 11, 2014

Category

linux

~246 words

Tags

  • array 16
  • by 7
  • equals 4
  • file 92
  • get 22
  • line 31
  • property 1
  • scripts 63
  • substring 4
  • to 63