1834457 Members
2617 Online
110067 Solutions
New Discussion

Re: shell script help

 
SOLVED
Go to solution
joinsiva
Advisor

shell script help

hi gurus,

i am writing a shell script, where i have got some doubts, can somebody clear me..

my file name is arpt_356789.dbf
which is saved in variable reala

now, i want to cut the number alone 356789,
if i use the cut -c 6-11 i will get the number, but in future the filenumbers may increase the counter value.. can anybody help me in getting that number alone from the variable.. ( i feel some ascii conversion loop is required .. i dont know.. just a guess)

can somebody help me..

also,

how to sftp to other server automatically without typing the password ??

my server is using HPUX 11.11

thanks in advance,
sivakumar p.
12 REPLIES 12
RAC_1
Honored Contributor
Solution

Re: shell script help

There are number of ways ot do it.
Shell patern atching would fasted and it will be shell built in function.

Is pattern arpt_xxxx.dbf common, if yes do it as follows

var1=arpt_356789.dbf
echo ${var1#arpt_}|awk -F . '{print $1}'

With awk

cat "arpt_356789.dbf" |awk -F _ '{print $2}'|awk -F . '{print $1}'

Anil
There is no substitute to HARDWORK
Ralph Grothe
Honored Contributor

Re: shell script help

$ file=arpt_356789.dbf

$ echo $file
arpt_356789.dbf

$ echo $file|tr -cd \[0-9]
356789
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: shell script help

or if you have a Posix shell (like HP-UX sh)

echo ${file#*_}|cut -d. -f1
356789
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: shell script help

There are hundred ways.
But I'd say the most lightweight is
shell variable expansion or usage of shell built-ins (read man sh-posix)
Then comes tr.
The mos heavy weight are interpreters like awk or Perl.
And if you use the latter two,
don't spend superfluous program calls like cat (awk like grep does this inherently)
Madness, thy name is system administration
john korterman
Honored Contributor

Re: shell script help

Hi,

inspired by Ralph:

# var1=arpt_356789.dbf
# newvar=$(echo $var1| tr -cd '[^0-9]')
# echo $newvar
356789

regards,
John K.
it would be nice if you always got a second chance
H.Merijn Brand (procura
Honored Contributor

Re: shell script help

Watch it with tr!

sh-3.00$ var1=arp1t_356789.dbf
sh-3.00$ newvar=$(echo $var1| tr -cd '[^0-9]')
sh-3.00$ echo $newvar
1356789
sh-3.00$ var1=arpt_356789.dbf1
sh-3.00$ newvar=$(echo $var1| tr -cd '[^0-9]')
sh-3.00$ echo $newvar
3567891
sh-3.00$

If you know it's the only number in the name, you're safe. If there might be more, you're not

if you want the first number

sh-3.00$ var1=arpt_356789.dbf1
sh-3.00$ newvar=$(echo $var1 | perl -ne'/(\d+)/&&print$1')
sh-3.00$ echo $newvar
356789
sh-3.00$

sed will do too I guess

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Sandman!
Honored Contributor

Re: shell script help

Sivakumar,

Your filename is stored in a shell script variable and to extract the number string from it, follow the awk excerpt given below:

=============================================
FNAME=arpt_356789.dbf

echo $FNAME | awk -F"." '{print z[split($1,z,"_")]}'
=============================================

cheers!
Sandman!
Honored Contributor

Re: shell script help

>>how to sftp to other server automatically without typing the password ??<<

for the above take a look at this URL
http://www.snailbook.com/faq/
joinsiva
Advisor

Re: shell script help

thanks everybody,

procura, that perl syntax doesn't worked.

anyway, i am using anils solution.

it worked fine.

thanks everybody once again

sivakumar p.
Muthukumar_5
Honored Contributor

Re: shell script help

You can try as,

# echo "arpt_356789.dbf" | sed 's/^.*_//;s/\..*$//'
356789

# echo "arpt_356789.dbf" | perl -pe 's/^.*_//;s/\..*$//'
356789

hth.
Easy to suggest when don't know about the problem!
Seth Parker
Trusted Contributor

Re: shell script help

Here's one with just the cut command:

$ echo arpt_356789.dbf | cut -f2 -d"_" | cut -f1 -d"."
356789
$
joinsiva
Advisor

Re: shell script help

thanks all

it worked.

sivakumar p.