0

I wrote the following code to read a file and write it in a list using bash script:

index=0
while read line
do
  array[$index]="$line"
  index=$(($index+1))
done < ../../file.xml

However, I only need to write lines into the array if they contain the word "icon". An array element should look like this:

<icon height="36" width="36" density="ldpi" src="res/icon/android/ldpi.png"/>

Could anyone help me to fix this problem?

5
  • arr=($(grep -w 'icon' ../../file.xml))? Commented Oct 3, 2019 at 7:41
  • @Wiktor that will read one token per array element, not one line. Commented Oct 3, 2019 at 7:47
  • Thank you for your comment. In the file have different information, where I need to select lines containing word 'icon'. An element in an array should look like this:<icon height="36" width="36" density="ldpi" src="res/icon/android/ldpi.png"/> Commented Oct 3, 2019 at 7:49
  • @tripleee OK, readarray arr < <(grep -w 'icon' ../../file.xml) Commented Oct 3, 2019 at 8:00
  • I'm using a later version of bash so I cannot use readarray Commented Oct 3, 2019 at 8:24

2 Answers 2

1

Trivially, with a condition.

case $line in *icon*) ... do stuff;;

You should probably fix the syntax to use read -r and the index variable is really unnecessary.

array=()
while read -r line
do
  case $line in *icon*) array+=("$line");; esac
done < ../../file.xml

More sensibly, do it all in one fell swoop, in Bash 4+

readarray index < <(grep 'icon'  ../../file.xml)

Probably most sensibly, if the file really is XML, use an XML parser like xmlstarlet to properly identify and extract the structure you want to examine.

readarray index < <(xmlstarlet sel -t -m //icon -c . -n ../../file.xml)
Sign up to request clarification or add additional context in comments.

1 Comment

the vital part here is really to use a proper XML parser for parsing XML, rather than attempting your own.
0

You can use regex:

regex="icon"
index=0
while read line
do
  if [[ $line =~ $regex ]]; then
    array[$index]="$line"
    #If you need cut address uncommend next line and comment before line
    #array[$index]="$(sed 's/.*src=\"\(.*\)\".*/\1/' <<< $line)"
    index=$(($index+1))
  fi
done < ../../file.xml

5 Comments

What's with the crazy sed script in the assignment? That might be useful for something but wasn't in the requirements.
transfer "<icon height="36" width="36" density="ldpi" src="res/icon/android/ldpi.png"/>" to "res/icon/android/ldpi.png"
If not need cut address of image - write only array[$index]=$line
Šerg, thank you for your help. I got the following output: 0: <icon 1: height="36" 2: width="36" 3: density="ldpi" 4: src="res/icon/android/ldpi.png"/> 5: <icon 6: height="48" 7: width="48" .... However, an array element should look like this: 0: <icon height="36" width="36" density="ldpi" src="res/icon/android/ldpi.png"/> (Not separately)
Gera, write simple exapmle of file.xml please

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.