How to change name of the file with first line of the file which has some unwanted text in it?

I have a log file, which i have divided into 14 files using csplit, the file looks like below

test-000000
test-000001 #and so on until 14

now I want all the 14 files generated to be renamed as the some part of test in first line of the file how can i eliminate the unwanted text?

sample first line of the file looks like, all the files have similar format

0-00:01:01.412 <main>:[UNIT]:[info]:  Test: SCHEDULE_REQUEST_TestCase ...

I want to rename the file as SCHEDULE_REQUEST_TestCase.log
I know that mv command can be used to rename the file, and read or head can be used to read the first line of the file, but i want to remove the text before SCHEDULE and dots after TestCase.

This is what i have at the moment

line="$(head -n 1 test-000000)"
mv test-000000 ${line}.log

I want to perform this for all the files i have generated i don't really get how to do that and eliminate the unwanted text from the first line? I guess sed or awk can be used to eliminate the text I have started shell scripting like five days ago any help would be greatly appreciated.

The following is totally untested, but should come close to what you want. If the lines printed out by this look like the mv commands you hope to run, remove the echo from the awk script and run it again to actually remove the files:

awk 'FNR == 1 {print "echo mv",  FILENAME, $4 ".log"}' test-?????? | sh

On some systems, this could be made to run considerably faster by changing it to:

awk 'FNR == 1 {print "echo mv",  FILENAME, $4 ".log"; nextfile}' test-?????? | sh

but the awk nextfile command is not present in all versions of awk and you didn't bother to tell us what operating system or shell you're using (so we can't rely on it being available in your environment).

1 Like

Thanks Don Cragun, it is prints what i wanted it to look like . I am using bash shell, I have used nextfile displayed the same thing.

but how can do the same thing for the all the 14 files I have given that i standing on the current level , without having to do this manually for the every file

all the files are named similarly like

test-000000
test-000001
test-000002

I know i should loop through but was not sure how to increment the loop to go to next file, how can i achieve this?

Don Cragun's little awk script does exactly that - loop through ALL test-?????? files around, where ?????? matches the file names the sample numbers you post: 000000 , 000001 , 000002 , etc.
If - as he explains - the echo command is removed, and the schript's output is piped through sh , all your test files should be renamed according to your spec. That is - if all files contain DIFFERENT strings as $4 in their first line. If not, files with lower numbers might be overwritten by the ones with higher number and identical $4.

EDIT: To avoid that problem, consider this small adaption of Don Cragun's script:

$ awk 'FNR == 1 {while ($4 in FNARR) $4 = $4 "_" ++CNT; FNARR[$4];  print "echo mv",  FILENAME, $4 ".log"}' test-?????? | sh
mv test-000000 SCHEDULE_REQUEST_TestCase.log
mv test-000001 SCHEDULE_REQUEST_TestCase_1.log
mv test-000002 SCHEDULE_REQUEST_TestCase_2.log
mv test-000003 SCHEDULE_REQUEST_TestCase_3.log
etc...
1 Like

Thanks Rudic! i have replaced the ?????? in the awk script and substituted them with numbers now i have tried it and it works.

I will try your suggestion as well.

You don't need to "substitute them with numbers" - the shell will do that for you. Run the script with the -x (xtrace) option set, and you will see...