开发者

Need to grab data inbetween tilde character

开发者 https://www.devze.com 2023-04-02 21:11 出处:网络
Can any one advise how to search on linux for some data between a tilde character. I ne开发者_StackOverflowed to get IP data however its been formed like the below.

Can any one advise how to search on linux for some data between a tilde character. I ne开发者_StackOverflowed to get IP data however its been formed like the below.

Details:
20110906000418~118.221.246.17~DATA~DATA~DATA


One more:

echo '20110906000418~118.221.246.17~DATA~DATA~DATA' | sed -r 's/[^~]*~([^~]+)~.*/\1/'


echo "20110906000418~118.221.246.17~DATA~DATA~DATA" | cut -d'~' -f2

This uses the cut command with the delimiter set to ~. The -f2 switch then outputs just the 2nd field.

If the text you give is in a file (called filename), try:

grep "[0-9]*~" filename | cut -d'~' -f2


With cut:

 echo "20110906000418~118.221.246.17~DATA~DATA~DATA" | cut -d~ -f2


With awk:

 echo "20110906000418~118.221.246.17~DATA~DATA~DATA" 
       | awk -F~ '{ print $2 }'


In awk:

echo '20110906000418~118.221.246.17~DATA~DATA~DATA' | awk -F~ '{print $2}'


Just use bash

$ string="20110906000418~118.221.246.17~DATA~DATA~DATA"
$ echo ${string#*~}
118.221.246.17~DATA~DATA~DATA
$ string=${string#*~}
$ echo ${string%%~*}
118.221.246.17


one more, using perl:

$ perl -F~ -lane 'print $F[1]' <<< '20110906000418~118.221.246.17~DATA~DATA~DATA'
118.221.246.17

bash:

#!/bin/bash
IFS='~'

while read -a array;
do
    echo ${array[1]}
done < ip

If string is constant, the following parameter expansion performs substring extraction:

$ a=20110906000418~118.221.246.17~DATA~DATA~DATA
$ echo ${a:15:14}
118.221.246.17

or using regular expressions in bash:

$ echo $(expr "$a" : '[^~]*~\([^~]*\)~.*')
118.221.246.17

last one, again using pure bash methods:

$ tmp=${a#*~}
$ echo $tmp
118.221.246.17~DATA~DATA~DATA
$ echo ${tmp%%~*}
118.221.246.17
0

精彩评论

暂无评论...
验证码 换一张
取 消