如何在unix中用分隔符拆分字符串

时间:2014-09-27 16:35:07

标签: bash unix awk sed

我有以下字符串,我想将其分为3部分:

文本:

<http://rdf.freebase.com/ns/american_football.football_player.footballdb_id>    <http://www.w3.org/2000/01/rdf-schema#label>    "footballdb ID"@en

输出应为

$1 = <http://rdf.freebase.com/ns/american_football.football_player.footballdb_id>
$2 = <http://www.w3.org/2000/01/rdf-schema#label>
$3 = "footballdb ID"@en

基本上是将RDF的元组拆分成它的部分。 我想通过UNIX脚本执行此操作,但我不知道sed或awk。 请帮忙。

5 个答案:

答案 0 :(得分:3)

如果您的输入字段以制表符分隔,则会产生您发布的所需输出:

$ awk -F'\t' '{ for (i=1;i<=NF;i++) printf "$%d = %s\n", i, $i }' file
$1 = <http://rdf.freebase.com/ns/american_football.football_player.footballdb_id>
$2 = <http://www.w3.org/2000/01/rdf-schema#label>
$3 = "footballdb ID"@en

或者,如果您的字段不是以制表符分隔的,那么这可能是您想要的:

$ cat tst.awk
{
    gsub(/<[^>]+>/,"&\n")
    split($0,a,/[[:space:]]*\n[[:space:]]*/)
    for (i=1; i in a; i++)
        printf "$%d = %s\n", i, a[i]
}
$
$ awk -f tst.awk file
$1 = <http://rdf.freebase.com/ns/american_football.football_player.footballdb_id>
$2 = <http://www.w3.org/2000/01/rdf-schema#label>
$3 = "footballdb ID"@en

如果您的输入字段不是如何分开和/或不是您要输出的内容,请更新您的问题以澄清。

答案 1 :(得分:2)

read A B C <<< $string
echo -e "\$1 = $A\n\$2 = $B\n\$3 = $C" 

输出:

$1 = <http://rdf.freebase.com/ns/american_football.football_player.footballdb_id>
$2 = <http://www.w3.org/2000/01/rdf-schema#label>
$3 = "footballdb ID"@en

答案 2 :(得分:1)

无论你使用什么来分割字符串,都需要不仅识别空格,还要识别双引号“保护”ID之前的空格并防止它分割字段的约定。我担心这种计算可能超出了sed的可能性。你可以用awk来做,但是awk在这里没什么特别的优势。

您使用引号显示以空格分隔的格式。类似的问题是用引号解析逗号分隔格式。相关问题:

答案 3 :(得分:-1)

回声&#34;你的字符串&#34; | awk -F&#34; &#34; &#39; {print $ 1 $ 2 $ 3 $ 4}&#39;

答案 4 :(得分:-2)

awk '{ print "$1 = " $1 "\n$2 = " $2 "\n$3 = " $3 }'  filename