LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 07-10-2009, 04:19 PM   #1
lenik
LQ Newbie
 
Registered: Jul 2009
Posts: 5

Rep: Reputation: 0
Creating text file from ls output


Hello, i was trying to write a script that will list the contents of a directory in a file, but i need to add some prefix and suffix stuff to the listed files.

I want something like this:

<url='filname1.mp3'/>
<url='filname2.mp3'/>
....
<url='filenamexx.mp3'/>

so i thought about
Code:
Code:
echo -e "<url='$(ls -1 *.mp3)'/>"
but all i get is this:

<url='filename1.mp3
...
filenamexx.mp3'/>

Any ideas?
 
Old 07-10-2009, 04:34 PM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
You can loop through all files one by one. The basics below.
Code:
for f in $( ls -l *.mp3 ); do
        echo $f
done
 
Old 07-10-2009, 04:36 PM   #3
kilgoretrout
Senior Member
 
Registered: Oct 2003
Posts: 2,987

Rep: Reputation: 388Reputation: 388Reputation: 388Reputation: 388
Try:

$ ls -l *.mp3 | xargs -n1 -i echo "<url='{}'/>"

Last edited by kilgoretrout; 07-10-2009 at 04:38 PM.
 
Old 07-10-2009, 04:50 PM   #4
lenik
LQ Newbie
 
Registered: Jul 2009
Posts: 5

Original Poster
Rep: Reputation: 0
#!/bin/bash
for f in $( ls -1 *.mp3 ); do
echo -e "<url='$f'/>"
done

It works with files with no space in the name.
If there's a space it will jump to a new line dividing the file's name.
Is there a way to fix it, or better rename all files to replace the space with "_"?
 
Old 07-10-2009, 05:07 PM   #5
lenik
LQ Newbie
 
Registered: Jul 2009
Posts: 5

Original Poster
Rep: Reputation: 0
ok, solved!

IFS=$'\n'
for f in `find .`; do
file=$(echo $f | tr [:blank:] '_')
[ -e $f ] && [ ! -e $file ] && mv "$f" $file
done

for f in $( ls -1 *.mp3 ); do
echo -e "<url='$f'/>" >> playlist2.xml
done

Thank You all!!
 
  


Reply



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 to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 02:36 PM
in Pascal: how to exec a program, discard text output or send to text file Valkyrie_of_valhalla Programming 6 05-02-2007 09:50 AM
Creating a postscript output from a image and text rdfoerster Linux - Software 3 08-31-2004 10:26 AM
creating file with default text..how to? rohan208 Linux - General 3 05-10-2004 04:53 AM
Piping output to a text file joadoor Linux - Newbie 7 04-19-2002 03:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:52 PM.

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