LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-12-2006, 06:37 AM   #1
pbickerd
LQ Newbie
 
Registered: Jan 2006
Posts: 6

Rep: Reputation: 0
Redirecting output into a filename


Hi all,

Im having some problems...

What I am trying to do is take some text from a file and pre-fix it onto a file name.

I.e. say i have a file test.txt with a contents as follows:

123
bleh blah test test blah test blah test
test test test test test

and i want to rename the file:

123-text.txt

so somehow i need to redirect the output from something like 'cat' into something like a 'mv' command?

Not sure how I would do this. Hope that made some sense!

-Paul
 
Old 01-12-2006, 06:43 AM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
Here's one way:

mv test.txt "`head -n1 test.txt`-text.txt"
 
Old 01-12-2006, 06:48 AM   #3
jerril
Member
 
Registered: Nov 2005
Location: Ontario, Canada
Distribution: Linux Mint
Posts: 116

Rep: Reputation: 16
Hi Paul;

I'm not sure if I understand your question.

But...

mv is a short hand for move which can also be rename, so:

$ mv file.foo test/

will move the file to the test directory, if it exists

$ mv test.txt 123-test.txt

will rename the file.

I hope this helps.

jer
 
Old 01-12-2006, 06:50 AM   #4
jerril
Member
 
Registered: Nov 2005
Location: Ontario, Canada
Distribution: Linux Mint
Posts: 116

Rep: Reputation: 16
Oh shoot, I get it now, sorry!

jer
 
Old 01-12-2006, 07:33 AM   #5
pbickerd
LQ Newbie
 
Registered: Jan 2006
Posts: 6

Original Poster
Rep: Reputation: 0
I tried this as macemoneta suggested:

mv test.txt "`head -n1 test.txt`-text.txt"

but what it actually did was to rename test.txt to this:

`head -n1 test.txt`-text.txt

I then had some trouble deleting this weirdly named file

Does this have to be done from within a script? I tried just typing it in directly at the command line...

Thanks for your responses,
-Paul
 
Old 01-12-2006, 07:44 AM   #6
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
You used the wrong quote mark. In my example, they are not single quotes, but accent marks. Let me break it down for you:

fn=`head -n1 test.txt`

This will execute the command "head -n1 test.txt" and place the output into the variable "fn". The accented quotes tell the shell to do this. The result will be that the variable fn contains the first line of the file test.txt.

So if we look at the method I suggested:

mv test.txt "`head -n1 test.txt`-text.txt"

The mv command will be executed to rename test.txt to the first line of the file test.txt suffixed with the string "-text.txt".

The important part is the use of the proper accent quotes, to cause the result of the command to be substituted.

Last edited by macemoneta; 01-12-2006 at 07:45 AM.
 
Old 01-12-2006, 09:19 AM   #7
pbickerd
LQ Newbie
 
Registered: Jan 2006
Posts: 6

Original Poster
Rep: Reputation: 0
Ahhh that worked thankyou!!!

Now i'm going to pick your brains slighly further... I have managed to get that working but now the goalposts have been moved!

Basically now, the requirement has changed from using the text from the whole first line to prefix the filename to using a specific part of that line.

The first line of the file looks like this:
114925753200012617 01-Jan-2009

This is a concatination of a branch number + order number + deilvery date.

The number i want are the 3 digits after the first 1. Or if thats too difficult.. just the first 4 digits would do. The rest of the line can be discarded.

Im sure this can be done, i just need to know which of the many text display commands is needed

Also, at some point i will need to script this so it will make the same change on every file in a particular folder. So i will need to do something like:

mv %filename% "'head -n1 %filename%'-%filename"

im using a fictional variable %filename% above to try to get accross what i am trying to achieve.


Thanks again for all of your help!
-Paul

Last edited by pbickerd; 01-12-2006 at 09:25 AM.
 
Old 01-12-2006, 09:52 AM   #8
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Something like this..
Code:
#!/bin/bash

for i in *.txt ; do
   j=$(sed -n 1's/.\(.\{3\}\).*/\1/p' $i)
   mv $i "$j-$i"
done
or
Code:
#!/bin/bash

for i in *.txt ; do
   j=$(head -n1 < $i | sed -e 's/.\(.\{3\}\).*/\1/')
   mv "$i" "$j-$i"
done

Last edited by homey; 01-12-2006 at 10:25 AM.
 
Old 01-12-2006, 09:54 AM   #9
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
The Bash shell supports substrings via the syntax:

${string:position:length}

So, for your case where you want to start at the second character for a length of 3 (position is zero based):

${string:1:3}

Updating the earlier mv command:

line1=`head -n1 test.txt`
mv test.txt "${line1:1:3}-text.txt"

You might find the Advanced Bash Scripting Guide useful.

Last edited by macemoneta; 01-12-2006 at 09:56 AM.
 
Old 01-12-2006, 10:51 AM   #10
pbickerd
LQ Newbie
 
Registered: Jan 2006
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks guys! Im learning loads here.

I have made a script called "renameit" that will successfully rename my test.txt to what i want it to be. So now i need to make it so the script will accept a filename as a parameter and rename that file.

I have looked at that scripting guide macemoneta posted. Looks good. I think i can sort this out myself with a bit of reading.

Thanks again,
-Paul
 
Old 02-24-2006, 02:41 PM   #11
cramer
Member
 
Registered: Feb 2006
Distribution: Red Hat 9
Posts: 112

Rep: Reputation: 15
Just FYI, in the future steer away from naming files "test" as that is a command recognized by Bash.
 
  


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
Redirecting output of running command tommyr1216 Linux - General 3 03-04-2005 08:20 PM
redirecting output of compile to a file halfcan Linux - Hardware 1 02-05-2005 10:01 PM
Redirecting mplayer output driptray Linux - Software 1 10-12-2004 07:42 AM
redirecting output in Bourne shell trutnev Linux - Software 2 04-22-2004 03:55 AM
redirecting xmms output durden2.0 Linux - Software 0 03-06-2004 07:29 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:31 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