Extract certain sections of a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract certain sections of a line
# 1  
Old 11-20-2013
Extract certain sections of a line

I have a log that looks like below
Code:
sc.mng_10_Err.20131020_000000.log:NCSSC_MNG_UP_PE_TO_BE :  Failed to change dvc_trx_sts from PE to BE for srvtrx: 213323141427349 dvcsfx: 1
sc.mng_4_Err.20131020_000000.log:NCSSC_MNG_UP_PE_TO_BE :  Failed to change dvc_trx_sts from PE to BE for srvtrx: 213323183703756 dvcsfx: 1

I want to create a file to extract the sc.mng # , srvtrx#, and the dvcsfx# and print them like the following:
Code:
<srvtrx_no> <sc.mng_No> <dvcsfx_No>

Code:
213323141427349 10 1
213323183703756 4 1

# 2  
Old 11-20-2013
Code:
awk -F'[_ ]' '{print $(NF-2), $2, $(NF)}' myFile

# 3  
Old 11-20-2013
Try :

Code:
$ cat file
sc.mng_10_Err.20131020_000000.log:NCSSC_MNG_UP_PE_TO_BE :  Failed to change dvc_trx_sts from PE to BE for srvtrx: 213323141427349 dvcsfx: 1
sc.mng_4_Err.20131020_000000.log:NCSSC_MNG_UP_PE_TO_BE :  Failed to change dvc_trx_sts from PE to BE for srvtrx: 213323183703756 dvcsfx: 1

Code:
$  awk '{gsub("[[:alpha:]]|[[:punct:]]|Err.*srvtrx:|dvcsfx:",x);$0 =  $2 FS $1 FS $3}1' file

Code:
213323141427349 10 1
213323183703756 4 1


Last edited by Akshay Hegde; 11-20-2013 at 01:45 PM..
# 4  
Old 11-20-2013
Another approach using match function:
Code:
awk '
        {
                match ( $0, /srvtrx:[ ][^ ]*/ )
                srvtrx_no = sprintf ( "%s", substr ( $0, RSTART + 8, RLENGTH - 8 ) )
                match ( $0, /dvcsfx:[ ][^ ]*/ )
                dvcsfx_no = sprintf ( "%s", substr ( $0, RSTART + 8, RLENGTH - 8 ) )
                match ( $0, /sc[.]mng_[0-9]*/ )
                sc_mng_no = sprintf ( "%s", substr ( $0, RSTART + 7, RLENGTH - 7 ) )

                print srvtrx_no, sc_mng_no, dvcsfx_no
        }
' file

# 5  
Old 11-20-2013
Code:
awk '{match($0,/sc\.mng_(.*?)_Err.*srvtrx: (.*) dvcsfx: (.*?)/,a);print a[2]" "a[1]" "a[3]}' file

213323141427349 10 1
213323183703756 4 1

This User Gave Thanks to in2nix4life For This Post:
# 6  
Old 11-20-2013
Thanks for all your feedback. Here is the outcomes of all your sugestions
didnt work
Code:
awk -F'[_ ]' '{print $(NF-2), $2, $(NF)}' myFile

output:
Code:
dvcsfx: 5
dvcsfx: 8

This works
Code:
awk '{gsub("[[:alpha:]]|[[:punct:]]|Err.*srvtrx:|dvcsfx:",x);$0 =  $2 FS $1 FS $3}1' file

Output:
Code:
213323141427363 5 1
213323183703729 8 1

did not work
Code:
awk '
        {
                match ( $0, /srvtrx:[ ][^ ]*/ )
                srvtrx_no = sprintf ( "%s", substr ( $0, RSTART + 8, RLENGTH - 8 ) )
                match ( $0, /dvcsfx:[ ][^ ]*/ )
                dvcsfx_no = sprintf ( "%s", substr ( $0, RSTART + 8, RLENGTH - 8 ) )
                match ( $0, /sc[.]mng_[0-9]*/ )
                sc_mng_no = sprintf ( "%s", substr ( $0, RSTART + 7, RLENGTH - 7 ) )

                print srvtrx_no, sc_mng_no, dvcsfx_no
        }
' file

Output:
Code:
 Syntax Error The source line is 1.
 The error context is
                {match ( $0, /srvtrx:[ ][^ ]*/ )srvtrx_no >>>  = <<<  sprintf ( "%s", substr ( $0, RSTART + 8, RLENGTH - 8 ) ) match ( $0, /dvcsfx:[ ][^            ]*/ ) dvcsfx_no = sprintf ( "%s", substr ( $0, RSTART + 8, RLENGTH - 8 ) ) match ( $0, /sc[.]mng_[0-9]*/ ) sc_mng_no = sprintf ( "%s", substr ( $0, RSTAR           T + 7, RLENGTH - 7 ) ) print srvtrx_no, sc_mng_no, dvcsfx_no}
 awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.

did not work
Code:
awk '{match($0,/sc\.mng_(.*?)_Err.*srvtrx: (.*) dvcsfx: (.*?)/,a);print a[2]" "a[1]" "a[3]}' file

Output:
Code:
 Syntax Error The source line is 1.
 The error context is
                {match($0,/sc\.mng_(.*?)_Err.*srvtrx: (.*) dvcsfx: >>>  (.*?)/, <<<
 awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.

# 7  
Old 11-20-2013
strange... on your sample file:
Code:
sc.mng_10_Err.20131020_000000.log:NCSSC_MNG_UP_PE_TO_BE :  Failed to change dvc_trx_sts from PE to BE for srvtrx: 213323141427349 dvcsfx: 1
sc.mng_4_Err.20131020_000000.log:NCSSC_MNG_UP_PE_TO_BE :  Failed to change dvc_trx_sts from PE to BE for srvtrx: 213323183703756 dvcsfx: 1

running awk -F'[_ ]' '{print $(NF-2), $2, $(NF)}' mySampleFile I get:
Code:
213323141427349 10 1
213323183703756 4 1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl to extract information from a file line by line

In the below perl code I am using tags within each line to extract certain information. The tags that are used are: STB >0.8 is STRAND BIAS otherwise GOOD FDP is the second number GO towards the end of the line is read into an array and the value returned is outputed, in the first line that... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

Remove sections based on duplicate first line

Hi, I have a file with many sections in it. Each section is separated by a blank line. The first line of each section would determine if the section is duplicate or not. if the section is duplicate then remove the entire section from the file. below is the example of input and output.... (5 Replies)
Discussion started by: ahmedwaseem2000
5 Replies

3. Shell Programming and Scripting

HELP: Shell Script to read a Log file line by line and extract Info based on KEYWORDS matching

I have a LOG file which looks like this Import started at: Mon Jul 23 02:13:01 EDT 2012 Initialization completed in 2.146 seconds. -------------------------------------------------------------------------------- -- Import summary for Import item: PolicyInformation... (8 Replies)
Discussion started by: biztank
8 Replies

4. 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

5. Shell Programming and Scripting

Extract Line and Column from CSV Line in ksh or bash format

Hi, I was doing some research and can't seem to find anything. I'm trying to automate a process by creating a script to read a csv line and column and assigning that value to a variable for the script to process it. Also if you could tell me the line and column if it's on another work ... (3 Replies)
Discussion started by: vpundit
3 Replies

6. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

7. Programming

extract different sections of a file

Hi All, I have a file with the data 10;20;30;40;50;60;70;80;123;145;156;345. the output i want is the first fourth sixth elements and everything from there on. How do i achieve this. (1 Reply)
Discussion started by: raghu_shekar
1 Replies

8. Shell Programming and Scripting

Modify sections of the line in a file

Hello.. I have a line in a file which I have to edit: the line looks like: <!]> Sometimes, the section of the line can have only one entry for cn, or maybe more than 2 like below: <!]> I have a variable which has the following value: CN="(cn=MNO)(cn=XYZ)" I need to replace the part... (4 Replies)
Discussion started by: chiru_h
4 Replies

9. Shell Programming and Scripting

extract multiple sections of file

I have a file that I need to parse multiple sections from the file. The file contains multiple lines that start with ST (Abunch of data) Then the file contains multiple lines that start with SE (Abunch of data) SE*30*0001 ST*810*0002 I need all of the lines between and including these.... (6 Replies)
Discussion started by: rgentis
6 Replies

10. UNIX for Advanced & Expert Users

extract multiple sections of a file

I have a file that I need to parse multiple sections from the file. The file contains multiple lines that start with ST (Abunch of data) Then the file contains multiple lines that start with SE (Abunch of data) SE*30*0001 ... (1 Reply)
Discussion started by: rgentis
1 Replies
Login or Register to Ask a Question