pattern matching

Hi,

any idea how to extract alphabets alone in the string which contains numbers and alphabets. Thanks in advance!

Post sample data and desired output.

you may try something like this

echo "aB12Cd"  | tr -dc '[a-zA-Z]'
1 Like

Here is the sample input: 1234567890 abc 1234
Alphabets inside the string could be of any length and i have some 70+ different patterns ( like abc or abc- or abc . ) in another file. I just want to check whether it matches any of those patterns. If so, i would like to extract the things before the pattern and after the pattern.

Thanks in advance!

So you just want to remove digits from the input file? If so, try this:

sed 's/[0-9]//g' infile > outfile
1 Like

Thanks... But i would like to know how to extract the numbers which are present before the pattern and after the pattern.

echo "1234567890 abc 1234" | awk -F "[a-zA-Z]*" '{print $1 $2}'

if u have the character in a file then

awk -F "[a-zA-Z]*" '{print $1 $2}' sample.txt

where $1 will be before the pattern and $2 after the pattern

1 Like

Assuming "abc" is your pattern:

perl -nle 'print "$1 $2" if /(\d+) abc (\d+)/' file
1 Like

Thanks, Shipra and bartus11.
One more query. There could be any alphabets inside the string. How to get alphabets alone.
For example, 123456 abc 3446. I just want to get those alphabets present inside string.

Thanks...

if you have input as number string number then u can do the following to get the 3 seperately

to get pre/postfix

awk -F " " '{print $1 $3}' sample 

to get the string

awk -F " " '{print $2}' sample 

Thanks Shipra.

I think your logic is assuming that there could be spaces before and after the alphabets. But the issue here is there are spaces in between numbers too. Sometimes no spaces in the string. I just want to get the all the alphabets in the string. Is it possible...

Try:

perl -ln0e '$,="\n";print /[a-zA-Z]+/g' file