使用awk匹配&;存储、追加模式并拆分具有分隔符的行

使用awk匹配&;存储、追加模式并拆分具有分隔符的行,awk,Awk,我有下面的输出从一个文本文件,我需要格式更可读 julian text:case2345 maria text:case4567 clover text,text,text,text,text,text:case3456 neil text,text:case09876 我需要按如下方式重新格式化输出: julian text:case2345 maria text:case4567 clover text:case3456 clover text:case3456 clover t

我有下面的输出从一个文本文件,我需要格式更可读

julian text:case2345
maria  text:case4567
clover text,text,text,text,text,text:case3456
neil   text,text:case09876
我需要按如下方式重新格式化输出:

julian text:case2345
maria  text:case4567
clover text:case3456 
clover text:case3456
clover text:case3456
clover text:case3456
clover text:case3456
clover text:case3456
neil   text:case09876
neil   text:case09876
使用
awk
我试图匹配模式大小写[0-9],将其存储在变量中,然后使用分隔符“,”拆分行,最后打印。 我之前尝试过下面的内容,但无法获得所需的输出

awk '/match($0,/case[0-9]/){val=substr($0,RSTART,RLENGTH);next}{split($2,k,","); for (i in k) {printf ("%s %s %s\n\n",$1,k[i],val)}}'
测试结果:

$ cat infile
julian text:case2345
maria  text:case 4567
clover text,text,text,text,text,text:case3456
neil   text,text:case09876

$ awk -F '[: ]+' '/,/{split($2,arr,/,/);for(i=1; i in arr;i++)print $1,arr[i]":"$NF;next}1' infile
julian text:case2345
maria  text:case 4567
clover text:case3456
clover text:case3456
clover text:case3456
clover text:case3456
clover text:case3456
clover text:case3456
neil text:case09876
neil text:case09876
测试结果:

$ cat infile
julian text:case2345
maria  text:case 4567
clover text,text,text,text,text,text:case3456
neil   text,text:case09876

$ awk -F '[: ]+' '/,/{split($2,arr,/,/);for(i=1; i in arr;i++)print $1,arr[i]":"$NF;next}1' infile
julian text:case2345
maria  text:case 4567
clover text:case3456
clover text:case3456
clover text:case3456
clover text:case3456
clover text:case3456
clover text:case3456
neil text:case09876
neil text:case09876

下面的
awk
可能会有所帮助。(考虑到您的实际输入文件是所示的示例)

awk-F'+|,|:'$NF~/[cC][aA][sS][eE]/&NF>2{for(i=2;i2{
对于(i=2;i2{###检查条件,如果最后一个字段中有大小写或大小写字符串,并且字段数超过2。

对于(i=2;i以下
awk
可能有帮助。(考虑到您的实际输入文件就是所示的示例)

awk-F'+|,|:'$NF~/[cC][aA][sS][eE]/&NF>2{for(i=2;i2{
对于(i=2;i2{###检查条件,如果最后一个字段中有大小写或大小写字符串,并且字段数超过2。
对于(i=2;i只需调整:

$awk-F'[,:]+'{for(i=2;i只需调整:


$awk-F'[,:]+'{for(i=2;ithanks@Akshay..你能解释一下这是什么-->'[:]+'以及$NF存储字段的最后一个值吗?以及如何在$NF中赋值吗?找到了gnu手册,其中解释了$NF将始终代表最后一个字段谢谢@Akshay..你能解释一下这是什么-->'[:]+'还有,$NF是否存储字段的最后一个值?以及如何在$NF中分配该值?找到gnu手册,其中解释$NF将始终表示最后一个字段@感谢Ravinder的解释。.真的很有帮助,我正在进行同样的工作将确认-->awk-F'+|是否为空格设置字段分隔符?符号是什么“+”@shirop的重要性,
+
意味着将一个以上连续出现的空格放在一起(作为单个字段)
$NF~/[cCaAsSeE]/
在最终脚本中与
$NF~/[cC][aA][sS][eE]不同在您的前2个版本中,/<代码>,但无论如何,只需调用ToWar,而不是在ReGEXP中的每个字符中创建括号表达式中的大写和小写字符列表,例如使用<代码> ToWar($NF)~/CAS/ >而不是<代码> $NF~/[cc] [AA] [SS] [EE]/
。另外,
i@thanksRavinder的解释..真的很有帮助,我正在做同样的工作将确认-->awk-F'+|是否为空格设置字段分隔符?'+'@shiro的意义是什么,
+
意味着将多个空格连续出现在一起(作为单个字段)
$NF~/[cCaAsSeE]在您的最终脚本中,在您的前2个版本中,您的最终脚本与“代码> $NF~/[Acc] [As] [SS] [EE] /<代码>不一样,但无论如何,只需调用ToWar,而不是在ReGEXP中的每个字符中创建括号表达式中的大写和小写字符列表,例如使用<代码> ToWar($NF)~/Case/<代码>,而不是<代码$FF~/[cc] [AA] [SS] [EE]/
。此外,
这是一个输入错误,只是查看了实际输入文件,在“case”和数字之间没有空格。。感谢您注意到这是一个输入错误,只是查看了实际输入文件,在“case”和数字之间没有空格。。感谢您注意到for循环中的拆分是如何发生的,因为我们不明确只提到在split中使用哪一个分隔符..比如说like..split($2,arr,/,/)@Thanksy您不需要对split()进行单独的显式调用,因为awk已经根据我正在设置的FS值将每个记录拆分为字段+“
因此,awk每次看到空格、逗号或冒号时都会拆分输入。如果我们没有明确提到在拆分中使用哪个分隔符,那么在for循环中拆分是如何进行的呢?比如说like..split($2,arr,/,/)@Thanksy您不需要单独、明确地调用split()因为awk已经根据我用
-F'[,:]+'
设置的FS值将每条记录拆分为字段,所以awk每次看到空格、逗号或冒号时都会拆分输入。
awk -F' +|,|:' '$NF~/[cC][aA][sS][eE]/ && NF>2{for(i=2;i<=(NF-1);i++){print $1 OFS $i":"$NF};next} 1' Input_file
awk -F' +|,|:' '
$NF~/[cC][aA][sS][eE]/ && NF>2{
  for(i=2;i<=(NF-1);i++){
    print $1 OFS $i":"$NF};
  next
}
1
'  Input_file
awk -F' +|,|:' '           ##Setting field separator as space(s) OR comma OR colon here for each line.
$NF~/[cCaAsSeE]/ && NF>2{  ##Checking condition here if last field is having case OR CASE string in it and number of fields are more than 2.
  for(i=2;i<=(NF-1);i++){  ##Starting a for loop which starts from 2nd value to second last value of total fields value here.
    print $1 OFS $i":"$NF};##first field OFS(whose default value is space) value of current field and colon with last field of line.
  next                     ##next is awk default keyword which will skip all further lines now.
}
1                          ##Only those lines will come here which was NOT true for above conditions, simple printing of line will happen here.
' Input_file               ##Mentioning Input_file name here.
$ awk -F'[ ,:]+' '{for (i=2;i<NF;i++) print $1, $i ":" $NF}' file
julian text:case2345
maria text:case4567
clover text:case3456
clover text:case3456
clover text:case3456
clover text:case3456
clover text:case3456
clover text:case3456
neil text:case09876
neil text:case09876