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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sed - stripping out directories & file extensions 2

Status
Not open for further replies.

isileth

Technical User
Jul 18, 2001
9
US
I've got a utility which outputs certain filenames in the following format:

/dir1/dir2/dir3/dir4/line_name__________more_info.extension

/dir1/dir2/dir3/line_name__________more_info.extension
^^^^^^^^^
I need to strip out just the line_name part (without the trailing underscores). The line name COULD be preceeded by any number of subdirectories.

The line_name__________ will always total 30 chars in length.

(line_name, dir*, more_info, extension are all variables)

Thanks....

 
>echo $e
/dir1/dir2/dir3/line_name__________more_info.extension

>echo $e | awk ' {
gsub(/\/[a-z]+_[a-z]+/,"/",$0) ; print $0
}'

/dir1/dir2/dir3/__________more_info.extension

I had trouble with the same pattern with sed for some
reason...
 
sed 's/\/[a-z][a-z]*_[a-z][a-z]*/\//' will do it in sed.

To assure the change is only to the start of the filename and not the start of a directory name then

sed 's/\(.*\/\)[a-z][a-z]*_[a-z][a-z]*/\1/'

Does to poster want the filename's substring 'line_name' as a constant removed? Then it would be

sed 's/\(.*\/\)line_name/\1/' Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
OK just reread my original post...

....could be slightly unclear. I have a large file with thousands of lines in the following format:

/dir1/dir2/dir3/dir4/line_name__________more_info.extension

And I simply want a listing of the line_name section (as discussed in my initial email).
 
awk ' {
if (match($0,/\/[a-z]+_[a-z]+/) {
line = substr($0,RSTART,RLENGTH)
sub(/\//,"",line)
print line
}'
 
something like that in nawk:

nawk -F "/" '{ split($NF, tmp, "_[_]+"); print tmp[1]}' file.txt vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top