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
# 8  
Old 02-13-2019
@Don, but that may give unexpected results if the original field 9 contained wildcard characters, no?
# 9  
Old 02-13-2019
Quote:
Originally Posted by Scrutinizer
@Don, but that may give unexpected results if the original field 9 contained wildcard characters, no?
Yes. But for the three filenames shown in post #5:
Quote:
""filename001_ABC ," "filename002_DEF ," and "filename009_XYZ "
it works (assuming that the leading <double-quote> characters before "filename001_ABC " inside the ICODE tags was a typo).
# 10  
Old 02-13-2019
Quote:
Originally Posted by Scrutinizer
In bash, instead of ${string} you can use ${string// } to remove all spaces from string.
Thank you so much for this tidbit. I modified what should be line number nine in the excerpt below which eliminated two lines of code that I came across elsewhere. Below is an excerpt of the shell script file that includes your code which seems to work as expected and hoped.
-
Code:
for a in $(ls *.*); do csplit -s -k -f "$a"_split $a /^ISA/ {*}; rm *split00; mv *split[0-9]* split/; mv $a original;done
cd split
for a in $(ls *split*); do sed ':a;N;$! ba;s/\n//g' < $a > "$a"_$(TZ=":US/Eastern" date -r $a +%Y%m%d_%H%M%SET); rm $a; done
for file in *split*
do
  if [ -r "$file" ]; then
#    IFS='~' read -a ${string[8]} < "$file"
    IFS='~' read x x x x x x x x string x < "$file"
    if [ "$string" ]; then
      mv -- "$file" "${file}_${string// }.edi"
     fi
  fi
done

Thank you again.

By the way, I wasn't able to get what you mentioned the following earlier but I imagine that I was implementing it incorrectly which resulted in the errors when I tried to tinker with it. If you don't mind, can you walk me through implementing the cleaner-looking method using the array, please?
-
Quote:
Originally Posted by Scrutinizer
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.
# 11  
Old 02-13-2019
Quote:
Originally Posted by HLee1981
...
By the way, I wasn't able to get what you mentioned the following earlier but I imagine that I was implementing it incorrectly
... -
What shell version do you use?
What "shebang" in the beginning of your script?
What did you do?
Which errors did you get?
What be the output of
Code:
IFS='~' read -a string <"$file"
echo ${string[8]}

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