-1
    * Each line consists of two fields, separated by a pipe '|', where
    * the first field is a comma-separated list of items, and
    * the second field is a tag.

这是我的输入:

        100,210,354,462|acct
        331,746,50|mis
        90,263,47,14|sales

和所需的输出:

        100acct
        210acct
        354acct
        462acct
        331mis
        746mis
        50mis
        90sales
        263sales
        47sales
        14sales
4

5 回答 5

2
sed '{s/^\([^a-z].*\),\([^a-z].*\),\([^a-z].*\),\([^a-z].*\)|\([^0-9].*\)$/\1\5\n\2\5\n\3\5\n\4\5/;s/^\([^a-z].*\),\([^a-z].*\),\([^a-z].*\)|\([^0-9].*\)$/\1\4\n\2\4\n\3\4/}' filename 
于 2012-10-03T04:54:25.793 回答
2

一种使用方式GNU awk

awk -F "[,|]" '{ for (i=1; i<NF; i++) print $i$NF }' file.txt

结果:

100acct
210acct
354acct
462acct
331mis
746mis
50mis
90sales
263sales
47sales
14sales
于 2012-10-03T05:04:00.790 回答
1

使用以下

sed 's/^\([^a-z].*\),\([^a-z].*\),\([^a-z].*\),\([^a-z].*\)|\([^0-9].*\)$/\1\5\n\2\5\n\3\5\n\4\5/g;s/^\([^a-z].*\),\([^a-z].*\),\([^a-z].*\)|\([^0-9].*\)$/\1\4\n\2\4\n\3\4/g'
于 2012-10-03T05:01:34.203 回答
0
sed 's/\([0-9]\),\([0-9]*\),\([0-9]*\),*\([0-9]*\)\([,|]\)\(.*\)/\1\6\n\2\6\n\3\6\n\4\6/' input | sed '/^[a-z]*$/d'

此表达式为您提供正确的输出。

于 2012-10-03T04:51:19.193 回答
0

这可能对您有用(GNU sed):

sed 's/\s*//;:a;s/,\(.*|\(.*\)\)/\2\n\1/;ta;s/|//' file

解释:

  1. s/\s*//删除记录前面的空格。
  2. :a;s/,\(.*|\(.*\)\)/\2\n\1/;ta,用最后一个字段和换行符替换每个
  3. s/|//去除那个|

要保留空格,请使用:

sed -r 's/(\s*)(.*\|)/\2\1/;:a;s/,(.*\|(.*))/\2\n\1/;ta;s/\|//;s/(\S+)(\s+)(\S+)/\2\1\3/g' file
于 2012-10-03T05:03:53.083 回答