Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

substr string from end of the string 2

Status
Not open for further replies.

himridul

Programmer
Jun 23, 2003
62
0
0
US
Hi,

I have number of files in a dir name ends with ?.err where ? is single literal. For example, the file names are:
MMLGO_3010_20100201100500.3010.5.err
MMLGO_3010_20100201100500.3010.7.err

I want to get the name MMLGO_3010_20100201100500.3010

Facts:
The length of MMLGO_3010_20100201100500.3010 can vary.

I'm very new in scripting. Can we use sed or awk to get the result? Thanks in advance!
 
Hi

Code:
[navy]str[/navy][teal]=[/teal][green][i]'MMLGO_3010_20100201100500.3010.5.err'[/i][/green]
echo [green][i]"${str%.*.err}"[/i][/green]
Tested with [tt]bash[/tt] and [tt]mksh[/tt].
himridul said:
Can we use sed or awk to get the result?
Yes...

Feherke.
 
Hi,

Try this code
Code:
for FILENAME in $(ls *.err )
do
echo ${FILENAME%.*.err}
done
 
And what about basename?

Code:
$ a="MMLGO_3010_20100201100500.3010.5.err"
$ b=$(basename $a .err)
$ echo $b
MMLGO_3010_20100201100500.3010.5


HTH,

p5wizard
 
Hi

Yes, but the OP wants to remove the last two dot ( . ) separated pieces, from which one seems to be fixed but the other is variable. So [tt]basename[/tt] alone can not solve it.

Feherke.
 
Don't use ls in shellscripts. Instead of
Code:
for FILENAME in $(ls *.err )
use
Code:
for FILENAME in *.err
otherwise you get problems with blanks in filenames, and you spend much too much time on typing. :)
Code:
asux:~/proj/mini/forum/quelle > touch a "b b" ccc
asux:~/proj/mini/forum/quelle > ls
a  b b  ccc
asux:~/proj/mini/forum/quelle > l
insgesamt 2
drwxr-xr-x  2 stefan stefan  120 2010-02-15 22:50 .
drwxr-xr-x 22 stefan stefan 1912 2010-02-15 22:11 ..
-rw-r--r--  1 stefan stefan    0 2010-02-15 22:50 a
-rw-r--r--  1 stefan stefan    0 2010-02-15 22:50 b b
-rw-r--r--  1 stefan stefan    0 2010-02-15 22:50 ccc
asux:~/proj/mini/forum/quelle > for f in $(ls *) ; do echo $f ; done 
a
b
b
ccc
asux:~/proj/mini/forum/quelle > for f in * ; do echo $f ; done 
a
b b
ccc
asux:


don't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top