简体   繁体   English

AWK 字段分隔符新行以匹配开头

[英]AWK field seperator new line begin with match

I have a file with repeat attributed seperated by one common value -- .我有一个带有重复属性的文件,由一个常见值分隔--

The idea is to take what is in between and use awk to print the new line values in one line so I can sort by unique count.这个想法是取两者之间的内容并使用awk在一行中打印新的行值,以便我可以按唯一计数进行排序。

File:文件:

--
country = canada
region = ontario
isp = bell
type = isp
--
country = usa
region = newyork
isp = comcast
type = isp
--

Desired output:所需的 output:

country = canada, region = ontario, isp = bell, type = isp
country = usa, region = newyork, isp = comcast, type = isp

There are a few ways to accomplish this.有几种方法可以做到这一点。

One would be to reset/print a string variable every time you hit the -- lines, and in between concatenate line contents to that variable.一种方法是在每次点击--行时重置/打印一个字符串变量,并在将行内容连接到该变量之间。

awk '/^--$/ { 
  if(line) print substr(line,3);
  line=""; 
  next; 
}
/\S/ { line = line ", " $0; }'

Another would be to change the record separator (RS) to --\n , and then use some regex subs to replace the CRs with commas:另一个是将记录分隔符(RS)更改为--\n ,然后使用一些正则表达式 subs 用逗号替换 CR:

awk -v RS="--\n" '{ 
  gsub(/^[\n \t]+|[\n \t]+$/, "");
  gsub(/\n+/, ", "); 
  if($0) print $0; 
}'
awk -F "\n" -v RS="--" -v OFS=", " 'NF >= 5 { print $2, $3, $4, $5 }' file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM