我需要有关 bash 脚本编写的帮助。以下是我的输入:
Grp: MG1
user1
user2
user3
Grp: MG2
user7
user1
user9
user6
user2
结果应该如下所示:
Reporting MG1
MG1,user1
MG1,user2
MG1,user3
Reporting MG2
MG2,user7
MG2,user1
MG2,user9
MG2,user6
MG2,user2
我尝试过sed -n '/cn:/,/cn:/p' file
,但它没有达到我想要的效果。
答案1
这是用于awk
文本格式化的正确工具:
awk '/^Grp:/ { OFS=" "; $1= "Reporting"; mg=$2; print; next}
{ OFS=","; print mg, $0}' infile
答案2
使用sed
:
$ cat script.sed
/^Grp: / { ;# A "Grp: " line
s/// ;# Remove "Grp: "
h ;# Save in hold space
s/^/Reporting /p ;# Insert "Reporting " at start, print
d ;# Delete, start next cycle
}
# Any other line:
G ;# Append the hold space
s/\(.*\)\n\(.*\)/\2,\1/ ;# Swap strings around \n, insert comma
$ sed -f script.sed file
Reporting MG1
MG1,user1
MG1,user2
MG1,user3
Reporting MG2
MG2,user7
MG2,user1
MG2,user9
MG2,user6
MG2,user2
作为“一行”:
sed -e '/^Grp: /{s///;h;s/^/Reporting /p;d;}' \
-e 'G;s/\(.*\)\n\(.*\)/\2,\1/' file
与上述类似的方法awk
:
awk '/^Grp: / { sub("^Grp: ", ""); group = $0; print "Reporting " $0; next }
{ print group "," $0 }' file
这个答案中的和变体(以及下面末尾的变体)都会处理数据中的空格,无论是在字符串中sed
还是在字符串中:awk
sh
MG
user
$ cat file
Grp: some group ID
line 1
the other line
$ sed -e '/^Grp: /{s///;h;s/^/Reporting /p;d;}' -e 'G;s/\(.*\)\n\(.*\)/\2,\1/' file
Reporting some group ID
some group ID,line 1
some group ID,the other line
就像一个有趣的练习一样,使用/bin/sh
:
while IFS= read -r line; do
case $line in
'Grp: '*)
group=${line#Grp: }
printf 'Reporting %s\n' "$group"
;;
*)
printf '%s,%s\n' "$group" "$line"
esac
done
运行与
sh script.sh <file
答案3
鉴于上面的示例输入,您可以使用以下内容:
#!/bin/bash
group=""
while read line; do
if [[ "${line}" =~ ^Grp:* ]]; then
group="$(echo "${line}" | awk '{ print $2 }')"
echo "Reporting ${group}"
elif [[ "${line}" == "" ]]; then
echo
else
echo "${group},${line}"
fi
done
例如:
$ cat input
Grp: MG1
user1
user2
user3
Grp: MG2
user7
user1
user9
user6
user2
$
$ ./ex.sh < input
Reporting MG1
MG1,user1
MG1,user2
MG1,user3
Reporting MG2
MG2,user7
MG2,user1
MG2,user9
MG2,user6
MG2,user2
$
该脚本运行一个读取一行文本的循环。如果该行以 开头Grp:
,则它将第二个空格分隔的标记保存为group
。如果该行为空,则打印空行。否则,它会打印最后读取的组,后跟逗号,然后是该行的内容。