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
bhas85
July 4, 2014, 11:48am
3
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.
bhas85
July 4, 2014, 12:07pm
5
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:-
Rather than variables i, j & k, could you use an array? Something like:-
set -A myvar # Define array
l1=0
while read fullline
do
((l1=$l1+1))
nodot="${fullline#\.}"
myvar[$l1]="${nodot%\/*}"
done<temp.txt
l2=0
until [ $l2 -ge $l1 ]
do
((l2=$l2+1))
print "${myvar[$l2]}"
done
You should get the output lines trimmed and ready for use in the rest of your script as array elements of myvar indexed with a sequential number. Ignore the nodot variable, it's only temporary and has no further purpose.
The second loop in the script based on $l2 is just to display what you have extracted.
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
RudiC
July 5, 2014, 1:46am
7
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..
bhas85
July 10, 2014, 12:58pm
10
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