简体   繁体   中英

Found string with regex in AWK or SED

I want to find for example the following string 9Stest1.test2D9 in File. Then i want to cut the first 2 charechter and last 2 charechter and finally print the text befor and after . in two seperate Line.

Example Text :

7U8vTest(#G-HLjYM6QqJj1j"7MFx$^Qd
.f@alU|A#Z<inCWV6a=L?o`A5vIod"%Mm+YW1RM@,L;aN
r^n<&)}[??!VcVIV**9Stest1.test2D9**94EN~yK,$lU=9?UT.[
e`)G:FS.nGz%?@~k!20aLJ^PU-[@}0W\ !8x
cujOmEK"1;!cI134lu%0-A +/t!VIf?8uT`!
aC1QAQY>4RE$46iVjAE^eo5yR|
1?/T?<H5,%G~[|9I/c&8MY$O]%,UYQe{!{Bm[rRC[
aHC`<m?BUau@N_O>Yct.MXo[>r5^uV&26@MkYB'Kiu\Y
K(*}ldO:ZQnI8t989fi+

Output should be so :

test1
test2

I have tried with following code grep "[0-9][a-zA-Z]\\+\\.[a-zA-Z]\\+[0-9]" to find the string.Now i can cut the firs two line with cut command but last two line? I think with AWK can i solve my problem easily but i dont know how. Thanks

see this line if it helps:

kent$  grep -Po '(?<=9S).*?(?=D9)' file
test1.test2

or more dynamic, this works for your example:

kent$  grep -Po '(?<=\d[a-zA-Z]).*\..*(?=[a-zA-Z]\d)' file
test1.test2

EDIT

as @devnull suggested, to get the desired output, you could

grep -Po '(?<=\d[a-zA-Z]).*\..*(?=[a-zA-Z]\d)' file|tr '.' '\n'

This depends on how and what to search for. Like text between **

awk -F"**"  '{a=substr($2,3,length($2)-4)} a {print a}'
test1.test2
dO:ZQnI8t989f

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