Visit Our UNIX and Linux User Community


Print between multiple patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print between multiple patterns
# 8  
Old 09-11-2013
Why then don't you post a representative sample file in the first place?
# 9  
Old 09-11-2013
Quote:
Originally Posted by disedorgue
Hi,
Sed version:
Code:
sed 's/^.*[^"]\("*user id[^;]*;\)/ \1&/i;s/^.*[^"]\("*datasource name[^;]*\)/\1&/i;s/\([^;]*\);.*/\1/' file

-replace line by space + field user id + semicolon + line
and
-replace line by field datasource + line
and
-replace line by field newly build.

s/.../.../i for case insensitive
Regards.
Getting "cann't be parsed error"
# 10  
Old 09-11-2013
Work fine at home with input example (gnu sed 4.2.1):
Code:
$ cat dir_path
Dir Path2
Password="pwd2"; User id="uid2"; Connection pool="somename2"; "datasource dame"="DS name2";some other fields.
Dir Path 1
Connection pool="somename"; "DataSource Name"="DS name"; Password="pwd"; User Id="uid";some other fields
$ sed 's/^.*[^"]\("*user id[^;]*;\)/ \1&/i;s/^.*[^"]\("*datasource name[^;]*\)/\1&/i;s/\([^;]*\);.*/\1/' dir_path
Dir Path2
 User id="uid2"
Dir Path 1
"DataSource Name"="DS name" User Id="uid"

Regards.
# 11  
Old 09-11-2013
Quote:
Originally Posted by disedorgue
Work fine at home with input example (gnu sed 4.2.1):
Code:
$ cat dir_path
Dir Path2
Password="pwd2"; User id="uid2"; Connection pool="somename2"; "datasource dame"="DS name2";some other fields.
Dir Path 1
Connection pool="somename"; "DataSource Name"="DS name"; Password="pwd"; User Id="uid";some other fields
$ sed 's/^.*[^"]\("*user id[^;]*;\)/ \1&/i;s/^.*[^"]\("*datasource name[^;]*\)/\1&/i;s/\([^;]*\);.*/\1/' dir_path
Dir Path2
 User id="uid2"
Dir Path 1
"DataSource Name"="DS name" User Id="uid"

Regards.
Thanks, yes u r right but if the line is like this

---------- D:\ABC5\XYZ\ONE.CONFIG
<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="20"/>

it outputs as
---------- D:\ABC5\XYZ\ONE.CONFIG
"data source=127.0.0.1 <sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1

Reg
Venkat
# 12  
Old 09-11-2013
I've not same error:

File:
Code:
$ cat dir_path
Dir Path2
Password="pwd2"; User id="uid2"; Connection pool="somename2"; "datasource dame"="DS name2";some other fields.
Dir Path 1
Connection pool="somename"; "DataSource Name"="DS name"; Password="pwd"; User Id="uid";some other fields
---------- D:\ABC5\XYZ\ONE.CONFIG
<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="20"/>

sed with problem (last line is truncate):
Code:
$ sed 's/^.*[^"]\("*user id[^;]*;\)/ \1&/i;s/^.*[^"]\("*datasource name[^;]*\)/\1&/i;s/\([^;]*\);.*/\1/' dir_path
Dir Path2
 User id="uid2"
Dir Path 1
"DataSource Name"="DS name" User Id="uid"
---------- D:\ABC5\XYZ\ONE.CONFIG
<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1

sed with issue:
Code:
$ sed 's/^.*[^"]\("*user id[^;]*;\)/ \1&/i;s/^.*[^"]\("*datasource name[^;]*\)/\1&/i;s/\("*\(datasource\|user id\)[^;]*\);.*/\1/i' dir_path
Dir Path2
 User id="uid2"
Dir Path 1
"DataSource Name"="DS name" User Id="uid"
---------- D:\ABC5\XYZ\ONE.CONFIG
<sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="20"/>

Regards.
# 13  
Old 09-16-2013
Quote:
Originally Posted by Yoda
I guess this is a typo:
Code:
Dir Path2
Password="pwd2"; User id="uid2"; Connection pool="somename2"; "datasource same"="DS name2";some other fields.

This awk code should help:
Code:
awk '
        /Dir Path/ {
                print $0
        }
        !/Dir Path/ && NF {
                match ($0, /[Dd][Aa][Tt][Aa][Ss][Oo][Uu][Rr][Cc][Ee][ ][Nn][Aa][Mm][Ee][^;]*/ )
                nme = sprintf ( "%s", substr ( $0, RSTART, RLENGTH ) )
                match ($0, /[Uu][Ss][Ee][Rr][ ][Ii][Dd][^;]*/ )
                usr = sprintf ( "%s", substr ( $0, RSTART, RLENGTH ) )
                print nme, usr
        }
' file


Thanks, is there a way to delete duplicate lines(case sensitively) of the output

Reg
Venkat
# 14  
Old 09-16-2013
Quote:
Originally Posted by sirababu
Thanks, is there a way to delete duplicate lines(case sensitively) of the output
You can use an Associative Array to remove duplicates, but note that the order is not preserved in this code:
Code:
awk '
        /Dir Path/ {
                hdr = $0
        }
        !/Dir Path/ && NF {
                match ($0, /[Dd][Aa][Tt][Aa][Ss][Oo][Uu][Rr][Cc][Ee][ ][Nn][Aa][Mm][Ee][^;]*/ )
                nme = sprintf ( "%s", substr ( $0, RSTART, RLENGTH ) )
                match ($0, /[Uu][Ss][Ee][Rr][ ][Ii][Dd][^;]*/ )
                usr = sprintf ( "%s", substr ( $0, RSTART, RLENGTH ) )
                A[hdr RS nme OFS usr]
        }
        END {
                for ( k in A )
                        print k
        }
' file


Previous Thread | Next Thread
Test Your Knowledge in Computers #53
Difficulty: Easy
Computer caches are typically built using EPROMS.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print different multiple lines after two patterns?

Hello, I need to print some lines as explained below, TXT example 1111 2222 3333 4444 5555 6666 7777 8888 6666 9999 1111 2222 3333 4444 5555 (8 Replies)
Discussion started by: liuzhencc
8 Replies

2. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

3. Shell Programming and Scripting

Grep from multiple patterns multiple file multiple output

Hi, I want to grep multiple patterns from multiple files and save to multiple outputs. As of now its outputting all to the same file when I use this command. Input : 108 files to check for 390 patterns to check for. output I need to 108 files with the searched patterns. Xargs -I {} grep... (3 Replies)
Discussion started by: Diya123
3 Replies

4. Shell Programming and Scripting

How to print only lines in between patterns?

Hi, I want to print only lines (green-italic lines) in between first and last strings in column 9. there are different number of lines between each strings. 10 AUGUSTUS exon 4558 4669 . - . 10.g1 10 AUGUSTUS exon 8771 8889 . ... (6 Replies)
Discussion started by: jamo
6 Replies

5. Shell Programming and Scripting

Search for the two patterns and print everything in between

Hi all, I have a file having data: @HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTAATA NTTGGGTTTTCT @HWUSI-EAS1727:19:6:1:3674:984:0:1#GTTA... NTTGGGTTTTCT @HWUSI-EAS1727:19:6:1:3674:984:0:1#.....CT NTTGGGTTTTCT I want to print everything starting from # till line ends. can you please help me how... (5 Replies)
Discussion started by: pirates.genome
5 Replies

6. Shell Programming and Scripting

awk: Multiple search patterns & print in an one liner

I would like to print result of multiple search pattern invoked from an one liner. The code looks like this but won't work gawk -F '{{if ($0 ~ /pattern1/) pat1=$1 && if ($0 ~ /pattern2/) pat2=$2} ; print pat1, pat2}' Can anybody help getting the right code? (10 Replies)
Discussion started by: sdf
10 Replies

7. Shell Programming and Scripting

Match multiple patterns in a file and then print their respective next line

Dear all, I need to search multiple patterns and then I need to print their respective next lines. For an example, in the below table, I will look for 3 different patterns : 1) # ATC_Codes: 2) # Generic_Name: 3) # Drug_Target_1_Gene_Name: #BEGIN_DRUGCARD DB00001 # AHFS_Codes:... (3 Replies)
Discussion started by: AshwaniSharma09
3 Replies

8. Shell Programming and Scripting

Search multiple patterns in multiple files

Hi, I have to write one script that has to search a list of numbers in certain zipped files. For eg. one file file1.txt contains the numbers. File1.txt contains 5,00,000 numbers and I have to search each number in zipped files(The number of zipped files are around 1000 each file is 5 MB) I have... (10 Replies)
Discussion started by: vsachan
10 Replies

9. Shell Programming and Scripting

print lines which match multiple patterns

Hi, I have a text file as follows: 11:38:11.054 run1_rdseq avg_2-5 999988.0000 1024.0000 11:50:52.053 run3_rdrand 999988.0000 1135.0 128.0417 11:53:18.050 run4_wrrand avg_2-5 999988.0000 8180.5833 11:55:42.051 run4_wrrand avg_2-5 999988.0000 213.8333 11:55:06.053... (2 Replies)
Discussion started by: annazpereira
2 Replies

10. Shell Programming and Scripting

Find multiple patterns on multiple lines and concatenate output

I'm trying to parse COBOL code to combine variables into one string. I have two variable names that get literals moved into them and I'd like to use sed, awk, or similar to find these lines and combine the variables into the final component. These variable names are always VAR1 and VAR2. For... (8 Replies)
Discussion started by: wilg0005
8 Replies

Featured Tech Videos