Extract first line of a file and use as filename


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extract first line of a file and use as filename
# 1  
Old 06-30-2010
Extract first line of a file and use as filename

I am trying to find a way to create a script which will extract the first line of a file and then rename the file (or create a new file with the same content as the old file) using the first line as the name. The first line being a single word, that is. I am hopeless at programming, if anyone can help you I would appreciate it very much!!
# 2  
Old 06-30-2010
Hi, welcome to the forum. This would be one way of doing that:
Code:
mv "$file" "$(head -1 "$file")"

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 3  
Old 06-30-2010
Thank you!! That works like a charm. It always looks so much easier when someone else does it Smilie

---------- Post updated at 07:55 PM ---------- Previous update was at 05:19 PM ----------

You wouldn't happen to know how to make it iterate over every file in a directory? I've tried this:

for i in *; do mv $i "$(head -1 $i)"; done

Which doesn't work, and which I don't know enough to why it doesn't work. Well, it does rename the files after which terminal no longer seems to recognize them as files. Just using * in place of filename causes it to use the first line from every file...
# 4  
Old 07-01-2010
Hi, don't forget the double quotes.. See if this works better.

Code:
for i in *; do mv "$i" "$(head -1 "$i")"; done

Are you sure each and every file contains a strictly a file name on the first line?

To get an idea of what your command does you can do this:
Code:
for i in *; do echo mv "$i" "$(head -1 "$i")"; done


Last edited by Scrutinizer; 07-01-2010 at 03:37 AM..
# 5  
Old 07-01-2010
Similar idea but with some error handling. Also deal with unlimited number of filenames, filenames containing spaces and what happens when the script is run a second time.
When tested, remove "echo" from the "mv" line.

Code:
#!/bin/ksh
ls -1d * | while read FILENAME1
do
        # Ignore if not a regular file
        if [ ! -f "${FILENAME1}" ]
        then
                continue
        fi
        # Bypass empty file
        if [ ! -s "${FILENAME1}" ]
        then
                echo "Empty file : ${FILENAME1}"
                continue
        fi
        # Extract filename from first record
        FILENAME2=`head -1 "${FILENAME1}"`
        if [ -f "${FILENAME2}" ]
        then
                echo "File from ${FILENAME1} aleady exists: ${FILENAME2}"
                continue
        fi
        # Rename file (remove echo when tested)
        echo mv "${FILENAME1}" "${FILENAME2}"
done

# 6  
Old 07-01-2010
Thanks!! I haven't tried the second solution, but I will. The first works, I figured out the python script creating the files was adding a space. Once I fixed that everything started working. Thank you!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to extract when filename contains file seperator..?

Hi, I want to extract part of filename, for eg: File="010020004_S-TOR-Sort-CASAP_20170519_121504_0007.TXT" here i need first 5 words of file i.e. FilePart="$(echo "${File%"${File#******}"}")" Echo $FilePart 010020004_S-TOR-Sort-CASAP But what if i get filename like below: ... (3 Replies)
Discussion started by: gnnsprapa
3 Replies

2. Shell Programming and Scripting

Perl to extract information from a file line by line

In the below perl code I am using tags within each line to extract certain information. The tags that are used are: STB >0.8 is STRAND BIAS otherwise GOOD FDP is the second number GO towards the end of the line is read into an array and the value returned is outputed, in the first line that... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

Extract data according to keys from filename mentioned in file

Hello experts, I want to join a file with files whosE names are mentioned in one of the columns of the same file. File 1 t1,a,b,file number 1 t1,a,c,file number 1 t2,c,d,file number 2 t2,c,e,file number 2 t2,c,f,file number 2 t2,c,g,file number 2 t3,e,f,file number 3 file number 1... (3 Replies)
Discussion started by: ritakadm
3 Replies

4. Programming

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My code: if then set "subscriber" "promplan" "mapping" "dedicatedaccount" "faflistSub" "faflistAcc" "accumulator"\ "pam_account"; for i in 1 2 3 4 5 6 7 8;... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

5. UNIX for Dummies Questions & Answers

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My Requirement: 1) There are some set of files in a directory like given below OTP_UFSC_20120530000000_acc.csv OTP_UFSC_20120530000000_faf.csv OTP_UFSC_20120530000000_prom.csv... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

6. Shell Programming and Scripting

HELP: Shell Script to read a Log file line by line and extract Info based on KEYWORDS matching

I have a LOG file which looks like this Import started at: Mon Jul 23 02:13:01 EDT 2012 Initialization completed in 2.146 seconds. -------------------------------------------------------------------------------- -- Import summary for Import item: PolicyInformation... (8 Replies)
Discussion started by: biztank
8 Replies

7. Shell Programming and Scripting

extract every filename containing certain string in a directory and do some commend per file

Hi, Here is my question: suppose I have files like 1990_8xdaily_atmos.nc 1991_8xdaily_atmos.nc 1992_8xdaily_atmos.nc 1993_8xdaily_atmos.nc 1990_daily_atmos.nc 1991_daily_atmos.nc 1992_daily_atmos.nc 1993_daily_atmos.nc 1990_month_atmos.nc 1991_month_atmos.nc 1992_month_atmos.nc... (1 Reply)
Discussion started by: 1988PF
1 Replies

8. UNIX for Dummies Questions & Answers

Replacing first line of file by >filename

Hi All, I have a set of files named S5_SK1.chr01 S5_SK1.chr02 S5_SK1.chr03 ..... and the first line of these files is >SK1.chr01 >SK1.chr02 >SK1.chr03 ..... Can anyone suggest how I can change the first line of all these files with the filename itself? So my expected output for the first lines of... (14 Replies)
Discussion started by: pawannoel
14 Replies

9. Shell Programming and Scripting

Extract date from filename and create a new file

Hi, i have a filename CRED20102009.txt in a server 20102009 is the date of the file ddmmaaaa format the complete route is /dprod/informatica/Fuentes/CRED20102009.csv i want to extract the date to create a new file named Parameters.txt I need to create Parameters.txt with this... (6 Replies)
Discussion started by: angel1001
6 Replies

10. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies
Login or Register to Ask a Question