awk替换序列化的数字行并向上移动其他行

时间:2014-06-10 00:54:51

标签: awk sed

我有一个具有以下格式的文件

1 - descrio #944
name
address
2 - desanother #916 
name
address
3 - somedes #957 
name
address

我希望得到输出,

Usercode #944, name, address
Usercode #916, name, address
Usercode #957, name, address 

3 个答案:

答案 0 :(得分:3)

使用awk

awk 'NR%3 == 1{sub(/^.*#/, "Usercode #")};{ORS=NR%3?", ":"\n"};1' file
Usercode #944, name, address
Usercode #916, name, address
Usercode #957, name, address

对于可变行数

awk -v RS='(^|\n)[[:digit:]]+[[:blank:]]*-[[:blank:]]*' '{sub(/\n$/, "");
gsub(/\n/, ", "); printf "%s", $0""RT}END{print ""}' file

答案 1 :(得分:0)

如果您的任何说明中没有#,请尝试:

sed -e 's/.*#/Usercode #/;N;N;s/\n/, /g' input

答案 2 :(得分:0)

您也可以尝试此命令,

$ paste -d'~' - - - < ccc | sed 's/^[^#]*/Usercode /g;s/~/, /g'
Usercode #944, name, address
Usercode #916, name, address
Usercode #957, name, address