Bash string explode [duplicate]

我是研究僧i 提交于 2019-12-04 05:25:06

问题


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 would be in $3).

This keys on the names of the variables rather than depending on a regex match of the value. It uses spaces or equal signs as field separators.




回答2:


If you have control over the ini file, rewrite it as:

record=123
date="2012.01.20 10:22"

Then in your "bash file" you do

. myfile.ini

echo $record

This is the typical approach. (if you have control over the ini file)




回答3:


You can use:

#!/bin/sh
VALUE="`grep '^record=' myfile.ini |sed 's/[^0-9]//g'`"



回答4:


You can do something like this:

awk '/=[0-9]+$/' file.txt

to print only the line with numeric content after = sign on stdout.

However if you just want to capture 123 into a variable then you can use:

val=$(awk -F"=" '/=[0-9]+$/{print $2}' file.txt)


来源:https://stackoverflow.com/questions/8943132/bash-string-explode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!