How to cut the name ?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to cut the name ?
# 1  
Old 11-06-2007
Data How to cut the name ?

Hi,
I do have a file name as follows:

12345.txt


Now in my script I need to have a varibale which will hold the file name alone wihtout any extensions.

VAL=12345


How can this be done ?? The file name size is not fixed.
# 2  
Old 11-06-2007
Code:
x=12345.txt
val=$( echo ${x%.*} )
echo $val

# 3  
Old 11-06-2007
Have You tried basename?

lakris@Lakrix:~/Projects/scripts$ basename 12345.txt
12345.txt
lakris@Lakrix:~/Projects/scripts$ basename 12345.txt .txt
12345
lakris@Lakrix:~/Projects/scripts$ basename /tmp/12345.txt .txt
12345
lakris@Lakrix:~/Projects/scripts$ man basename
Reformatting basename(1), please wait...
lakris@Lakrix:~/Projects/scripts$ dirname /tmp/12345.txt
/tmp
lakris@Lakrix:~/Projects/scripts$

/Lakris
# 4  
Old 11-06-2007
Quote:
Originally Posted by matrixmadhan
Code:
x=12345.txt
val=$( echo ${x%.*} )
echo $val

This should be sufficient:
Code:
x=12345.txt val="${x%.*}"

# 5  
Old 11-06-2007
hi radoulov ,

Thank u for the solution. But i need one more help. You have assigned
x=12345.txt

Say I do have some ten files in folder. I need to get the names of those files (without the extensions) . The print the maximum of that.

Say I do have follwoing files in a folder:

111.txt
222.txt
333.txt

I need a script whcih will give an output as :
333

Kindly give a soultion for this .. please
# 6  
Old 11-06-2007
The implementation depends on the shell you're using.

With zsh:

Code:
zsh-4.3.4% ls
1111.txt  111.txt  222.txt  333.txt
zsh-4.3.4% set -- *(:r)
zsh-4.3.4% print ${${(n)@}[-1]}
1111

With bash/ksh93 and sort:

Code:
(IFS=$'\n';set -- $(printf "%s\n" *|sort -rn);printf "%s\n" "${1%.*}")

Otherwise:

Code:
(IFS='
';set -- $(printf "%s\n" *|sort -rn);printf "%s\n" "${1%.*}")

(assuming no embedded new lines in the filenames)
(IFS is set to new line)

Last edited by radoulov; 11-06-2007 at 05:58 AM..
# 7  
Old 11-08-2007
Code:
ls | sort -rn | read a; echo ${a%.*}

I think that's the shortest solution (in bytes). Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using :<<cut / cut to comment out block of bash script

I am using : << cut / cut to comment out block of code. Works fine on few lines of script, then it gives me this cryptic error when I try to comment out about 80 lines. The "warning " is at last line of script. done < results 169 echo "END read all positioning parameters" 170... (8 Replies)
Discussion started by: annacreek
8 Replies

2. UNIX for Beginners Questions & Answers

Cut command: can't make it cut fields

I'm a complete beginner in UNIX (and not a computer science student either), just undergoing a tutoring course. Trying to replicate the instructions on my own I directed output of the ls listing command (lists all files of my home directory ) to My_dir.tsv file (see the screenshot) to make use of... (9 Replies)
Discussion started by: scrutinizerix
9 Replies

3. Shell Programming and Scripting

Cut Command error cut: Bad range

Hi Can anyone what I am doing wrong while using cut command. for f in *.log do logfilename=$f Log "Log file Name: $logfilename" logfile1=`basename $logfilename .log` flength=${#logfile1} Log "file length $flength" from_length=$(($flength - 15)) Log "from... (2 Replies)
Discussion started by: dgmm
2 Replies
Login or Register to Ask a Question