explode

Bash string explode [duplicate]

我是研究僧i 提交于 2019-12-04 05:25:06
问题 This question already has answers here : How do I split a string on a delimiter in Bash? (32 answers) Closed 11 months ago . My file looks like this: record=123 date=2012.01.20 10:22 In the bash file I do cat myfile.ini , and then I need to use something like explode, because I need ONLY to grep the 123 numeric record data, and nothing else. How it can be done in the bash ? 回答1: awk -F'=| ' '/record/ {print $2}' Substitute "date" for "record" in the command above to get the date (the time

Split String Into Array and Append Prev Value

久未见 提交于 2019-12-04 00:12:52
I have this string: var/log/file.log I eventually want to end up with an array looking like this: Array => [ '1' => 'var', '2' => 'var/log', '3' => 'var/log/file.log' ] I currently have this: <?php $string = 'var/log/file.log'; $array = explode('/', $string); $output = [ 1 => $array[0], 2 => $array[0]. '/' .$array[1], 3 => $array[0]. '/' .$array[1]. '/' .$array[2] ]; echo '<pre>'. print_r($output, 1) .'</pre>'; This feels really counter-intuitive and I'm not sure if there's already something built into PHP that can take care of this. How do I build an array using appending previous value? This

explode error \r\n and \n in windows and linux server

醉酒当歌 提交于 2019-12-03 21:21:59
问题 I have used explode function to get textarea's contain into array based on line. When I run this code in my localhost (WAMPserver 2.1) It work perfectly with this code : $arr=explode("\r\n",$getdata); When I upload to my linux server I need to change above code everytime into : $arr=explode("\n",$getdata); What will be the permanent solution to me. Which common code will work for me for both server? Thank you 回答1: The constant PHP_EOL contains the platform-dependent linefeed, so you can try

Possible grouping of words

陌路散爱 提交于 2019-12-03 09:50:05
This is not homework: Came across this scenario while working on PHP String Differences and Dynamic Restrictions Given a string of n words how to distribute them into m groups without altering the word sequence? Example 1: String: "My name is SparKot" Groups: 2 (string is split in to two strings) Possible groups will be: ('My', 'name is SparKot'), ('My name', 'is SparKot'), ('My name is', 'SparKot') with the same string Example 2: String: "My name is SparKot" Groups: 3 (string will be split in to three strings) Possible groups will be: ('My', 'name', 'is SparKot'), ('My', 'name is', 'SparKot')

extract part of a string before and after a word

匿名 (未验证) 提交于 2019-12-03 07:50:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: i need to extract and show some words before and after a query word , something like google search results, for example: $str = "hi user! welcome to new php open source world, we are trying to learn you something!" ; $query = "new php" ; $result = "... welcome to new php open source ..." ; i searched google an SO but didn't find a clear answer or maybe my php knowledge was not enough! is there a workable and easy-to-use function to do this job? 回答1: function yourFuncName ( $str , $query , $numOfWordToAdd ) { list ( $before , $after

Explode in PySpark

匿名 (未验证) 提交于 2019-12-03 07:47:04
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I would like to transform from a DataFrame that contains lists of words into a DataFrame with each word in its own row. How do I do explode on a column in a DataFrame? Here is an example with some of my attempts where you can uncomment each code line and get the error listed in the following comment. I use PySpark in Python 2.7 with Spark 1.6.1. from pyspark.sql.functions import split, explode DF = sqlContext.createDataFrame([('cat \n\n elephant rat \n rat cat', )], ['word']) print 'Dataset:' DF.show() print '\n\n Trying to do explode: \n'

explode two-item-list in array as key=>value

人盡茶涼 提交于 2019-12-03 03:13:15
I'd like to explode a multi-line-string like this color:red material:metal to an array like this $array['color']=red $array['material']=metal any idea? Use explode() , you can use a regexp for it, but it's simple enough without the overhead. $data = array(); foreach (explode("\n", $dataString) as $cLine) { list ($cKey, $cValue) = explode(':', $cLine, 2); $data[$cKey] = $cValue; } As mentioned in comments, if data is coming from a Windows/DOS environment it may well have CRLF newlines, adding the following line before the foreach() would resolve that. $dataString = str_replace("\r", "",

DataFrame explode list of JSON objects

匿名 (未验证) 提交于 2019-12-03 02:23:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have JSON data in the following format: { "date": 100 "userId": 1 "data": [ { "timeStamp": 101, "reading": 1 }, { "timeStamp": 102, "reading": 2 } ] } { "date": 200 "userId": 1 "data": [ { "timeStamp": 201, "reading": 3 }, { "timeStamp": 202, "reading": 4 } ] } I read it into Spark SQL: val df = SQLContext.read.json(...) df.printSchema // root // |-- date: double (nullable = true) // |-- userId: long (nullable = true) // |-- data: array (nullable = true) // | |-- element: struct (containsNull = true) // | | |-- timeStamp: double (nullable

how to convert array values from string to int?

匿名 (未验证) 提交于 2019-12-03 02:12:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: $string = "1,2,3" $ids = explode(',', $string); var_dump($ids); returns array(3) { [0]=> string(1) "1" [1]=> string(1) "2" [2]=> string(1) "3" } I need for the values to be of type int instead of type string. Is there a better way of doing this than looping through the array with a foreach and converting each string to int? 回答1: $integerIDs = array_map('intval', explode(',', $string)); 回答2: This is almost 3 times faster than explode() , array_map() and intval() : $integerIDs = json_decode('[' . $string . ']', true); 回答3: So I was curious

How to put string in array, split by new line?

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a string with line breaks in my database. I want to convert that string into an array, and for every new line, jump one index place in the array. If the string is: My text1 My text2 My text3 The result I want is this: Array ( [0] => My text1 [1] => My text2 [2] => My text3 ) 回答1: You can use the explode function, using " \n " as separator : $your_array = explode("\n", $your_string_from_db); For instance, if you have this piece of code : $str = "My text1\nMy text2\nMy text3"; $arr = explode("\n", $str); var_dump($arr); You'd get this