Taking out part of a string by matching a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Taking out part of a string by matching a pattern
# 1  
Old 04-26-2018
Taking out part of a string by matching a pattern

Hi All,

My Problem is like below.

I have a file which contains just one row and contains data like
Code:
PO_CREATE12457888888888889SK1234567878744551111111111SK89456321145789955455555SK8888888815788852222

i want to extract SK12345678
Code:
SK89456321
SK88888888

So basically SK and next 8 characters.
can someone please help.

Last edited by vgersh99; 04-26-2018 at 12:15 PM.. Reason: code tags, please!
# 2  
Old 04-26-2018
Quote:
Originally Posted by Asfakul Islam
Hi All,

My Problem is like below.

I have a file which contains just one row and contains data like
Code:
PO_CREATE12457888888888889SK1234567878744551111111111SK89456321145789955455555SK8888888815788852222

i want to extract SK12345678
Code:
SK89456321
SK88888888

So basically SK and next 8 characters.
can someone please help.
I am not quite sure what the desired output is given your sample input.
What does this mean?
Code:
 i want to extract SK12345678

Furthermore, indicate your OS and shell type.

---------- Post updated at 12:03 PM ---------- Previous update was at 11:18 AM ----------

if you have gawk available:
Code:
echo 'PO_CREATE12457888888888889SK1234567878744551111111111SK89456321145789955455555SK8888888815788852222' | gawk -v FPAT='SK[0-9]{8}' 'NF{for(i=1;i<=NF;i++) print $i}'
SK12345678
SK89456321
SK88888888


Last edited by vgersh99; 04-26-2018 at 12:36 PM..
# 3  
Old 04-26-2018
Code:
$ 
$ echo PO_CREATE12457888888888889SK1234567878744551111111111SK89456321145789955455555SK8888888815788852222 | perl -lne 'while (/(SK.{8})/g){print $1}'
SK12345678
SK89456321
SK88888888
$ 
$

# 4  
Old 04-26-2018
You could also use the -o option to egrep

Code:
$  echo PO_CREATE12457888888888889SK1234567878744551111111111SK89456321145789955455555SK8888888815788852222 | egrep -o "SK.{8}"
SK12345678
SK89456321
SK88888888

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 04-27-2018
Does
Code:
sed 's/SK........//g' inputfile >outputfile

work?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to 'improve' this script and also 'fix' the pattern matching part?

Hi all, Below is my script. It is currently working but I want some advice on maybe improving it and need some help on the pattern matching xx.ksh: #!/bin/ksh # # ------------------------------------------------------------------------------------------------- # #Fatal NI connect error... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. UNIX for Dummies Questions & Answers

String pattern matching and position

I am not an expert with linux, but following various posts on this forum, I have been trying to write a script to match pattern of charters occurring together in a file. My file has approximately 200 million characters (upper and lower case), with about 50 characters per line. I have merged all... (5 Replies)
Discussion started by: biowizz
5 Replies

3. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

4. UNIX for Dummies Questions & Answers

Extracting sub-string matching the pattern.

Hi, I have a string looks like the following: USERS 32767.9844 UNDOTBS1 32767.9844 SYSAUX 32767.9844 SYSTEM 32767.9844 EMS 8192 EMS 8192 EMS_INDEXES 4096 EMS_INDEXES 4096 8 rows selected. How do I extract a sub-string to get the expected output as following: EMS 8192 EMS_INDEXES 4096 ... (3 Replies)
Discussion started by: NetBear
3 Replies

5. Shell Programming and Scripting

Get matching string pattern from a file

Hi, file -> temp.txt cat temp.txt /home/pradeep/123/a_asp.html /home/pradeep/123/a_asp1.html /home/pradeep/435/a_asp2.html /home/pradeep/arun/abc/a_dfr.html /home/pradeep/arun/123/a_kir.html /home/pradeep/123/arun/a_dir.html .... .... .. i need to get a_*.html(bolded strings... (4 Replies)
Discussion started by: pradebban
4 Replies

6. Shell Programming and Scripting

Matching part of a string that is in an array

I am trying to do a comparison to see if these two string arrays match. I have tried assigning the array variable to another variable to get it to work but i can't figure it out. Here is what I have. #example of items that could be in the array #string="TEST"... (3 Replies)
Discussion started by: zatarra777
3 Replies

7. Shell Programming and Scripting

taking a part of string from a given path

Hi I have two path as follows system/console/bin/code/sample/testfile.txt system/console/bin/database/files/new/dbfile.txt I need the output as code/sample in first case database/files/new in second case That is I am omitting system/console/bin and the filename(s) in both... (2 Replies)
Discussion started by: ssuresh1999
2 Replies

8. Shell Programming and Scripting

Problem extracting just a part of a matching pattern

Hello everyone, this is my first post so please give me a hand. I apologize for my English, I'll try to be clear with my request. I need to write a script (Bash) which finds all the variables defined in the file .h of the folder and then writes the name of the files .c where these variables are... (1 Reply)
Discussion started by: paxilpaz
1 Replies

9. Shell Programming and Scripting

how to get the last part of a string followed by a pattern

assuming "cat" is the pattern, string (regardless length) asdadfcat4 I need to get 4 for eirtrjkkkcat678- I'd get 678 (in b-shell) Thanks in advance!!! (4 Replies)
Discussion started by: bluemoon1
4 Replies

10. UNIX for Dummies Questions & Answers

vi part-pattern matching and deletion

Hi, I have a log file which shows the files which has been changed over the last week. They follow this pattern: old_file_version_number@@new_file_version_number Now I need to know how to delete from each line parts starting from @@. I would be issuing the command inside vi(m). So... (5 Replies)
Discussion started by: vino
5 Replies
Login or Register to Ask a Question