0

I have a string let's say

k=CHECK_${SOMETHING}_CUSTOM_executable.acs

Now I want to fetch only CUSTOM_executable from the above string. This is what I have tried so far in Unix

echo $k|awk -F '_' '{print $2}'

Can you explain how can i do this

5 Answers 5

1

Try this :

$ echo "$k"
CHECK_111_CUSTOM_executable.acs

code:

echo "$k" | awk 'BEGIN{FS=OFS="_"}{sub(/.acs/, "");print $3, $4}'
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Suppose my SOMETHING is 111_111 ? Then how can i handle this?
Then you have to use $4, $5
0

Assume the variable ${SOMETHING} has the value SOMETHING just for simplicity.

The following assignment, therefore,

k=CHECK_${SOMETHING}_CUSTOM_executable.acs

sets the value of k to CHECK_SOMETHING_CUSTOM_executable.acs.

When split into fields on _ by awk -F '_' (note the single quotes aren't necessary here).

You get the following fields:

$ echo "$k" | awk -F _ '{for (i=0; i<=NF; i++) {print i"="$i}}'
0=CHECK_SOMETHING_CUSTOM_executable.acs
1=CHECK
2=SOMETHING
3=CUSTOM
4=executable.acs

So to get the output you want simply use

echo "$k" | awk -F _ -v OFS=_ '{print $3,$4}'

Comments

0

Suppose if SOMETHING variable is having 111_222_333 (or) 111_222_333_444,

Use this:

$ k=CHECK_${SOMETHING}_CUSTOM_executable.acs

$ echo $k | awk 'BEGIN{FS=OFS="_"}{ print $(NF-1),$NF }'

(Or)

 echo $k | awk -F_ '{ print $(NF-1), $NF }' OFS=_

Explanation :

NF - The number of fields in the current input record.

Comments

0

Try this simple :

awk -F[._] '{print $3"_"$4}' <<<"$k"

CUSTOM_executable

The -F[._] defines both dot and underline as field separator. Then awk prints the filed number 3 and 4 from $k as input.

If the k contains k='CHECK_${111_111}_CUSTOM_executable.acs', then use filed with numbers $4 and $5:

awk -F[._] '{print $4"_"$5}' <<<"$k"
CHECK_${111_111}_CUSTOM_executable.acs
| $1| |$2 | |$3| | $4 | |   $5   | |$6|

Comments

0

You do not need to use , it can be done in easily. I assume that $SOMETHING does not contains _ characters (also CUSTOM and executable part is just some text, they also not contains _). Then:

k=CHECK_${SOMETHING}_CUSTOM_executable.acs
l=${k#*_}; l=${l#*_}; l=${l%.*}; 

This cuts anything from the beginning to the 2nd _ char, and chomps off anything after the last . char. Result is put into the l env.var.

If $SOMETHING may contain _ then a little bit work has to be done (I assume the CUSTOM and executable part does not contain _):

k=CHECK_${SOMETHING}_CUSTOM_executable.acs
l=${k%_*}; l=${l%_*}; l=${k#${l}_*}; l=${l%.*}; 

This chomps off everything after the last but one _ character, the cuts the result off from the original string. The last statement chomps the extension off. The result is in l env.var.

Or it can be done using :

[[ $k =~ ([^_]+_[^_]+)\.[^.]+$ ]] && l=${BASH_REMATCH[1]}

This matches any string containing two words separated by _ and finished with .<extension>. The extension part is chomped off and result is in l env.var.

I hope this helps!

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.