LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 04-25-2006, 09:13 AM   #1
Reegz
Member
 
Registered: Mar 2006
Location: South Africa
Distribution: Ubuntu, Slackware, openSuse
Posts: 44

Rep: Reputation: 15
String Tokenizing in shell script


Hi,

I have tar-ed up a directory. What I am now trying to do is list the contents of the archive verbosely and pipe the output to a file after using grep to fitler the output for mp3s. That I can do, However, I would like to somehow tokenize the output so that I get only the name of the file (*.mp3).

Any help would be appreciated.

Thanks
 
Old 04-25-2006, 09:29 AM   #2
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 10, Linux Mint 11
Posts: 148

Rep: Reputation: 18
The best I can come up with is

Code:
tar -tvf file.tar | egrep 'mp3$' | tr -s [:blank:] ' ' | cut -d' ' -f6
which gives the directory and file name. If you only want the file name, you can try

Code:
for i in $(tar -tvf file.tar | egrep 'mp3$' | tr -s [:blank:] ' ' | cut -d' ' -f6)
do
 echo ${i##*/}
done
Bit more complex, but should give you what you want. Any questions, just let me know. I'm sure there is a sed command for this, but I don't use sed often. But, I'm looking into it.
 
Old 04-25-2006, 09:39 AM   #3
Reegz
Member
 
Registered: Mar 2006
Location: South Africa
Distribution: Ubuntu, Slackware, openSuse
Posts: 44

Original Poster
Rep: Reputation: 15
Hi,

Thanks for those. I've tried them both. They work. However, some of the file names have white spaces (eg: An Artist - This song.mp3).

Is it possible to use regular expressions to solve the problem of white space?
 
Old 04-25-2006, 09:44 AM   #4
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
sed can't do what cut can.
If you want to do both the transliteration (as with tr) and the column cutting (as with cut), awk
can help you, but sed probably can't.
A Perl script may be an option too, but might be overkill if tr and cut do the trick together.

@Hobbletoe, in your solution, leaving out the -v option to tar eliminates the need to use tr and cut altogether.
 
Old 04-25-2006, 09:46 AM   #5
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
@Reegz, I recommend leaving off the -v option to tar in this case. It will only make the remainder of the command more complex, since the verbose output is multi-column, whereas the non-verbose output only lists the filenames and their paths (which is what you're looking for/grep'ing on).
 
Old 04-25-2006, 09:48 AM   #6
Reegz
Member
 
Registered: Mar 2006
Location: South Africa
Distribution: Ubuntu, Slackware, openSuse
Posts: 44

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by timmeke
@Reegz, I recommend leaving off the -v option to tar in this case. It will only make the remainder of the command more complex, since the verbose output is multi-column, whereas the non-verbose output only lists the filenames and their paths (which is what you're looking for/grep'ing on).
Cool thnx. Let me try that. Will let you know what happens
 
Old 04-25-2006, 09:49 AM   #7
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 10, Linux Mint 11
Posts: 148

Rep: Reputation: 18
Quote:
@Hobbletoe, in your solution, leaving out the -v option to tar eliminates the need to use tr and cut altogether.
Thank you. I rarely use the -t option for tar, and all of our documentation here at work uses the -v option, so it is second nature for me. Good to know though.
 
Old 04-25-2006, 09:50 AM   #8
tells
Member
 
Registered: Jun 2003
Location: Peoria, IL
Distribution: Ubuntu (home) / RHEL (work)
Posts: 65

Rep: Reputation: 15
tar -tf file.tar | sed 's/.*\///g' | grep ".mp3"
 
Old 04-25-2006, 09:58 AM   #9
Reegz
Member
 
Registered: Mar 2006
Location: South Africa
Distribution: Ubuntu, Slackware, openSuse
Posts: 44

Original Poster
Rep: Reputation: 15
@timmeke,

Your option helps. Only the path is displayed. Extra information is discarded
 
Old 04-25-2006, 10:21 AM   #10
Reegz
Member
 
Registered: Mar 2006
Location: South Africa
Distribution: Ubuntu, Slackware, openSuse
Posts: 44

Original Poster
Rep: Reputation: 15
Cool

Quote:
Originally Posted by tells
tar -tf file.tar | sed 's/.*\///g' | grep ".mp3"

Thats really cool. It works perfectly!!! Thanks everyone.
 
Old 04-25-2006, 10:26 AM   #11
Reegz
Member
 
Registered: Mar 2006
Location: South Africa
Distribution: Ubuntu, Slackware, openSuse
Posts: 44

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by tells
tar -tf file.tar | sed 's/.*\///g' | grep ".mp3"
If you dont mind, could you please explain to me what the syntax of the 'sed' command means as its been used in the command you've posted.

Thanks
 
Old 04-25-2006, 12:40 PM   #12
tells
Member
 
Registered: Jun 2003
Location: Peoria, IL
Distribution: Ubuntu (home) / RHEL (work)
Posts: 65

Rep: Reputation: 15
I am doing a find and replace with sed. So I searched for anything */ and replaced it with nothing.

s = search
/ = separator used by sed
. = match any character
* = match any number of the pervious character
\/ = this tells I am looking for the "/" character, since "/" is used as a separator in sed
/ = separator used by sed
since I did not specify anything here I am replacing with nothing
/ = separator used by sed
g = global
 
Old 04-26-2006, 10:36 AM   #13
Reegz
Member
 
Registered: Mar 2006
Location: South Africa
Distribution: Ubuntu, Slackware, openSuse
Posts: 44

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by tells
I am doing a find and replace with sed. So I searched for anything */ and replaced it with nothing.

s = search
/ = separator used by sed
. = match any character
* = match any number of the pervious character
\/ = this tells I am looking for the "/" character, since "/" is used as a separator in sed
/ = separator used by sed
since I did not specify anything here I am replacing with nothing
/ = separator used by sed
g = global
Cool. Thanks for that!
 
Old 04-26-2006, 02:53 PM   #14
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
Hint: when dealing with slashes it's usually easier to switch to another sed-separator. Example: #
Code:
tar -tf file.tar |sed 's#.*/##g' |grep ".mp3"
Now we don't have to escape every slash and it does the same
 
Old 04-26-2006, 04:07 PM   #15
Reegz
Member
 
Registered: Mar 2006
Location: South Africa
Distribution: Ubuntu, Slackware, openSuse
Posts: 44

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by muha
Hint: when dealing with slashes it's usually easier to switch to another sed-separator. Example: #
Code:
tar -tf file.tar |sed 's#.*/##g' |grep ".mp3"
Now we don't have to escape every slash and it does the same
Cool. Thanks for that tip as well
 
  


Reply

Tags
sed


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
Shell Script - String Replacement revof11 Programming 7 11-29-2005 06:38 AM
(shell script) string parsing kuru Programming 4 09-12-2005 07:59 PM
Shell script to find a particular string Prasun1 Linux - General 5 08-30-2005 09:23 AM
Get video DVD title string for shell script? cheesekeeper Linux - Hardware 9 05-05-2005 05:53 AM
change case of a string (TOUPPER) in shell script raees Linux - Software 3 05-03-2005 02:13 PM


All times are GMT -5. The time now is 02:31 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration