user3582044
user3582044

Reputation: 15

Find lines of file with multi patterns

I want to find lines of a file that have special patterns and print them in another file. For example:

a(code(4),message(h),b)

x(code(6),6)

v(message(d),b)

I want below output:

4 h

6

d

It means if code and message parts exist, values of them print and if one of them exists, print only that one

EDIT: I have lines like :

ereport(ERROR,(errmsg("database hash table corrupted during cleanup --- abort")));

ereport(FATAL,(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),errmsg("data directory \"%s\" has wrong ownership",DataDir),errhint("The server must be started by the user that owns the data directory.")));

I need below output:

database hash table corrupted during cleanup --- abort

ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE[tab]data directory \"%s\" has wrong ownership[tab]The server must be started by the user that owns the data directory.

These are error messages in PostgreSQL and I want to find list of them.

Upvotes: 0

Views: 176

Answers (2)

M. Adel
M. Adel

Reputation: 407

for the above example this code will extract the data as you want:

for i in `cat data.txt`
do
    echo $i | grep -E 'message|code' | sed 's/^.(//' | sed 's/code(//' | sed 's/,[a-z])//' | sed 's/,message(//' | cut -d, -f1 | sed 's/)/ /' | sed 's/)//' | sed 's/message(//'
done

Upvotes: 1

Jotne
Jotne

Reputation: 41446

You can use this:

awk -F"[()]" '{for (i=3;i<=NF;i+=2) printf "%s ",$i;print ""}' file
4 h
6
d

It will not work on your real data

Upvotes: 1

Related Questions