Unix 基于星号字符(*)连接行
我有一个文本文件,其中有几行以“I”开头,一些以“u>I”开头,另一些为“?”,并且在这些行下面总是有两行,我想将这两行(附加)与具有“anywhere”的行连接起来 尝试了awk、sed,但没有得到可以解决此问题的确切字符串 文件数据:-Unix 基于星号字符(*)连接行,unix,awk,sed,gawk,Unix,Awk,Sed,Gawk,我有一个文本文件,其中有几行以“I”开头,一些以“u>I”开头,另一些为“?”,并且在这些行下面总是有两行,我想将这两行(附加)与具有“anywhere”的行连接起来 尝试了awk、sed,但没有得到可以解决此问题的确切字符串 文件数据:- *i 80.95.226.72/29 n/a None 193.105.101.26
*i 80.95.226.72/29 n/a None
193.105.101.26 None -
28834 64690
*? 85.118.193.0/25 n/a None
193.105.101.26 None -
28834 64814
u*>i 213.226.55.0/24 n/a None
193.105.101.27 None -
28834 64757
*i 80.65.79.160/27 n/a None
193.105.101.26 None -
28834 9146
预期产出:-
*i,80.95.226.72/29,n/a,None,193.105.101.26 ,None,-,28834,64690
*?,85.118.193.0/25,n/a,None,193.105.101.26 ,None,-,28834,64814
u*>i,213.226.55.0/24,n/a,None193.105.101.27 ,None,-,28834,9146
awk无论任何行中的字段数是多少,解决方案都是:
$ cat tst.awk
BEGIN{OFS=","}
NF && p && !/\*/ { p = p OFS coalesce(); next}
/\*/{ if (p) print p; p=coalesce()}0
function coalesce(){
s=""
for (i=1;i<=NF;i++) s=s (i==NF ? $i : $i OFS)
return s
}
或作为oneliner:
BEGIN{OFS=","} # set Output Field Separator to ","
NF && p && !/\*/ { # if nr of fields > 0, not containing
# '*' and p is set
p = p OFS coalesce(); next # add formatted $0 to p
}
/\*/{ # if $0 contains '*'
if (p) print p; # if p then print it
p=coalesce() # initialize p
}0 # ignore rest of lines
function coalesce(){
s=""
for (i=1;i<=NF;i++) s=s (i==NF ? $i : $i OFS)
return s
}
$ awk -f tst.awk input.txt
*i,80.95.226.72/29,n/a,None,193.105.101.26,None,-,28834,64690
*?,85.118.193.0/25,n/a,None,193.105.101.26,None,-,28834,64814
u*>i,213.226.55.0/24,n/a,None,193.105.101.27,None,-,28834,64757
$ awk -v OFS=, '$0 && p && !/\*/ { p = p OFS coalesce(); next}/\*/{ if (p) print p; p=coalesce()}0;function coalesce(){s="";for (i=1;i<=NF;i++) s=s (i==NF ? $i : $i OFS);return s}' input.txt
$awk-v OFS=,'$0&&p&&!/\*/{p=p of s coalesce();next}/\*/{if(p)print p;p=coalesce()}0;函数coalesce(){s=”“;for(i=1;i一行线awk
$ awk -v OFS=, 'NF{$1=$1; s=(s?s OFS:"") $0}NF==2 && s{print s; s=""}' infile
要解决您的帖子标题:
awk -v OFS=, -v n=2 'NF{$1=$1}/\*/{s=$0;next}s && NF && n>=c++{s = s OFS $0}c==n{print s; s=c=""}' infile
awk -v OFS=, -v n=2 '
NF{
$1 = $1
}
/\*/{
s=$0
next
}
s && NF && n>=c++{
s = s OFS $0
}
c==n{
print s
s = c = ""
}
' infile
$ cat infile
*i 80.95.226.72/29 n/a None
193.105.101.26 None -
28834 64690
*? 85.118.193.0/25 n/a None
193.105.101.26 None -
28834 64814
u*>i 213.226.55.0/24 n/a None
193.105.101.27 None -
28834 64757
$ awk -v OFS=, 'NF{$1=$1; s=(s?s OFS:"") $0}NF==2 && s{print s; s=""}' infile
*i,80.95.226.72/29,n/a,None,193.105.101.26,None,-,28834,64690
*?,85.118.193.0/25,n/a,None,193.105.101.26,None,-,28834,64814
u*>i,213.226.55.0/24,n/a,None,193.105.101.27,None,-,28834,64757
$ awk -v OFS=, -v n=2 'NF{$1=$1}/\*/{s=$0;next}s && NF && n>=c++{s = s OFS $0}c==n{print s; s=c=""}' infile
*i,80.95.226.72/29,n/a,None,193.105.101.26,None,-,28834,64690
*?,85.118.193.0/25,n/a,None,193.105.101.26,None,-,28834,64814
u*>i,213.226.55.0/24,n/a,None,193.105.101.27,None,-,28834,64757
可读性更好:
awk -v OFS=, -v n=2 'NF{$1=$1}/\*/{s=$0;next}s && NF && n>=c++{s = s OFS $0}c==n{print s; s=c=""}' infile
awk -v OFS=, -v n=2 '
NF{
$1 = $1
}
/\*/{
s=$0
next
}
s && NF && n>=c++{
s = s OFS $0
}
c==n{
print s
s = c = ""
}
' infile
$ cat infile
*i 80.95.226.72/29 n/a None
193.105.101.26 None -
28834 64690
*? 85.118.193.0/25 n/a None
193.105.101.26 None -
28834 64814
u*>i 213.226.55.0/24 n/a None
193.105.101.27 None -
28834 64757
$ awk -v OFS=, 'NF{$1=$1; s=(s?s OFS:"") $0}NF==2 && s{print s; s=""}' infile
*i,80.95.226.72/29,n/a,None,193.105.101.26,None,-,28834,64690
*?,85.118.193.0/25,n/a,None,193.105.101.26,None,-,28834,64814
u*>i,213.226.55.0/24,n/a,None,193.105.101.27,None,-,28834,64757
$ awk -v OFS=, -v n=2 'NF{$1=$1}/\*/{s=$0;next}s && NF && n>=c++{s = s OFS $0}c==n{print s; s=c=""}' infile
*i,80.95.226.72/29,n/a,None,193.105.101.26,None,-,28834,64690
*?,85.118.193.0/25,n/a,None,193.105.101.26,None,-,28834,64814
u*>i,213.226.55.0/24,n/a,None,193.105.101.27,None,-,28834,64757
说明:
awk -v OFS=, -v n=2 'NF{$1=$1}/\*/{s=$0;next}s && NF && n>=c++{s = s OFS $0}c==n{print s; s=c=""}' infile
awk -v OFS=, -v n=2 '
NF{
$1 = $1
}
/\*/{
s=$0
next
}
s && NF && n>=c++{
s = s OFS $0
}
c==n{
print s
s = c = ""
}
' infile
$ cat infile
*i 80.95.226.72/29 n/a None
193.105.101.26 None -
28834 64690
*? 85.118.193.0/25 n/a None
193.105.101.26 None -
28834 64814
u*>i 213.226.55.0/24 n/a None
193.105.101.27 None -
28834 64757
$ awk -v OFS=, 'NF{$1=$1; s=(s?s OFS:"") $0}NF==2 && s{print s; s=""}' infile
*i,80.95.226.72/29,n/a,None,193.105.101.26,None,-,28834,64690
*?,85.118.193.0/25,n/a,None,193.105.101.26,None,-,28834,64814
u*>i,213.226.55.0/24,n/a,None,193.105.101.27,None,-,28834,64757
$ awk -v OFS=, -v n=2 'NF{$1=$1}/\*/{s=$0;next}s && NF && n>=c++{s = s OFS $0}c==n{print s; s=c=""}' infile
*i,80.95.226.72/29,n/a,None,193.105.101.26,None,-,28834,64690
*?,85.118.193.0/25,n/a,None,193.105.101.26,None,-,28834,64814
u*>i,213.226.55.0/24,n/a,None,193.105.101.27,None,-,28834,64757
NF{$1=$1}
如果行/记录/行至少有一个字段(NF
中没有字段
记录),然后重新编译记录,因为当您执行$1=$1
(或任何
对字段的其他赋值)它会导致记录重新编译,其中$0
重建时,将每个FS
替换为OFS
/\*/{s=$0;next}
如果行包含*
,则将变量s
分配为当前值
记录/行/行,然后转到下一行
s&&NF&&n>=c++{s=s of s$0}
如果设置了变量s
,并且记录至少有1个字段和变量
n
大于或等于变量c++
(++
增量
运算符),然后连接变量s
c==n{print s;s=c=”“}
如果变量c
等于变量n
,则打印变量s
,并
将变量s
和c
输入:
awk -v OFS=, -v n=2 'NF{$1=$1}/\*/{s=$0;next}s && NF && n>=c++{s = s OFS $0}c==n{print s; s=c=""}' infile
awk -v OFS=, -v n=2 '
NF{
$1 = $1
}
/\*/{
s=$0
next
}
s && NF && n>=c++{
s = s OFS $0
}
c==n{
print s
s = c = ""
}
' infile
$ cat infile
*i 80.95.226.72/29 n/a None
193.105.101.26 None -
28834 64690
*? 85.118.193.0/25 n/a None
193.105.101.26 None -
28834 64814
u*>i 213.226.55.0/24 n/a None
193.105.101.27 None -
28834 64757
$ awk -v OFS=, 'NF{$1=$1; s=(s?s OFS:"") $0}NF==2 && s{print s; s=""}' infile
*i,80.95.226.72/29,n/a,None,193.105.101.26,None,-,28834,64690
*?,85.118.193.0/25,n/a,None,193.105.101.26,None,-,28834,64814
u*>i,213.226.55.0/24,n/a,None,193.105.101.27,None,-,28834,64757
$ awk -v OFS=, -v n=2 'NF{$1=$1}/\*/{s=$0;next}s && NF && n>=c++{s = s OFS $0}c==n{print s; s=c=""}' infile
*i,80.95.226.72/29,n/a,None,193.105.101.26,None,-,28834,64690
*?,85.118.193.0/25,n/a,None,193.105.101.26,None,-,28834,64814
u*>i,213.226.55.0/24,n/a,None,193.105.101.27,None,-,28834,64757
输出-1:
awk -v OFS=, -v n=2 'NF{$1=$1}/\*/{s=$0;next}s && NF && n>=c++{s = s OFS $0}c==n{print s; s=c=""}' infile
awk -v OFS=, -v n=2 '
NF{
$1 = $1
}
/\*/{
s=$0
next
}
s && NF && n>=c++{
s = s OFS $0
}
c==n{
print s
s = c = ""
}
' infile
$ cat infile
*i 80.95.226.72/29 n/a None
193.105.101.26 None -
28834 64690
*? 85.118.193.0/25 n/a None
193.105.101.26 None -
28834 64814
u*>i 213.226.55.0/24 n/a None
193.105.101.27 None -
28834 64757
$ awk -v OFS=, 'NF{$1=$1; s=(s?s OFS:"") $0}NF==2 && s{print s; s=""}' infile
*i,80.95.226.72/29,n/a,None,193.105.101.26,None,-,28834,64690
*?,85.118.193.0/25,n/a,None,193.105.101.26,None,-,28834,64814
u*>i,213.226.55.0/24,n/a,None,193.105.101.27,None,-,28834,64757
$ awk -v OFS=, -v n=2 'NF{$1=$1}/\*/{s=$0;next}s && NF && n>=c++{s = s OFS $0}c==n{print s; s=c=""}' infile
*i,80.95.226.72/29,n/a,None,193.105.101.26,None,-,28834,64690
*?,85.118.193.0/25,n/a,None,193.105.101.26,None,-,28834,64814
u*>i,213.226.55.0/24,n/a,None,193.105.101.27,None,-,28834,64757
输出-2:
awk -v OFS=, -v n=2 'NF{$1=$1}/\*/{s=$0;next}s && NF && n>=c++{s = s OFS $0}c==n{print s; s=c=""}' infile
awk -v OFS=, -v n=2 '
NF{
$1 = $1
}
/\*/{
s=$0
next
}
s && NF && n>=c++{
s = s OFS $0
}
c==n{
print s
s = c = ""
}
' infile
$ cat infile
*i 80.95.226.72/29 n/a None
193.105.101.26 None -
28834 64690
*? 85.118.193.0/25 n/a None
193.105.101.26 None -
28834 64814
u*>i 213.226.55.0/24 n/a None
193.105.101.27 None -
28834 64757
$ awk -v OFS=, 'NF{$1=$1; s=(s?s OFS:"") $0}NF==2 && s{print s; s=""}' infile
*i,80.95.226.72/29,n/a,None,193.105.101.26,None,-,28834,64690
*?,85.118.193.0/25,n/a,None,193.105.101.26,None,-,28834,64814
u*>i,213.226.55.0/24,n/a,None,193.105.101.27,None,-,28834,64757
$ awk -v OFS=, -v n=2 'NF{$1=$1}/\*/{s=$0;next}s && NF && n>=c++{s = s OFS $0}c==n{print s; s=c=""}' infile
*i,80.95.226.72/29,n/a,None,193.105.101.26,None,-,28834,64690
*?,85.118.193.0/25,n/a,None,193.105.101.26,None,-,28834,64814
u*>i,213.226.55.0/24,n/a,None,193.105.101.27,None,-,28834,64757
你也可以试试下面的awk吗
awk 'NF{gsub(/ +/,",");printf("%s",!/^,/?(NR!=1?RS $0:$0):$0)} END{print ""}' Input_file
输出如下
*i,80.95.226.72/29,n/a,None,193.105.101.26,None,-,28834,64690
*?,85.118.193.0/25,n/a,None,193.105.101.26,None,-,28834,64814
u*>i,213.226.55.0/24,n/a,None,193.105.101.27,None,-,28834,64757
现在还增加了解释和一种非线性形式的解决方案
awk '
NF{ ##If a line is NON-empty then do following steps.
gsub(/ +/,","); ##Globally substituting all continuous spaces with a single comma in each non-empty line.
printf("%s",!/^,/?(NR!=1?RS $0:$0):$0) ##printing line here with a condition if line number is NOT equal to 1 and starts with comma then print RS(whose default value is a new line and current line) else print current line, outer else print current line.
}
END{
print "" ##Printing NULL value here to print a new line.
}
' Input_file ##Mentioning Input_file name here too.
请正确编辑布局。用鼠标绘制数据,然后单击编辑器中的{}
。