[英]Find multiple strings between values and replace with newline in bash
I need to write a bash script to list values from an sql database. 我需要编写一个bash脚本以列出sql数据库中的值。
I've got so far but now I need to get the rest of the way. 到目前为止,我已经走了,但现在我需要继续做下去。
The string so far is 到目前为止的字符串是
10.255.200.0/24";i:1;s:15:"10.255.207.0/24";i:2;s:14:"192.168.0.0/21
I now need to delete everything between the speech marks and send it to a new line. 现在,我需要删除语音标记之间的所有内容并将其发送到新行。
desired output: 所需的输出:
10.255.200.0/24
10.255.207.0/24
192.168.0.0/21
any help would be greatly appreciated. 任何帮助将不胜感激。
$ tr '"' '\n' <<< $string | awk 'NR%2'
10.255.200.0/24
10.255.207.0/24
192.168.0.0/21
You could use : 您可以使用:
echo 'INPUT STRING HERE' | sed $'s/"[^"]*"/\\\n/g'
Explanation : 说明:
sed 's/<PATTERN1>/<PATTERN2/g'
: we substitute every occurrence of PATTERN1
by PATTERN2
sed 's/<PATTERN1>/<PATTERN2/g'
:用PATTERN2
替换每次出现的PATTERN1
[^"]*
: any character that is not a "
, any number of time [^"]*
:任何不是"
字符,任何时间 \\\\\\n
: syntax for newline in sed ( reference here ) \\\\\\n
:sed中换行符的语法( 在此处参考 ) Considering that your Input_file is same as shown sample then could you please try following. 考虑到您的Input_file与所示示例相同,那么您可以尝试以下操作。
awk '
{
while(match($0,/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\/[0-9]+/)){
print substr($0,RSTART,RLENGTH)
$0=substr($0,RSTART+RLENGTH)
}
}' Input_file
This might work for you (GNU sed): 这可能对您有用(GNU sed):
sed 's/"[^"]*"/\n/g' file
Or using along side Bash: 或与Bash一起使用:
sed $'/"[^"]*"/\\n/g' file
Or using most other sed's: 或使用其他大多数sed:
sed ':a;/"[^"]*"\(.*\)\(.\|$\)/{G;s//\2\1/;ba}' file
This uses the feature that an unadulterated hold space contains a newline. 这使用了无保留空间包含换行符的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.