Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.
How to rename a file based 1st line
Name: VivRichards Date: April 29, 2008 at 09:45:05 Pacific 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.
Name: nails Date: April 29, 2008 at 11:18:19 Pacific
Reply:
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}"
0
Response Number 2
Name: VivRichards Date: April 29, 2008 at 11:39:41 Pacific
Reply:
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
0
Response Number 3
Name: nails Date: April 29, 2008 at 13:55:07 Pacific
Reply:
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.
0
Response Number 4
Name: ghostdog Date: April 30, 2008 at 00:06:22 Pacific
Summary: Hi, How to parse a file that has more than 199 columns. 'awk' fails when line is having more than 199 fields. Is there a way to resolve this...what i am thinking is to strip off all those columns be...