Extract some characters from lines based on pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Extract some characters from lines based on pattern
# 1  
Old 05-17-2017
Extract some characters from lines based on pattern

Hi All,

i would like to get some help regarding extracting certain characters from a line grepped.

Code:
blahblah{1:F01IRVTUS30XXXX0000000001}{2:I103IRVTDEF0XXXXN}{4:blah
blahblah{1:F01IRVTUS30XXXX0000000001}{2:I103IRVTDEF0XXXXN}{4:blah
blahblah{1:F01IRVTUS30XXXX0000000001}{2:I103IRVTDEF0XXXXN}{4:blah

In the above snap shot i am going to grep for tag '{1:F' and i wanted to get the 3 characters after tag '{2:I' the value '103'.

The tags '{1:F' & '{2:I' are fixed so based on this i wanted to get the 3 chars coming after '{2:I'. So my motivation is to get 3 chars after tag '{2:I'.

Either sed or awk wil be better.

I tried something like
Code:
 awk '/{2:I/{print $NF}'

Code:
sed -n -e '/{1:F/ s/.*\{2:I *//p'

But not fruitful.

Note : I am using AIX 6.0 version UNIX.

Thanks in advance
# 2  
Old 05-17-2017
Is the format of the lines fixed? (That is, will "{1:F" and "{2:I" always appear at a certain column?) If so, you won't even need to search for it.

First, you won't need grep because sed can do everything grep can do too:

Code:
sed -n '/{1:F/p' /your/file

If you need "{1:F" to appear on a certain column, modify the line like this:

Code:
sed -n '/^.\{xx\}{1:F/p' /your/file

where the red "xx" is a number representing the number of characters befor the search string.

In the same way you output the characters in question via a so-called "back-reference":

Code:
sed -n '/^.\{xx\}{1:F/ s/^\(.\{yy\}{2:I\)\(...\).*/\2/p;' /your/file

Again, enter sensible integer values for the "xx" and "yy" marked red.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 05-17-2017
Hi,

Thanks for your reply. Actually the tags i mentioned are fixed in position but nobody knows when they might change the position for future requirements. So i want to fetch based on the {2:I tag only.

Thanks.
# 4  
Old 05-17-2017
Code:
 perl -nle '/\{2:I(\d{3})/ and print $1' mad_man.example

This User Gave Thanks to Aia For This Post:
# 5  
Old 05-17-2017
Hi,

I am going to try both the solutions tomorrow and will let you know the results.

Thanks for the reply.
# 6  
Old 05-17-2017
Try also
Code:
awk 'match ($0, /{1:F.*2:I.../) {print substr ($0, RSTART+RLENGTH-3,3)}' file
103
103
103

This User Gave Thanks to RudiC For This Post:
# 7  
Old 05-17-2017
Hello mad_man,

Could you please try following and let me know if this helps you.
Solution 1st:
Code:
awk '/1:F.*2:I/{sub(/.*2:I/,"");print substr($0,1,3)}'   Input_file

Solution 2nd:
Code:
awk '/1:F.*2:I/{print substr($0,index($0,"2:I")+3,3)}'   Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 05-17-2017 at 05:52 PM..
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract file name based on the pattern

Hello All, I have multiple files in a hadoop /tmp/cloudera directory. Filename are as follows ABC_DATA_BAD5A_RO_F_20161104.CSV ABC_DATA_BAD6C_VR_F_20161202.CSV ABC_DATA_BAD7A_TR_F_20162104.CSV ABC_DATA_BAD2A_BR_F_20161803.CSV ABC_DATA_BAD3T_KT_F_20160106.CSV I just need filenames... (6 Replies)
Discussion started by: prajaktaraut
6 Replies

2. Shell Programming and Scripting

Extract date from files based on file pattern

I want to extract dates from the files and i have different types of files with pattern. I have list file with the patterns and want to date extract based on it in a sh script Files in the directory : file1_20160101.txt file2_20160101_abc.txt filexyz20160101.txt list file with... (2 Replies)
Discussion started by: lijjumathew
2 Replies

3. Shell Programming and Scripting

Precede and Append characters using sed/awk based on a pattern

I have an input file which is similar to what I have shown below. Pattern : Data followed by two blank lines followed by data again followed by two blank lines followed by data again etc.. The first three lines after every blank line combination(2 blank lines between data) should be... (2 Replies)
Discussion started by: bikerboy
2 Replies

4. Shell Programming and Scripting

Extract lines that match a pattern

Hi all, I got a file that contains the following content, Actually it is a part of the file content, Installing XYZ XYZA Image, API 18, revision 2 Unzipping XYZ XYZA Image, API 18, revision 2 (1%) Unzipping XYZ XYZA Image, API 18, revision 2 (96%) Unzipping XYZ XYZA Image, API 18,... (7 Replies)
Discussion started by: Kashyap
7 Replies

5. Shell Programming and Scripting

Search for a pattern,extract value(s) from next line, extract lines having those extracted value(s)

I have hundreds of files to process. In each file I need to look for a pattern then extract value(s) from next line and then search for value(s) selected from point (2) in the same file at a specific position. HEADER ELECTRON TRANSPORT 18-MAR-98 1A7V TITLE CYTOCHROME... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

6. Shell Programming and Scripting

Sed or awk : pattern selection based on special characters

Hello All, I am here again scratching my head on pattern selection with special characters. I have a large file having around 200 entries and i have to select a single line based on a pattern. I am able to do that: Code: cat mytest.txt | awk -F: '/myregex/ { print $2}' ... (6 Replies)
Discussion started by: usha rao
6 Replies

7. Shell Programming and Scripting

Awk to extract lines with a defined number of characters

This is my problem, my file (file A) contains the following information: Now, I would like to create a file (file B) containing only the lines with 10 or more characters but less than 20 with their corresponding ID: Then, I need to compare the entries and determine their frequency. Thus, I... (7 Replies)
Discussion started by: Xterra
7 Replies

8. Shell Programming and Scripting

extract based on pattern

I have a mail log file and I want to extract some lines belonging to one domain. For example Input File: Dec 12 03:15:28 postfix/smtpd: 3F481EB0295: client=unknown, sasl_method=PLAIN, sasl_username=abcd@xyz.com Dec 12 03:22:08 postfix/smtpd: 60B56EE001D: client=5ad9b9ba.com,... (7 Replies)
Discussion started by: Bijayant Kumar
7 Replies

9. Shell Programming and Scripting

extract lines based on few conditions

Hi, I need to extract lines based on some conditions as explained below: File format details: notes: 1. each set starts with AAA only 2. number of columns is fixed 3. number of rows per set may vary (as one set is upto DDD - 4 rows) Now, if any BBB's 5th column is blank then then... (4 Replies)
Discussion started by: prvnrk
4 Replies

10. HP-UX

extract field of characters after a specific pattern - using UNIX shell script

Hello, Below is my input file's content ( in HP-UX platform ): ABCD120672-B21 1 ABCD142257-002 1 ABCD142257-003 1 ABCD142257-006 1 From the above, I just want to get the field of 13 characters that comes after 'ABCD' i.e '120672-B21'... . Could... (2 Replies)
Discussion started by: jansat
2 Replies
Login or Register to Ask a Question