1

I have an array, on the attached screen.

I need to get all third components from this array.

Example (my existing array):

array1 = 
 ["SRV WW ZSTG HSM BlackDuck RW", "SRV WW ZSDB M204 BlackDuck RW", etc] 

The result should be:

array2 = ["HSM", "M204"]

enter image description here

  • I need to delete all empty elements from array

My code to work with it right now:

FILE="$1"

index=0
while read name; do
    get_group_names_from_file[$index]="$name"
    index=$(($index+1))
done < "${FILE}"

get_group_names_from_file=("${get_group_names_from_file[@]:3}")

for ((a=0; a < ${#get_group_names_from_file[*]}; a++))
do
    echo "${get_group_names_from_file[$a]}"
done
tripleee
  • 175,061
  • 34
  • 275
  • 318
Maksim
  • 27
  • 3
  • Arrays in `bash` don't take `,` to store its elements. What is your use-case here? How are you populating this array? – Inian Jan 31 '19 at 11:39
  • @Inian I updated ticket – Maksim Jan 31 '19 at 11:42
  • What is your other requirement to delete empty elements from array? What does it mean? – Inian Jan 31 '19 at 11:44
  • @Inian on the screen, you can see last 2 elements is empty, there is no elements there, I need to delete these from my array. – Maksim Jan 31 '19 at 11:46
  • It would be good if you can paste that content to the question above and explain it to your other useful information and show us the actual complete content of the array – Inian Jan 31 '19 at 11:47
  • @Inian ok, just help me with the first part, to get another array. – Maksim Jan 31 '19 at 11:49
  • It is potentially a duplicate of https://stackoverflow.com/q/1469849/5291015. If you add more details, then it could make a question unique – Inian Jan 31 '19 at 11:52
  • I don't understand your needs. Is it just to get the 3rd column of the file? If so, simply `cat file | cut -d ' ' -f 3` should be enough – brunorey Jan 31 '19 at 12:24
  • @brunorey, yeap, I need third column, but where should I ad it in my code? – Maksim Jan 31 '19 at 12:26
  • I've posted an answer. – brunorey Jan 31 '19 at 12:28
  • 1
    `HSM` and `M204` are the 4th word, not the 3rd. Also, what do you really want to do? If you just want to print the 4th word of each line from a file then you don't need an array at all. – Socowi Jan 31 '19 at 12:46
  • Don't use images. Posting an image of your source data is worse than [posting images of code](http://idownvotedbecau.se/imageofcode). I can't copy/paste that, and I'm not going to spend a lot of type typing it in so I can run tests to get you a solution.... Please replace the image with a 4-space indented block of the actual data sample. – Paul Hodges Jan 31 '19 at 14:23

3 Answers3

0

If you only need the third column you can use the cut command (see man page):

FILE="$1"

for value in `cat $FILE | cut -d ' ' -f 3`
do
    echo "3rd column is $value"
done
brunorey
  • 2,135
  • 1
  • 18
  • 26
0

I've set ARRAY1 like this (there's no need for a , to seperate values in an array):

ARRAY1=("SRV WW ZSTG HSM BlackDuck RW" "SRV WW ZSDB M204 BlackDuck RW")

and extracted the third component of the array-values this way:

for (( i=0 ; i<$(echo ${#ARRAY1[*]}) ; i++ )) ; do ARRAY2+=($(echo ${ARRAY1[$i]} | cut -d ' ' -f3)); done

(If you want to have the 4th component, use cut -d ' ' -f4 instead)

Checking values of ARRAY2 via printf '%s\n' "${ARRAY2[@]}"


~$ ARRAY1=("SRV WW ZSTG HSM BlackDuck RW" "SRV WW ZSDB M204 BlackDuck RW")
~$ ARRAY2=() ; for (( i=0 ; i<$(echo ${#ARRAY1[*]}) ; i++ )) ; do ARRAY2+=($(echo ${ARRAY1[$i]} | cut -d ' ' -f3)); done
~$ printf '%s\n' "${ARRAY2[@]}"
~$ ZSTG
~$ ZSDB
Anyesto
  • 11
  • 4
  • I saw, that my request return string "SRV WW ZSTG HSM BlackDuck RW SRV WW ZSDB M204 BlackDuck RW", how can I transform it to array from the word SRV? – Maksim Jan 31 '19 at 14:29
  • Seems like your array values mentioned initialy aren't seperated correctly. Make sure your values for `ARRAY1` are seperated like this: `ARRAY=("Word1.1 Word1.2 Word1.3" "Word2.1 Word2.2 Word2.3")`. In your case specificly `ARRAY1=("SRV WW ZSTG HSM BlackDuck RW" "SRV WW ZSDB M204 BlackDuck RW")` – Anyesto Jan 31 '19 at 14:37
  • you are right, but it doesn't separate. they looks like ARRAY=("ValueA1 ValueA2 ValueA3 ValueA1 ValueB2 ValueB3") . All my element starts from word SRV. How can I separate them in array? Result: ARRAY=("ValueA1 ValueA2 ValueA3" "ValueA1 ValueB2 ValueB3") – Maksim Jan 31 '19 at 14:40
  • You've written in your first post, that you already have an array. `ARRAY=("ValueA1 ValueA2 ValueA3 ValueA1 ValueB2 ValueB3")` may be an array, but contains only **one** value so far --> `ValueA1 ValueA2 ValueA3 ValueA1 ValueB2 ValueB3` Maybe you should clarify your initial situation. I'm sorry, but I don't get the purpose of your inquiry. – Anyesto Jan 31 '19 at 14:50
0

This feels like an XY problem to me.
You talk about arrays, but you read a file. I'll put the data into arrays, in case that's what you needed...

If you need arrays, read it as an array.

$: cat xy.dat # used the two you had in your array that I could copy/paste
SRV WW ZSTG HSM BlackDuck RW
SRV WW ZSDB M204 BlackDuck RW

$: cat xy
#! /bin/env bash

declare -a array1=() array2=()
while read -ra row
do array1+=( "${row[*]}" ) # NOTE: using * makes one string of the row; @ would separate them
   [[ -n "${row[3]}" ]] && array2+=( "${row[3]}" )
done < "$1" # don't use capital FILE.

declare -p array1 array2 # show the contents

$: xy xy.dat
declare -a array1=([0]="SRV WW ZSTG HSM BlackDuck RW" [1]="SRV WW ZSDB M204 BlackDuck RW")
declare -a array2=([0]="HSM" [1]="M204")

If you don't particularly need arrays, or only need the second one, then simplify.

$: cat xy
#! /bin/env bash
while read -r one two three four five six
do [[ -n "$four" ]] && array2+=( "$four" )
done < "$1"
declare -p array2

$: xy xy.dat
declare -a array2=([0]="HSM" [1]="M204")

If you don't particularly need array2 either, just do whatever parsing you needed as you read through.

Good luck.

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
  • Thanks so much, Could you please help me with this: My array right nowt doesn't separate. it looks like ARRAY=("SRV WW ZSTG HSM BlackDuck RW SRV WW ZSDB M204 BlackDuck RW") . All new element starts from "SRV" word. How can I separate them in array? Result: ARRAY=("SRV WW ZSTG HSM BlackDuck RW" "SRV WW ZSDB M204 BlackDuck RW") – Maksim Jan 31 '19 at 14:45
  • look at my first example. Or, `while read -r line; do array1+=("$line"); done` Certainly read https://mywiki.wooledge.org/BashFAQ/001 for read examples and advice, and https://mywiki.wooledge.org/BashGuide/Arrays for arrays examples and advice. – Paul Hodges Jan 31 '19 at 15:13