Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Subject: How to rename a file based 1st line

Original Message
Name: VivRichards
Date: April 29, 2008 at 09:45:05 Pacific
Subject: How to rename a file based 1st line
OS: AIX
CPU/Ram: 6
Comment:
I like to rename a file based on the first line. Here is an example of what I need to do.

File: 034568.dat has 2000 lines with first line as
HIOSPAD23409JHK90000
I like to extract 3 characters from 5th column and rename the file to PAD_034568.dat.


Really appreciate your help.
Viv


Report Offensive Message For Removal

Response Number 1
Name: nails
Date: April 29, 2008 at 11:18:19 Pacific
Subject: How to rename a file based 1st line
Reply: (edit)
When you're sure it works, change 'cp' to 'mv':


#!/bin/ksh

# no error checking
myfile=034568.dat
prevar=$(head -1 "$myfile"| cut -c5-7)

cp "$myfile" "${prevar}_${myfile}"


Report Offensive Follow Up For Removal

Response Number 2
Name: VivRichards
Date: April 29, 2008 at 11:39:41 Pacific
Subject: How to rename a file based 1st line
Reply: (edit)
Thanks for your response, being a UNIX novice I have another Q related to this...

Now if I want to put this into a script to execute against unknown filename(s), how would I accomplish that?!

Thanks


Report Offensive Follow Up For Removal

Response Number 3
Name: nails
Date: April 29, 2008 at 13:55:07 Pacific
Subject: How to rename a file based 1st line
Reply: (edit)
Consider this:


#!/bin/ksh

# UNTESTED
cd /your/directory
for myfile in $*
do
if [[ -f $myfile ]]
then
prevar=$(head -1 "$myfile"| cut -c5-7)
cp "$myfile" "${prevar}_${myfile}"
fi
done

The above script changes to a /your/directory. That's where your files are.

If your script is called x.ss, execute it with your file names:

x.ss 034568.dat file2.dat file3.dat

if the file exists, it executes the commands.


Report Offensive Follow Up For Removal

Response Number 4
Name: ghostdog
Date: April 30, 2008 at 00:06:22 Pacific
Subject: How to rename a file based 1st line
Reply: (edit)

awk 'FNR==1{
cmd = "mv "FILENAME" "substr($0,5,3)"_"FILENAME
system(cmd)
}' *.dat


Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: How to rename a file based 1st line

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software



Version Tracker Pro
Keep your software current and secure, effortlessly

Click Here for a Free Scan

Driver Agent
Automatically find the latest drivers for your computer.
Click Here for a Free Scan



The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC