LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Redirecting output into a filename (https://www.linuxquestions.org/questions/linux-software-2/redirecting-output-into-a-filename-402416/)

pbickerd 01-12-2006 06:37 AM

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

macemoneta 01-12-2006 06:43 AM

Here's one way:

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

jerril 01-12-2006 06:48 AM

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

jerril 01-12-2006 06:50 AM

Oh shoot, I get it now, sorry!

jer

pbickerd 01-12-2006 07:33 AM

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

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

macemoneta 01-12-2006 07:44 AM

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.

pbickerd 01-12-2006 09:19 AM

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

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

homey 01-12-2006 09:52 AM

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


macemoneta 01-12-2006 09:54 AM

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.

pbickerd 01-12-2006 10:51 AM

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

cramer 02-24-2006 02:41 PM

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


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