0

Following is a sample script that I have written

line="/path/IntegrationFilter.java:150:         * <td>http://abcd.com/index.do</td>"
echo "$line"          <-- "$line" prints the text correctly
result_array=( `echo "$line"| sed 's/:/\n/1' | sed 's/:/\n/1'`)
echo "${result_array[0]}"
echo "${result_array[1]}"
echo "${result_array[2]}"  <-- prints the first filename in the directory due to wildcard character *  .

How to get the text "* http://abcd.com/index.do " printed instead of the filename when retrieved from an array?

2 Answers 2

2

Assuming bash is the right tool, there are a few ways:

  1. disable filename expansion temporarily
  2. use read with IFS
  3. use the substitution feature of bash expansion

Disabling expansion:

line="/path/IntegrationFilter.java:150:         * <td>http://abcd.com/index.do</td>"
set -f
OIFS=$IFS
IFS=$'\n'
result_array=( `echo "$line"| sed 's/:/\n/1' | sed 's/:/\n/1'`)
IFS=$OIFS
set +f
echo "${result_array[0]}"
echo "${result_array[1]}"
echo "${result_array[2]}"

(note we also had to set IFS, otherwise each part of the contents ends up in result_array[2], [3], [4], etc.)

Using read:

line="/path/IntegrationFilter.java:150:         * <td>http://abcd.com/index.do</td>"
echo "$line"
IFS=: read file number match <<<"$line"
echo "$file"
echo "$number"
echo "$match"

Using bash parameter expansion/substitution:

line="/path/IntegrationFilter.java:150:         * <td>http://abcd.com/index.do</td>"
rest="$line"
file=${rest%%:*}
[ "$file" = "$line" ] && echo "Error"
rest=${line#$file:}

number=${rest%%:*}
[ "$number" = "$rest" ] && echo "Error"
rest=${rest#$number:}

match=$rest

echo "$file"
echo "$number"
echo "$match"
Sign up to request clarification or add additional context in comments.

1 Comment

It works for me and I will be using the second solution.Thanks Mikel!
0

How about:

$ line='/path/IntegrationFilter.java:150:         * <td>http://abcd.com/index.do</td>'

$ echo "$line" | cut -d: -f3-
* <td>http://abcd.com/index.do</td>

1 Comment

Did not work Marc.The problem with wildcard happens when I split the string , assign it to an array and try to retrieve it from array and dispaly!.

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.