![]() |
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 |
The best I can come up with is
Code:
tar -tvf file.tar | egrep 'mp3$' | tr -s [:blank:] ' ' | cut -d' ' -f6Code:
for i in $(tar -tvf file.tar | egrep 'mp3$' | tr -s [:blank:] ' ' | cut -d' ' -f6) |
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? |
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. |
@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).
|
Quote:
|
Quote:
|
tar -tf file.tar | sed 's/.*\///g' | grep ".mp3"
|
@timmeke,
Your option helps. Only the path is displayed. Extra information is discarded |
Quote:
Thats really cool. It works perfectly!!! Thanks everyone. |
Quote:
Thanks |
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 |
Quote:
|
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" |
Quote:
|
| All times are GMT -5. The time now is 12:27 AM. |