LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2009, 04:13 PM   #1
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Rep: Reputation: 44
help with cut command using find. Cut last 8 characters leaving the rest


Im trying to use find and cut, but instead of cutting the characters I want to save, I want to cut those characters leaving the rest of the results of my find command.


For example:

I have a file with the same name in multiple directories. I want to find just the directories that file is in but I do not want to print the file name.

/path/to/file.txt
/path/to/another/file.txt
/path/to/yet/another/file.txt

So my "command" would be:
[QUOTE]find / -name file.txt print just the directories[/QUOTE

Ive tried the type d flag but it doesnt seem to work if you are using file names)

So my results would be
/path/to/
/path/to/another/
/path/to/yet/another/


Ive tried various pipes to cut to remove the last 8 characters as that's my only constant. But instead of cutting file.txt off and leaving the directories it only cuts file.txt so my result is:
file.txt



Or is there a way to use find to find a file name but just print the directories they are in?



commands Ive tried:

Quote:
find / -name file | cut -c -8
This cuts everything after the first 9 characters of my find result. I want to cut the last 8 characters of the result leaving the rest of the line.



Confused yet?

Thanks!
 
Old 09-16-2009, 04:41 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Code:
find / -type f -name file.txt -print0 | while IFS= read -r -d '' filename
do
    echo "${filename#/*}"
done
 
Old 09-16-2009, 04:44 PM   #3
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
[QUOTE=ncsuapex;3685865]Im trying to use find and cut, but instead of cutting the characters I want to save, I want to cut those characters leaving the rest of the results of my find command.


For example:

I have a file with the same name in multiple directories. I want to find just the directories that file is in but I do not want to print the file name.

/path/to/file.txt
/path/to/another/file.txt
/path/to/yet/another/file.txt

So my "command" would be:
Quote:
find / -name file.txt print just the directories[/QUOTE

Ive tried the type d flag but it doesnt seem to work if you are using file names)

So my results would be
/path/to/
/path/to/another/
/path/to/yet/another/


Ive tried various pipes to cut to remove the last 8 characters as that's my only constant. But instead of cutting file.txt off and leaving the directories it only cuts file.txt so my result is:
file.txt



Or is there a way to use find to find a file name but just print the directories they are in?



commands Ive tried:



This cuts everything after the first 9 characters of my find result. I want to cut the last 8 characters of the result leaving the rest of the line.



Confused yet?

Thanks!
one way..

pipe to
Code:
sed -e's/........$//g'

or even

Code:
perl -pi -e 's/.{8}$//'

Last edited by centosboy; 09-16-2009 at 04:47 PM.
 
Old 09-16-2009, 04:47 PM   #4
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Original Poster
Rep: Reputation: 44
Thanks for the response. After I posted I figured it out. I was putting my - in the wrong place. It should be after your number to indicate you want to cut from left to right


Quote:
find / -name file.txt | rev | cut -c 8- | rev
 
Old 09-16-2009, 08:55 PM   #5
linuxraja
LQ Newbie
 
Registered: Sep 2009
Posts: 17

Rep: Reputation: 0
Quote:
Originally Posted by ncsuapex View Post
Thanks for the response. After I posted I figured it out. I was putting my - in the wrong place. It should be after your number to indicate you want to cut from left to right

Thats one thingy funny about shell script ... syntax and even spaces matter!


testing = 123

and

testing=123

have completely different meanings !!!

// removed spam link

Last edited by crabboy; 11-01-2010 at 08:03 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how can we cut the last 8 characters of a word? sravanth.svk Linux - Newbie 8 07-01-2009 09:33 PM
cut command - how to find separator? tikit Linux - Newbie 7 09-25-2008 11:28 AM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 07:16 PM
Cut last 3 characters of stdin? jago25_98 Linux - General 2 09-04-2004 11:04 AM
Cut Command Help!! fooforon Programming 2 02-05-2004 10:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration