简体   繁体   English

如何使用bash打印具有由“n”个字符组成的字符串的列字段的内容?

[英]How to print contents of column fields that have strings composed of "n" character/s using bash?

Say I have a file which contains:假设我有一个文件,其中包含:

22  30  31  3a  31  32  3a  32          " 0 9 : 1 2 : 2
30  32  30  20  32  32  3a  31          1 2 7   2 2 : 1

And, I want to print only the column fields that have string composed of 1 character.而且,我只想打印具有由 1 个字符组成的字符串的列字段。 I want the output to be like this:我希望输出是这样的:

" 0 9 : 1 2 : 2
1 2 7   2 2 : 1

Then, I want to print only those strings that are composed of two characters, the output should be:然后,我只想打印那些由两个字符组成的字符串,输出应该是:

22  30  31  3a  31  32  3a  32
30  32  30  20  32  32  3a  31 

I am a beginner and I really don't know how to do this.我是初学者,我真的不知道该怎么做。 Thanks for your help!谢谢你的帮助!

Could you please try following, I am trying it in a different way for provided samples.您能否尝试以下操作,我正在以不同的方式尝试提供的样本。 Written and tested with provided samples only.仅使用提供的样本进行编写和测试。

For getting values before BULK SPACE try:要在 BULK SPACE 之前获取值,请尝试:

awk '
{
  line=$0
  while(match($0,/[[:space:]]+/)){
    arr=arr>RLENGTH?arr:RLENGTH
    start[arr]+=RSTART+prev_start
    prev_start=RSTART
    $0=substr($0,RSTART+RLENGTH)
  }
  var=substr(line,1,start[arr]-1)
  sub(/ +$/,"",var)
  print var
  delete start
  var=arr=""
}
'  Input_file

Output will be as follows.输出如下。

22  30  31  3a  31  32  3a  32
30  32  30  20  32  32  3a  31


For getting values after BULK SPACE try:要在 BULK SPACE 之后获取值,请尝试:

awk '
{
  line=$0
  while(match($0,/[[:space:]]+/)){
    arr=arr>RLENGTH?arr:RLENGTH
    start[arr]+=RSTART+prev_start
    prev_start=RSTART
    $0=substr($0,RSTART+RLENGTH)
  }
  var=substr(line,start[arr])
  sub(/^ +/,"",var)
  print var
  delete start
  var=arr=""
}
'  Input_file

Output will be as follows:输出如下:

" 0 9 : 1 2 : 2
1 2 7   2 2 : 1

You can try你可以试试

awk '{for(i=1;i<=NF;++i)if(length($i)==1)printf("%s ", $i);print("")}'

For each field, check the length and print it if it's desired.对于每个字段,检查长度并在需要时打印它。 You may pass the -F option to awk if it's not separated by blanks.如果没有空格分隔,您可以将-F选项传递给awk

The awk script is expanded as: awk脚本扩展为:

for( i = 1; i <= NF; ++i )
  if( length( $i ) == 1 )
    printf( "%s ", $i );
print( "" );

The print outside loop is to print a newline after each input line. print外循环是在每个输入行之后打印一个换行符。

Assuming all the columns are tab-separated (So you can have a space as a column value like the second line of your sample), easy to do with a perl one-liner:假设所有列都以制表符分隔(因此您可以将空格作为列值,例如示例的第二行),使用 perl 单行很容易:

$ perl -F"\t" -lane 'BEGIN { $, = "\t" } print grep { /^.$/ } @F' foo.txt
"       0       9       :       1       2       :       2
1       2       7               2       2       :       1
$ perl -F"\t" -lane 'BEGIN { $, = "\t" } print grep { /^..$/ } @F' foo.txt
22      30      31      3a      31      32      3a      32
30      32      30      20      32      32      3a      31

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

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