Get part of the current file name as a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get part of the current file name as a string
# 1  
Old 07-12-2010
Get part of the current file name as a string

Hi All,
I've been searching through existing post and have been able to find most of what I want but am still stuck...

I'm trying to get part of the current file name as a string. The file name is in the form of:

basefilename_xxx(...).sh

I need the bit xxx(...) and it varies in length.

So far I have:

Code:
File=`basename $0 | cut -c 13-16

Obviously this will only get the first 3 characters of what I want.

How do I get any amount of characters between the underscore and the period without the extension? awk, sed, grep?


Thanks in advance
# 2  
Old 07-12-2010
Hi
Can you please give as an example string? Wanted to know how the (...) is there in the string?

Guru.
# 3  
Old 07-12-2010
Sorry the (...) is my probably bad way of saying that it's varying length. So some examples would be:

basefilename_lengthone.sh
basefilename_lengththree.sh
basefilename_lengtheleven.sh
basefilename_fivehundredandfortyone.sh
# 4  
Old 07-12-2010
Hi


Code:
basename basefilename_lengthone.sh .sh | sed 's/.*_//'

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 5  
Old 07-12-2010
a=fadfagr_whatiwant.sh
a=${a#*_}
a=${a%.*}

a is the variable
# 6  
Old 07-12-2010
Code:
$ string=junk1_junk2_whatiwant.sh
$ echo ${string##*_}
whatiwant.sh
$ string=${string##*_}
$ echo ${string%.*}
whatiwant

# 7  
Old 07-13-2010
Code:
[house@leonov] echo "basefilename_lengthone.sh" | awk -F "[_.]" '{print $2}'
lengthone

This User Gave Thanks to dr.house For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print particular string in a field of csv file - part 2

Hi, all I need your help and suggestions. I want to print particular strings in a field of a csv file and show them in terminal. Here is an example of the csv file. SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw... (7 Replies)
Discussion started by: refrain
7 Replies

2. Shell Programming and Scripting

Grep a part of file based on string identifiers

consider below file contents cat myOutputFIle.txt 8 CCM-HQE-ResourceHealthCheck: Resource List : No RED/UNKNOWN resource Health entries found ---------------------------------------------------------- 9 CCM-TraderLogin-Status: Number of logins: 0... (4 Replies)
Discussion started by: vivek d r
4 Replies

3. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

4. Shell Programming and Scripting

Deleting part of a string : string manipulation

i have something like this... echo "teCertificateId" | awk -F'Id' '{ print $1 }' | awk -F'te' '{ print $2 }' Certifica the awk should remove 'te' only if it is present at the start of the string.. anywhere else it should ignore it. expected output is Certificate (7 Replies)
Discussion started by: vivek d r
7 Replies

5. Shell Programming and Scripting

Get a part of a String from a log file

Hey there, I'm searched for one day your forum for a similar solution. Didn't find one, sorry :( Now here is what I'm searching for. I have the following multiple lines from a log (Edit: log name is SystemOut.log - this is not my day. I Apologize): 000042e2 1_BLA_Yab I logging.LogInfo... (5 Replies)
Discussion started by: nobodyhere
5 Replies

6. Shell Programming and Scripting

Finding a string in a text file and posting part of the line

What would be the most succinct way of doing this (preferably in 1 line, maybe 2): searching the first 10 characters of every line in a text file for a specific string, and if it was found, print out characters 11-20 of the line on which the string was found. In this case, it's known that there... (13 Replies)
Discussion started by: busdude
13 Replies

7. Shell Programming and Scripting

get part of file with unique & non-unique string

I have an archive file that holds a batch of statements. I would like to be able to extract a certain statement based on the unique customer # (ie. 123456). The end for each statement is noted by "ENDSTM". I can find the line number for the beginning of the statement section with sed. ... (5 Replies)
Discussion started by: andrewsc
5 Replies

8. UNIX for Dummies Questions & Answers

replace part of single string in a file

hi! i have a file consisting of the following lines: (BTW, = space) . . . 12ME_T1mapping_flip30bshortf 13DCE_whole_brainbshortf 13DCE_3Dbshortf . . . the list of scans starts at 1 and goes on sometimes up to 60 scans. i would like to change only the lines that contain 'whole' to... (2 Replies)
Discussion started by: nixjennings
2 Replies

9. Shell Programming and Scripting

current directory as part of the csh prompt

I would like my csh prompt to behave like the linux csh prompt setting done by linux command (set prompt="%n@%m %c]$ ") how do I do that? What I'm trying to do is that I would like to see what directory I'm in by looking at the prompt. I've figured out that %n is like $user, and %m is like... (3 Replies)
Discussion started by: jamesloh
3 Replies
Login or Register to Ask a Question