Renaming files by appending string from within file to filename


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Renaming files by appending string from within file to filename
# 1  
Old 02-13-2019
Renaming files by appending string from within file to filename

Greetings. I am working in a Linux environment and am trying to figure out a way to rename files in a directory by appending a unique strings that appears within a certain area in those files. I have gotten as far as identifying what that particular unique string is with a command like the following:
Code:
for a in *split*; do cat $a | cut -d "~" -f 9 | head -n 1; done | wc -l

The part that I am trying to figure out is how to rename the various files in the directory by appending the value that the aforementioned command returns. For example, if filename "filename001," "filename002," and "filename009" contains strings "ABC," "DEF," and "XYZ" in the particular field respectively, the new filenames should read "filename001_ABC," "filename002_DEF," and "filename009_XYZ."

Thanks in advance for the assistance and please feel free to suggest elegant alternatives of the aforementioned command if you'd like.
# 2  
Old 02-13-2019
Hi, try so
Code:
for i in *split*; do tail="$(head -1 $i |cut -d "~" -f 9 )"; mv $i ${i}_$tail; done

This User Gave Thanks to nezabudka For This Post:
# 3  
Old 02-13-2019
Hi, try something like this:
Code:
for file in filename*                                  # For every file in the current directory that starts with "filename"
do
  if [ -r "$file" ]; then                              # Check if the file is readable
    IFS='~' read x x x x x x x x string x < "$file"    # Read the 9th field of the first line of the file into variable string and close
    if [ "$string" ]; then                             # Check if the string is not empty
      mv -- "$file" "${file}_${string}"                # Rename the file 
    fi 
  fi
done

you can put an echo statement before the mv command first to see what it does



--
If you are using bash , you can also read the fields of the first line into an array:
Code:
IFS='~' read -a string <"$file"

and then use ${string[8]} for the ninth field.

Last edited by Scrutinizer; 02-13-2019 at 01:30 AM..
These 3 Users Gave Thanks to Scrutinizer For This Post:
# 4  
Old 02-13-2019
But first check
Code:
for i in *split*; do tail="$(head -1 $i |cut -d "~" -f 9 )"; echo $i ${i}_$tail; done

# 5  
Old 02-13-2019
Thanks so much. I tried both methods and they worked. Unfortunately, I forgot to take into account that the particular field within the file where the string is extracted from the space(s) that follow it and both methods factored them in when renaming the files. Using the example filenames from before as an example, the renamed filenames read ""filename001_ABC ," "filename002_DEF ," and "filename009_XYZ " (the number of empty spaces varies). The ultimate goal is to have the filenames renamed as mentioned before but without the spaces and concluding with a specific suffix. I have tried variations of
Code:
for f in filename*; do mv "$f" `echo $f | tr ' ' '_'`; done

like
Code:
for a in filename*; do mv $a $a.edi; done

but they have been no-gos so far.

Last edited by Scrutinizer; 02-13-2019 at 03:24 AM.. Reason: Added iCode tags
# 6  
Old 02-13-2019
In bash, instead of ${string} you can use ${string// } to remove all spaces from string.

Another option is to use read again (bash/ksh93/zsh, but this time with the default IFS (Internal Field Separator)):
Code:
read string2 <<< "$string"

or other shells:
Code:
read string2 << EOF
$string
EOF


Last edited by Scrutinizer; 02-13-2019 at 03:49 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 02-13-2019
I'm not sure what you're trying to do. If you accidentally created a bunch of files with trailing <space> characters in their names and you just want to get rid of the trailing <space>s, try the following:
Code:
for file in *' '
do	mv "$file" $file
done

but this won't work if there are any <space> characters in the middle of a file's name.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Renaming multiple files at once (discarding suffix of the filename after a character)

Hi, I have a lot of files similar to the below order. I want to rename all the files .discrading the time stamp/numbers after cnf. Existing files id_info_20130405.cnf_20130801 in_info_20130405.cnf_20130891 iz_info_20130405.cnf_20130821 in_info_20130405.cnf_20130818... (2 Replies)
Discussion started by: saravanapandi
2 Replies

2. Shell Programming and Scripting

Check file for string existence before appending it with string

I want to append file with a string but before doing that i want to check if this string already exist in that file.I tried with grep on Solaris 10 but unsuccessful.Man pages from grep seems to suggest if the string is found command status will be 0 and if not 1.But i am not finding it.May be i... (2 Replies)
Discussion started by: sahil_shine
2 Replies

3. Shell Programming and Scripting

Renaming file that has multiple numbers as filename

Hi I have a file with filename as "partition-setup-and-ipl.vtcmd.76217657132.9721536798" Now i need to move this file as "partition-setup-and-ipl.vtcmd.76217657132.9721536798_org" i tried with # ls | grep -E "partition-setup-and-ipl.vtcmd.+"... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

4. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

5. Shell Programming and Scripting

Appending string, variable to file at the start and string at end

Hi , I have below file with 13 columns. I need 2-13 columns seperated by comma and I want to append each row with a string "INSERT INTO xxx" in the begining as 1st column and then a variable "$node" and then $2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13 and at the end another string " ; COMMIT;" ... (4 Replies)
Discussion started by: Vaddadi
4 Replies

6. Shell Programming and Scripting

Appending a string to all files in a directory

Hi, I will have to append a common string at the beginning of each and every line to all files in the same directory. How do i do this? (1 Reply)
Discussion started by: ragavhere
1 Replies

7. Shell Programming and Scripting

appending string to text file based on search string

Hi, I need to append string "Hi" to the beginning of the lines containing some specific string. How can I achieve that? Please help. Malay (1 Reply)
Discussion started by: malaymaru
1 Replies

8. Shell Programming and Scripting

Appending to filename a string of text grep finds

I am wanting to automate a process that includes the step of appending to a filename a string of text that's contained inside the file. I.e. if filename A.fileA contains a string of text that reads 1234 after the phrase ABC, I want the shell script file to rename the file 1234_FileChecked_A.fileA.... (3 Replies)
Discussion started by: HLee1981
3 Replies

9. UNIX for Dummies Questions & Answers

renaming a compressed file to filename without .Z

In a shell script I would like to use a compressed file name, i.e. with suffix of .Z, as a file input $1. After the file in uncompressed, I would like to use the file name without the .Z . How do I do this? Thank you. (8 Replies)
Discussion started by: bruceps
8 Replies

10. UNIX for Dummies Questions & Answers

Renaming files to have date/time in filename

I have a program that will export my data to a single file, but it assigns a file name that is overridden every time I run the program. I need to change the file name to have a sequential number in the filename. How do I rename a file so that the filename contains the system date and time. I want... (5 Replies)
Discussion started by: wayneb
5 Replies
Login or Register to Ask a Question