Removing last and first characters in a file

bash-3.00$ cat temp.txt
./a/REA01/ces1/apps/ces_ces1_init3_aa.ear/ces.war/WEB-INF/classes/reds/common/environment.properties
./a/REA01/ces1/apps/ces_ces1_init3_aa.ear/commonproperties/hi/HostIntegration.properties
./a/REA01/ces1/apps/ces_ces1_init3_aa.ear/commonproperties/hi/HostSocket.properties

I need a sheel script for the above file lines to be as below and it has to assign each one to one variable.
O/p:

/a/REA01/ces1/apps/ces_ces1_init3_aa.ear/ces.war/WEB-INF/classes/reds/common/
/a/REA01/ces1/apps/ces_ces1_init3_aa.ear/commonproperties/hi/
/a/REA01/ces1/apps/ces_ces1_init3_aa.ear/commonproperties/hi/
 
i=/a/REA01/ces1/apps/ces_ces1_init3_aa.ear/ces.war/WEB-INF/classes/reds/common/
j=/a/REA01/ces1/apps/ces_ces1_init3_aa.ear/commonproperties/hi/
k=/a/REA01/ces1/apps/ces_ces1_init3_aa.ear/commonproperties/hi/

could some one please let me know how to do it.

This might be a bit tricky to get into separately named variables. A few questions:-

  • What have you tried so far?
  • What errors/output are you getting?
  • What OS and version are you using?
  • What tools do you want to use? It looks like bash is there, but are you comfortable with anything else?

Most importantly, What have you tried so far?

Robin

Hi rbatte1,

Thanks for your reply.I tried to remove the last and first content in afile.But no luck.Below is the code which i tried so far.

cat temp.txt|awk -v skipstart=1 -v skipend=1 '{delim = "/"; for (i=skipstart+1;i<=NF-skipend;i++) {printf delim "%s", $i; delim = OFS}; printf "\n"}'

Comfortable with AIX machine.

dirname should help :slight_smile:

Hi rbatte1,

My find command will give this output.Can we take it in variable and can split it.Is it possible to do it..

find . -name "HostIntegration.properties" -o -name "HostSocket.properties" -o -name "environment.properties" 2> /dev/null |grep $var1|grep $var2|grep -e hi -e
 reds >/home/cbadmin/temp.txt

Almost anything is possible. I'm not sure what var1 & var2 are, but in theory this will work. Have you tried it? Did it give you some errors or just nothing?

I'm slightly confused by the change in the question. Back with the original post, I had prepared this answer already, so I'll share it anyway and see if that helps:-

Where does that leave you? is the above any use?

Is your find command building the input that you want to process? We can work with that.

Apologies if I've missed the point.

Robin

Try

A=105 # ="i"
while read INP; do INP=${INP%/*}; read $(printf "\x"$(printf "%X" $A)) <<< "${INP:1}"; ((A++)); done <file ; printf "%s\n" $i $j $k
/a/REA01/ces1/apps/ces_ces1_init3_aa.ear/ces.war/WEB-INF/classes/reds/common
/a/REA01/ces1/apps/ces_ces1_init3_aa.ear/commonproperties/hi
/a/REA01/ces1/apps/ces_ces1_init3_aa.ear/commonproperties/hi

Try:

for var in i j k
do 
  read $var
  eval $var=\${$var#.} 
  eval $var=\${$var%/*}/
done < file

As always, be very careful with eval with data from an uncontrolled source, as there may be security issues. It is better to do this with an array instead of separate variables.

i=0
while read arr
do
  arr=${arr#.}
  arr=${arr%/*}/
  (( i++ ))
done < file
echo "${arr[0]}"
echo "${arr[1]}"
echo "${arr[2]}"

Every example given are working as expected..Once again thanks to every one for helping me..

Hi Scrutinizer,

If i want save the modified file into to another file i.e file1.txt.What should i do.

for var in i j kdo   read $var  eval $var=\${$var#.}   eval $var=\${$var%/*}/done < file