LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   String Tokenizing in shell script (http://www.linuxquestions.org/questions/linux-newbie-8/string-tokenizing-in-shell-script-438652/)

Reegz 04-25-2006 09:13 AM

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

Hobbletoe 04-25-2006 09:29 AM

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.

Reegz 04-25-2006 09:39 AM

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?

timmeke 04-25-2006 09:44 AM

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.

timmeke 04-25-2006 09:46 AM

@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).

Reegz 04-25-2006 09:48 AM

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

Hobbletoe 04-25-2006 09:49 AM

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.

tells 04-25-2006 09:50 AM

tar -tf file.tar | sed 's/.*\///g' | grep ".mp3"

Reegz 04-25-2006 09:58 AM

@timmeke,

Your option helps. Only the path is displayed. Extra information is discarded

Reegz 04-25-2006 10:21 AM

Quote:

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


Thats really cool. It works perfectly!!! Thanks everyone.

Reegz 04-25-2006 10:26 AM

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

tells 04-25-2006 12:40 PM

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

Reegz 04-26-2006 10:36 AM

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!

muha 04-26-2006 02:53 PM

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 :D

Reegz 04-26-2006 04:07 PM

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 :D

Cool. Thanks for that tip as well


All times are GMT -5. The time now is 03:50 AM.