Linux 逐行切割
我想一行一行地通过剪切(很像Linux 逐行切割,linux,bash,zsh,cut,gnu-coreutils,Linux,Bash,Zsh,Cut,Gnu Coreutils,我想一行一行地通过剪切(很像egrep--line-buffered)。我该怎么做?[awk,sed,perl解决方案欢迎使用,请尽量简短。]如果您只想在UNIX中模拟循环切入功能(这可能非常慢),可以使用awk的substr函数 e、 例如,假设您有一个文本文件(lines.txt),其排列如下: line1: the first line line2: the second line line3: the third line line4: the fourth line line5: th
egrep--line-buffered
)。我该怎么做?[awk
,sed
,perl
解决方案欢迎使用,请尽量简短。]如果您只想在UNIX中模拟循环切入功能(这可能非常慢),可以使用awk的substr函数
e、 例如,假设您有一个文本文件(lines.txt),其排列如下:
line1: the first line
line2: the second line
line3: the third line
line4: the fourth line
line5: the fifth line
对于这一行,其中8是要开始打印每一行的字符的索引:
awk '{ print substr($0,8) }' lines.txt
结果是:
the first line
the second line
the third line
the fourth line
the fifth line
如果要删除特定的单词或regexp,可以将其作为字段分隔符输入awk,然后打印要删除的部分后面的行部分:
例如,这一行:
awk 'BEGIN { FS=": " } { print $2 }' lines.txt
还将打印出:
the first line
the second line
the third line
the fourth line
the fifth line
因为您可以利用每一行都包含一个分号,后跟一个空格(“:”),将该行分成两部分。然后,您只需告诉awk打印每行的第二部分(即字段)(打印$2)如果您只想在UNIX中模拟循环切入功能(这可能非常慢),您可以使用awk的substr函数 e、 例如,假设您有一个文本文件(lines.txt),其排列如下:
line1: the first line
line2: the second line
line3: the third line
line4: the fourth line
line5: the fifth line
对于这一行,其中8是要开始打印每一行的字符的索引:
awk '{ print substr($0,8) }' lines.txt
结果是:
the first line
the second line
the third line
the fourth line
the fifth line
如果要删除特定的单词或regexp,可以将其作为字段分隔符输入awk,然后打印要删除的部分后面的行部分:
例如,这一行:
awk 'BEGIN { FS=": " } { print $2 }' lines.txt
还将打印出:
the first line
the second line
the third line
the fourth line
the fifth line
因为您可以利用每一行都包含一个分号,后跟一个空格(“:”),将该行分成两部分。然后您只需告诉awk打印每行的第二部分(即字段)(打印$2)就可以使用
stdbuf
实用程序来控制使用stdio的任何命令的stdio缓冲
... | stdbuf -oL cut ... | ...
可以使用
stdbuf
实用程序控制使用stdio的任何命令的stdio缓冲
... | stdbuf -oL cut ... | ...
非常感谢。这很好用。我不知道awk是行缓冲的。:)不客气:)awk的默认行为是假设每一行都是一个单独的输入单元,或“记录”。如果要更改此行为,可以在“BEGIN”块中设置“record separator”或“RS”,即“BEGIN{RS=“x”}”;在这种情况下,就好像字母“x”表示每条记录的开始一样。谢谢!这很好用。我不知道awk是行缓冲的。:)不客气:)awk的默认行为是假设每一行都是一个单独的输入单元,或“记录”。如果要更改此行为,可以在“BEGIN”块中设置“record separator”或“RS”,即“BEGIN{RS=“x”}”;在这种情况下,它的工作原理就好像字母“x”表示每个记录的开始一样。这是可行的,但似乎使流滞后一行。这是可行的,但似乎使流滞后一行。