I have a file that has a lot of junk and special characters as well. I want to keep a particular alphanumeric pattern and ignore everything else - e.g AB123456789 - I want to extract only this keyword i.e. two alphabets 'AB' followed by 9 numbers.


[{"u_affected_cis":"m324nkj43nkj3n4kj34n","number":"hhggjjiiijjjf","akdsfj_skdfj":"","as_group":"1,324kj3k4j3k4jk34","order":"","__status":"success","phase":"gfhgh","cmdb_ci":"0989iujlkj","u_benefit_organization":"","u_creating_group":"luiy98798yukuh","work_notes_list":"","priority":"4","u_tier4_location":"","review_date":"","u_mf_batch_inst_opdoc_move":"","u_requesting_group":"kjhljlkjhlkuh098709kjh","business_duration":"","number":"AB123456789","requested_by":tgfgtf878789khgo7869876ff9007da158c","u_temp","change_plan":"","asd_def":"2023-02-10 11:58:21","implementation_plan":"","short_description":"data","u_alternate_programmer_work_number":"","work_start":"","u_assignment_group_updated":"","yy_uhggfjk":"","fds":"change_request","closed_by":"abcdef","start_date":"2023-02-10"}]



  • 1
    Some sample input & output would go a long ways towards getting a good answer. – Jeff Schaller Feb 10 '17 at 16:07
  • Added sample input and output. – Koshur Feb 10 '17 at 17:15
  • 2
    If your actual input is valid JSON, you would be better served by a JSON-aware tool such as jq, e.g. jq -r '.[0].number'. – dhag Feb 10 '17 at 17:23
  • 1
    I would first fix what ever created this, as it is not valid json (missing " before tgfgtf878789khgo…, u_temp has no value), then use @dhag's solution. And I thing dhag should make the comment into an answer. – ctrl-alt-delor Feb 10 '17 at 17:42

If your actual input is valid JSON, you would be better served by a JSON-aware tool, such as jq:

jq -r '.[0].number'.

(I say "if", because the input you posted is not valid JSON as it is missing a double quote and one of the keys has no value attached to it; I assume that the breakage may have happened as you were preparing the question.)


Some sed should do the job:

sed -e '/AB[0-9]\{9\}/!d' -e 's/.*\(AB[0-9]\{9\}\).*/\1/'

If your file has always the same number of fields and your pattern appears at the same location (eg: column 72) you may use a simple awk:

awk -F "\"" '{print $72}' input-file.txt

It appears that a pattern match is not suitable for you because you have the same pattern (AF123456789) at the beginning of the file.

I hope this answer helps you.


I've created these files to replicate a smaller scale of what you're doing:

┌─[root@Fedora]─[~/stack_exchange]─[03:38 pm]
└─[$]› ls
1234fnjfck   CA123456789      EA123456789  HA123456789  KA123456789   NA123456789  QA123456789  TA123456789              VA123456789  YA123456789
AA123456789  DA123456789      FA123456789  IA123456789  LA123456789  OA123456789  RA123456789  testing-please-delete-me  WA123456789  ZA123456789
BA123456789  DELETE1234  GA123456789  JA123456789  MA123456789  PA123456789  SA123456789  UA123456789              XA123456789

a regex variable matching the pattern will be able to handle pulling the desired files that do not match the pattern in a for in loop, with an if statement:

┌─[root@Fedora]─[~/stack_exchange]─[04:07 pm]
└─[$]› pattern="^[A-Z][A-Z][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$"

[$]› for i in $(ls ~/stack_exchange); do if ! [[ $i =~ $pattern ]]; then echo "$i does not match!"; fi; done
1234fnjfck does not match!
DELETE1234 does not match!
testing-please-delete-me does not match!

So to delete them:

[$]› for i in $(ls ~/stack_exchange); do if ! [[ $i =~ $pattern ]]; then rm -f $i; fi; done


[$]› ls
AA123456789  CA123456789  EA123456789  GA123456789  IA123456789  KA123456789  MA123456789  OA123456789  QA123456789  SA123456789  UA123456789  WA123456789  YA123456789
BA123456789  DA123456789  FA123456789  HA123456789  JA123456789  LA123456789  NA123456789  PA123456789  RA123456789  TA123456789  VA123456789  XA123456789  ZA123456789
  • Deleted the file itself.. – Koshur Feb 10 '17 at 17:02
  • 1
    There is nothing about deleting files in the question, so rm is the wrong way to go. Also nothing about filenames. – ctrl-alt-delor Feb 10 '17 at 17:44
  • sorry, I assumed @Koshur wanted to remove them due to the title "How can I delete everything but an alphanumeric pattern?" - also the question was different at the time of answering. – RobotJohnny Feb 13 '17 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.