简体   繁体   English

使用awk排除字符

[英]Exclude characters using awk

I am trying to find a way to exclude numbers on a file when I cat ti but I only want to exclude the numbers on print $1 and I want to keep the number that is in front of the word. 我正在尝试找到一种方法在排除ti时排除文件中的数字,但我只想排除打印$ 1上的数字,并且我想保留单词前面的数字。 I have something that I thought might might work but is not quite giving me what I want. 我有一些我认为可能会起作用的东西,但并没有完全满足我的需求。 I have also showed an example of what the file looks like.The file is separated by pipes. 我还展示了一个文件外观示例,该文件由管道分隔。

cat files  | awk -F '|' ' {print $1 "\t" $2}' |sed 's/0123456789//g'

input: 输入:

b1ark45 | dog23 | brown
m2eow66| cat24 |yellow
h3iss67 | snake57 | green

Output 输出量

 b1ark dog23    
 m2eow cat24    
 h3iss nake57

try this: 尝试这个:

awk -F'|' -v OFS='|' '{gsub(/[0-9]/,"",$1)}7'  file

the output of your example would be: 您的示例的输出为:

bark | dog23 | brown    
meow| cat24 |yellow    
hiss | snake57 | green

EDIT 编辑

this outputs col1 (without ending numbers and spaces) and col2, separated by <tab> 这将输出col1(没有结尾数字和空格)和col2,以<tab>分隔

kent$  echo "b1ark45 | dog23 | brown
m2eow66| cat24 |yellow
h3iss67 | snake57 | green"|awk -F'|' -v OFS='\t' '{gsub(/[0-9]*\s*$/,"",$1);print $1,$2}'
b1ark    dog23 
m2eow    cat24 
h3iss    snake57

这可能对您有用(GNU sed):

sed -r 's/[0-9]*\s*\|\s*(\S*).*/ \1/' file

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

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