2

I am trying to extract part of a file name to compare with other file names as it is the only part that does not change. here is the pattern and an example

clearinghouse.doctype.payer.transID.processID.date.time EMDEON.270.60054.1234567890123456789.70949996.20120925.014606403

all sections are the same length at all times with the exception of clearinghouse & doctype that can vary in character length.

The part of the filename that i need for comparison is the transID.

What would be the cleanest shortest way to do this in a shell script.

Thanks

4 Answers 4

1

There are lots of ways to do this, the easiest tool for simple tasks is the cut command. Tell cut what character you want to use as a delemiter and which fields you want to print. Here is the command that does what you want.

file=EMDEON.270.60054.1234567890123456789.70949996.20120925.014606403
transitId=$(echo $file | cut -d. -f4)

Awk can do the same thing, and allows you do much more complicated logic as well.

file=EMDEON.270.60054.1234567890123456789.70949996.20120925.014606403
transitId=$(echo $file | awk -F. '{print $4}')
Sign up to request clarification or add additional context in comments.

Comments

1

You can split the filename apart using the read command using an appropriate value for IFS.

filename="EMDEON.270.60054.1234567890123456789.70949996.20120925.014606403"
IFS="." read clHouse doctype payer transID procID dt tm <<< "$filename"
echo $transID

Since you only want the transaction ID, it's overkill to assign every part to a specific variable. Use a single dummy variable for the other fields:

# You only need one variable after transID to swallow the rest of the input without
# splitting it up.
IFS="." read _ _ _ transID _  <<< "$filename"

or just read each part into a single array and access the proper element:

IFS="." read -a parts <<< "$filename"
transID="${parts[3]}"

Comments

0

You can do this with a parameter expansion:

$ foo=EMDEON.270.60054.1234567890123456789.70949996.20120925.014606403
$ bar=${foo%.[0-9]*.[0-9]*.[0-9]*}
$ echo "${bar##*.}"
1234567890123456789

Comments

0
tranid==`echo file_name|perl -F -ane 'print $F[3]'`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.