Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert multiline string to array

I have this script:

nmapout=`sudo nmap -sP 10.0.0.0/24` names=`echo "$nmapout" | grep "MAC" | grep -o '(.\+)'` echo "$names" 

now the $names variable contains strings delimited with newlines:

>_  (Netgear)  (Hon Hai Precision Ind. Co.)  (Apple) 

I tried to do the array conversion with the sub-string approach:

names=(${names//\\n/ }) echo "${names[@]}" 

But the problem is that I can't access them by indexing (i.e., ${names[$i] etc.), if I run this loop

for (( i=0; i<${#names[@]}; i++ )) do      echo "$i: ${names[$i]"      # do some processing with ${names[$i]} done 

I get this output:

>_  0: (Netgear)  1: (Hon  2: Hai 

but what I want is:

>_  0: (Netgear)  1: (Hon Hai Precision Ind. Co.)  2: (Apple) 

I could not figure out a good way to do this, please note that the second string has spaces in it.

like image 872
ramgorur Avatar asked Jul 08 '14 09:07

ramgorur


People also ask

How to split multiline string in js?

Multiline string in JavaScript means a string having two or more lines. To split a multiline string to an array we need to use split() in our JavaScript code. split(separator, limit): The split() function is used to split the data depending upon the attributes we are passing in it.

How to create a multiline string in PowerShell?

A multiline string is a string whose value exceeds beyond one line. In that, the string should be enclosed within a here-string(@””@). Newline or carriage return value can also be used to create a multiline string. Multiline string can also be defined by using a line break within double-quotes.

What is multiline string example?

It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end. """Learn Python Programming""" Anything inside the enclosing Triple quotes will become part of one multiline string.


2 Answers

Set IFS (Internal Field Separator). Shell uses the IFS variable to determine what the field separators are. By default, IFS is set to the space character. Change it to the newline character, as demonstrated below:

#!/bin/bash names="Netgear Hon Hai Precision Ind. Co. Apple"      SAVEIFS=$IFS   # Save current IFS (Internal Field Separator) IFS=$'\n'      # Change IFS to newline char names=($names) # split the `names` string into an array by the same name IFS=$SAVEIFS   # Restore original IFS  for (( i=0; i<${#names[@]}; i++ )) do     echo "$i: ${names[$i]}" done 

Output

0: Netgear 1: Hon Hai Precision Ind. Co. 2: Apple 
like image 144
Sanket Parmar Avatar answered Oct 02 '22 17:10

Sanket Parmar


Bash also has a readarray builtin command, easily searchable in the man page. It uses newline (\n) as the default delimiter, and MAPFILE as the default array, so one can do just like so:

    names="Netgear     Hon Hai Precision Ind. Co.     Apple"      readarray -t <<<$names      printf "0: ${MAPFILE[0]}\n1: ${MAPFILE[1]}\n2: ${MAPFILE[2]}\n" 

The -t option removes the delimiter ('\n'), so that it can be explicitly added in printf. The output is:

    0: Netgear     1: Hon Hai Precision Ind. Co.     2: Apple 
like image 36
Ale Avatar answered Oct 02 '22 15:10

Ale