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
# 8  
Old 05-18-2017
In sed you need to mark the wanted 3 chars in a \( \) and restore them via a back reference.
Code:
sed -n '/{1:F/ s/.*{2:I\(...\).*/\1/p'

# 9  
Old 05-18-2017
Quote:
Originally Posted by bakunin
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

Hi I tried this command like below
Code:
document_type=`sed -n '/^.\{30\}{1:F/ s/^\(.\{30\}{2:I\)\(...\).*/\2/p;' $eachfile`

eachfile was the file which has the file name path. '30' was the position of the required value tag {2:I. This gave me empty value in the document type field. I am not sure the value in command i entered correct or not.

Thanks for your reply.

---------- Post updated at 06:30 PM ---------- Previous update was at 06:28 PM ----------

Quote:
Originally Posted by Aia
Code:
 perl -nle '/\{2:I(\d{3})/ and print $1' mad_man.example

Hi,

I tried this command as like below it gave me the exact value '103' i was looking for.

Code:
document_type=`perl -nle '/\{2:I(\d{3})/ and print $1' $eachfile | sort | uniq | sed -e "s/^[ ]*//g" | sed -e "s/[ ]*$//g`

Thanks.


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!


---------- Post updated at 07:14 PM ---------- Previous update was at 06:30 PM ----------

Quote:
Originally Posted by RudiC
Try also
Code:
awk 'match ($0, /{1:F.*2:I.../) {print substr ($0, RSTART+RLENGTH-3,3)}' file
103
103
103

Hi,

I tried this command as like below it gave me the exact value '103' i was looking for.


Code:
Code:
document_type=`awk 'match ($0, /{1:F.*2:I.../) {print substr ($0, RSTART+RLENGTH-3,3)}' $eachfile | sort | uniq | sed -e "s/^[ ]*//g" | sed -e "s/[ ]*$//g`

Thanks.

Last edited by RudiC; 05-18-2017 at 10:02 AM.. Reason: Added CODE tags.
# 10  
Old 05-18-2017
Why the loooong pipe? If you want to remove leading spaces, do so in awk. If you want to remove trailing spaces, do so in awk. If you want unique values, do so in awk.
# 11  
Old 05-18-2017
Quote:
Originally Posted by RavinderSingh13
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

Hi

I tried both of these commands i got the value '103' required.

Thanks.

---------- Post updated at 07:26 PM ---------- Previous update was at 07:21 PM ----------

Hi All,

Thanks for your help. I got the solutions.

Thanks a ton.
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