Printing value with no obvious field seperator

Hi all,

Can anybody think of a way to do this? I have a file with content like the following:

F_TOP_PARAM_VALUE[0]F_TOP_SOURCE[UD1]F_TOP_DEL_NOTIFICATION[N]F_DEST_ADDR[76849]F_TOP_DEL_TYPE

What I want to do is print out the value in the square brackets after F_TOP_SOURCE. So in this case I'd like to print UD1 for this line. The file contains multiple lines such as the above and the number of parameters listed in the file can change which means that F_TOP_SOURCE[???] won't always be the second paramter list - so I could have a file with two lines like:

F_TOP_PARAM_VALUE[0]F_TOP_SOURCE[UD1]F_TOP_DEL_NOTIFICATION[N]F_DEST_ADDR[76849]F_TOP_DEL_TYPE
F_TOP_ORIG[6547]F_TOP_PARAM_VALUE[0]F_TOP_SOURCE[PS1]F_TOP_DEL_NOTIFICATION[N]F_DEST_ADDR[76849]F_TOP_DEL_TYPE

Any ideas? The only thing I can be sure about is that "F_TOP_SOURCE" will always precede the value in the bracket that I want, and an "F" will always follow the bracket I want.

Nightmare!

Any help would be much appreciated. I think it's also worth noting that I didn't design the format of the file! :slight_smile:

Thanks in advance

Try this with sed:

sed 's/.*F_TOP_PARAM_VALUE\[\(.*\)\[.*/\1/' file

Regards

A Perl version:

cat temp.txt | perl -ne 'print "$_\n" if $_ =~ s/^.*F_TOP_SOURCE\[([^\]]+)].*$/$1/;'

This will print out the top source value for each line if there is one. If there isn't, it won't print anything for that line.

Also, this doesn't take into account multiple F_TOP_SOURCE sections in one line, so it will need to be modified if that can occur.

ShawnMilo

GNUawk

awk 'BEGIN{ RS="F_TOP_SOURCE[[]|[]]"}
RT ~ /F_TOP_SOURCE/{getline;print}
' "file"

output:

# ./test.sh
UD1
UD1
PS1

Thanks all, I used the perl solution which works really well.

Thanks again