Operating System - HP-UX
1838100 Members
4247 Online
110124 Solutions
New Discussion

Regarding string manipulation and ls in unix shell script

 
SOLVED
Go to solution
vind123
Regular Advisor

Regarding string manipulation and ls in unix shell script

I have a string

1. str1=topasa1
i want to store it in another string str2 as
str2=asa1
How do i do it in unix shell script

2. when i do a ls -ltr
ls -ltr
total 4
-rw-r--r-- 1 scc scc 8 Aug 14 00:46 test1
-rw-r--r-- 1 scc scc 17 Aug 14 00:47 test2

i want the latest file of test* to be stored in a string x1 how do i do it?

x1=ls -ltr test* |......
or
x1 = ls -lt test* |....

how do i get the latest file test2 into x1
i need to get the output as x1=test2

10 REPLIES 10
Peter Godron
Honored Contributor
Solution

Re: Regarding string manipulation and ls in unix shell script

Hi,

str2=`echo $str1 | cut -c3-`

x1=`ls -tr test? | tail -1`

vind123
Regular Advisor

Re: Regarding string manipulation and ls in unix shell script

Thanks a lot for the info. I am getting the below output when i run this command and i am getting the latestfile but i want remove the prefix "/home/users/scc/prodfiles/" in the output and store the test2 in x2 variable.

> ls -tr /home/users/fcc/prodfiles/test* | tail -1
/home/users/fcc/prodfiles/test2

I want the variable x2 to have test2 only
x2=`ls -tr /home/users/fcc/prodfiles/test* | tail -1`|.....

what do i need to do for this one. Also the directory name,path might change. i want to filter the content after final "/" to get the file name

Peter Godron
Honored Contributor

Re: Regarding string manipulation and ls in unix shell script

Hi,
please see:
man basename
vind123
Regular Advisor

Re: Regarding string manipulation and ls in unix shell script


>ls -tr /home/users/scc/prodfiles/test* | tail -1
/home/users/fcc/prodfiles/test2


I did the below one but i didn't get the output.

ls -tr /home/users/fcc/prodfiles/test* |tail -1 | basename



Marvin Strong
Honored Contributor

Re: Regarding string manipulation and ls in unix shell script

You can also just use your x1 variable as

${x1##*/}

that will drop off the path part of the variable. Useful if you want to preserve the path for some reason.

Peter Godron
Honored Contributor

Re: Regarding string manipulation and ls in unix shell script

Hi,
x1=`ls -tr /home/users/fcc/prodfiles/test* |tail -1`
x1=`basename $x1`

Also could you review your current threads after reading
http://forums1.itrc.hp.com/service/forums/helptips.do?#28

spex
Honored Contributor

Re: Regarding string manipulation and ls in unix shell script

Hi,

# x1=$(basename $(ls -1t /home/users/scc/prodfiles/test* | head -n 1))

PCS
Will Couillard
Occasional Advisor

Re: Regarding string manipulation and ls in unix shell script

For the first part of your post, if you always are removing the the first three characters of "str1" you can use:

$ str1=topasa1

$ str2=${str1#???}

Then:

$ print str2

to see the result

In this case each "?" removes antoher character from the left. To remove from the right, user "%".

Will
Will Couillard
Occasional Advisor

Re: Regarding string manipulation and ls in unix shell script

In my last response, line 6 should read

$ print $str2

Sorry,
Will
vind123
Regular Advisor

Re: Regarding string manipulation and ls in unix shell script

Thanks a lot for the info