简体   繁体   中英

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. 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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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