提取括号前的字符串并创建新行
Extract String before bracket and create new line
我有以下格式的数据
ABC-ERW 12344 ZYX 12345
FFANKN 2345 QW [123457, 89053]
FAFDJ-ER 1234 MNO [6532, 789, 234578]
我想使用 sed 或 awk 创建以下格式的数据。
ABC-ERW 12344 ZYX 12345
FFANKN 2345 QW 123457
FFANKN 2345 QW 89053
FAFDJ-ER 1234 MNO 6532
FAFDJ-ER 1234 MNO 789
FAFDJ-ER 1234 MNO 234578
我可以提取括号前的数据,但我不知道如何将相同的数据与括号中的数据重复连接。
我的努力:--
# !/bin/bash
while IFS= read -r line
do
echo "$line"
cnt=`echo $line | grep -o "\[" | wc -l`
if [ $cnt -gt 0 ]
then
startstr=`echo $line | awk -F[ '{print }'`
echo $startstr
intrstr=`echo $line | cut -d "[" -f2 | cut -d "]" -f1`
echo $intrstr
else
echo "$line" >> newfile.txt
fi
done < 1.txt
我能够获取第一部分并在新文件中保留没有“[”的行,但我不知道如何获取“[”中的值并在末尾将其作为“中的变量数”传递[" 保持随机变化。
此致
使用您展示的示例,请尝试以下 awk
代码。
awk '
match([=10=],/\[[^]]*\]$/){
num=split(substr([=10=],RSTART+1,RLENGTH-2),arr,", ")
for(i=1;i<=num;i++){
print substr([=10=],1,RSTART-1) arr[i]
}
next
}
1
' Input_file
说明:为以上代码添加详细说明。
awk ' ##Starting awk program from here.
match([=11=],/\[[^]]*\]$/){ ##Using match function to match from [ till ] at the end of line.
num=split(substr([=11=],RSTART+1,RLENGTH-2),arr,", ") ##Splitting matched values by regex above and passing into array named arr with delimiters comma and space.
for(i=1;i<=num;i++){ ##Running for loop till value of num.
print substr([=11=],1,RSTART-1) arr[i] ##printing sub string before matched along with element of arr with index of i.
}
next ##next will skip all further statements from here.
}
1 ##1 will print current line.
' Input_file ##Mentioning Input_file name here.
建议简单的 awk
脚本:
awk 'NR==1{print}{for (i=2;i<NF;i++)print , $i}' FS="( \\[)|(, )|(\\]$)" input.1.txt
解释:
FS="( \\[)|(, )|(\\]$)"
将 awk
字段分隔符设置为 [
,
]EOL
这将使有趣的字段 </code> ---> <code>$FN
附加到 </code></p>
<p><code>NR==1{print}
仅按原样打印第一行。
{for (i=2;i<NF;i++)print , $i}
对于第 2 行,打印:字段 $1 追加当前字段。
这可能适合您 (GNU sed):
sed -E '/(.*)\[([^,]*), /{s//\n[/;P;D};s/[][]//g' file
匹配开始方括号之前的字符串以及逗号之前和 space.
之前的字符串
用前导和尾随匹配字符串替换整个匹配项,后跟换行符和前导匹配字符串。
Print/delete 第一行并重复。
上面任何重复的最后一行都将失败,因为没有尾随逗号 space,在这种情况下,左右方括号也应该被删除。
选择:
sed -E ':a;s/([^\n]*)\[([^,]*), /\n[/;ta;s/[][]//g' file
我有以下格式的数据
ABC-ERW 12344 ZYX 12345
FFANKN 2345 QW [123457, 89053]
FAFDJ-ER 1234 MNO [6532, 789, 234578]
我想使用 sed 或 awk 创建以下格式的数据。
ABC-ERW 12344 ZYX 12345
FFANKN 2345 QW 123457
FFANKN 2345 QW 89053
FAFDJ-ER 1234 MNO 6532
FAFDJ-ER 1234 MNO 789
FAFDJ-ER 1234 MNO 234578
我可以提取括号前的数据,但我不知道如何将相同的数据与括号中的数据重复连接。
我的努力:--
# !/bin/bash
while IFS= read -r line
do
echo "$line"
cnt=`echo $line | grep -o "\[" | wc -l`
if [ $cnt -gt 0 ]
then
startstr=`echo $line | awk -F[ '{print }'`
echo $startstr
intrstr=`echo $line | cut -d "[" -f2 | cut -d "]" -f1`
echo $intrstr
else
echo "$line" >> newfile.txt
fi
done < 1.txt
我能够获取第一部分并在新文件中保留没有“[”的行,但我不知道如何获取“[”中的值并在末尾将其作为“中的变量数”传递[" 保持随机变化。
此致
使用您展示的示例,请尝试以下 awk
代码。
awk '
match([=10=],/\[[^]]*\]$/){
num=split(substr([=10=],RSTART+1,RLENGTH-2),arr,", ")
for(i=1;i<=num;i++){
print substr([=10=],1,RSTART-1) arr[i]
}
next
}
1
' Input_file
说明:为以上代码添加详细说明。
awk ' ##Starting awk program from here.
match([=11=],/\[[^]]*\]$/){ ##Using match function to match from [ till ] at the end of line.
num=split(substr([=11=],RSTART+1,RLENGTH-2),arr,", ") ##Splitting matched values by regex above and passing into array named arr with delimiters comma and space.
for(i=1;i<=num;i++){ ##Running for loop till value of num.
print substr([=11=],1,RSTART-1) arr[i] ##printing sub string before matched along with element of arr with index of i.
}
next ##next will skip all further statements from here.
}
1 ##1 will print current line.
' Input_file ##Mentioning Input_file name here.
建议简单的 awk
脚本:
awk 'NR==1{print}{for (i=2;i<NF;i++)print , $i}' FS="( \\[)|(, )|(\\]$)" input.1.txt
解释:
FS="( \\[)|(, )|(\\]$)"
将 awk
字段分隔符设置为 [
,
]EOL
这将使有趣的字段 </code> ---> <code>$FN
附加到 </code></p>
<p><code>NR==1{print}
仅按原样打印第一行。
{for (i=2;i<NF;i++)print , $i}
对于第 2 行,打印:字段 $1 追加当前字段。
这可能适合您 (GNU sed):
sed -E '/(.*)\[([^,]*), /{s//\n[/;P;D};s/[][]//g' file
匹配开始方括号之前的字符串以及逗号之前和 space.
之前的字符串用前导和尾随匹配字符串替换整个匹配项,后跟换行符和前导匹配字符串。
Print/delete 第一行并重复。
上面任何重复的最后一行都将失败,因为没有尾随逗号 space,在这种情况下,左右方括号也应该被删除。
选择:
sed -E ':a;s/([^\n]*)\[([^,]*), /\n[/;ta;s/[][]//g' file