我需要有關 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
。如果該行為空,則列印空白行。否則,它會列印最後讀取的群組,後面跟著逗號,然後是該行的內容。