Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Rename files based on simple text file Post 302990030 by Corona688 on Friday 20th of January 2017 02:59:27 PM
Old 01-20-2017
Why are the filename and message content in different files? One little mistake will create a giant mess.

In any case, this being UNIX, there's ways to hedge your bets... I'd make an 'out' folder and create links in it, instead of renaming the original files. If something goes wrong, you're not left with all your files irreparably misnamed, just a bit of trash you can delete and start over.

Code:
#!/bin/bash
mkdir -p ./out/

exec 5<filenames
exec 6<filetitles
while read LINE FILENAME <&5 && IFS=$'\t' read NUM FILETITLE <&6
do
        if [ "$NUM" -ne "$LINE"]
        then
                echo "Numbers do not match?"
                continue
        fi

        FILETITLE="${FILETITLE//$'\t'/ - }" # Change tab into ' - '        
        ln "$FILENAME" out/"${NUM} ${FILETITLE}"
done

exec 5<&- # Close files
exec 6<&-

This should create links in the ./out/ folder of the right names.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename files/directories based on their name

i have hundreds of directories that have to be renamed. the directory structure is fairly uniform which makes the scripting a little simpler. suppose i have many directories like this */*/*/*abc* (in other words i have similar directory names 3 dirs deep that all contain the pattern abc in... (8 Replies)
Discussion started by: quantumechanix
8 Replies

2. Shell Programming and Scripting

splitting files based on text in the file

I need to split a file based on certain context inside the file. Is there a unix command that can do this? I have looked into split and csplit but it does not seem like those would work because I need to split this file based on certain text. The file has multiple records and I need to split this... (1 Reply)
Discussion started by: matrix1067
1 Replies

3. UNIX for Dummies Questions & Answers

extracting text and reusing the text to rename file

Hi, I have some ps files where I want to ectract/copy a certain number from and use that number to rename the ps file. eg: 'file.ps' contains following text: 14 (09 01 932688 0)t the text can be variable, the only fixed element is the '14 ('. The problem is that the fixed element can appear... (7 Replies)
Discussion started by: JohnDS
7 Replies

4. Shell Programming and Scripting

Bash snippet to find files based on a text file?

Evening all. I'm having a terrible time with a script I've been working on for a few days now... Say I have a text file named top10song.tm2, with the following in it: kernkraft 400 Imagine i kissed a girl Thriller animals hallelujah paint it black psychosocial Oi to the world... (14 Replies)
Discussion started by: DJ Charlie
14 Replies

5. Shell Programming and Scripting

rename files Ax based on strings found in files Bx

Hi, I'm not very experienced in shell scripting and that's probably why I came across the following problem: I do have several hundred pairs of text files (PF00x.spl and PF00x.shd) where the first file (PF00x.spl) needs to be renamed according a string that is included in the second file... (12 Replies)
Discussion started by: inCH
12 Replies

6. UNIX for Dummies Questions & Answers

rename files based on their respective directory name

I have a number of files in directories labeled like this: /Data/tr_gray/tr_DTI/dti_FA.nii.gz (the brackets here represent a range of number that the files are labeled with) I need to rename each dti_FA.nii.gz file according to the name of the folder it resides in. For example, the file ... (3 Replies)
Discussion started by: tk0034
3 Replies

7. Shell Programming and Scripting

Extracting lines from text files in folder based on the numbers in another file

Hello, I have a file ff.txt that looks as follows *ABNA.txt 356 24 36 112 *AC24.txt 457 458 321 2 ABNA.txt and AC24.txt are the files in the folder named foo1. Based on the numbers in the ff.txt file, I want to extract the lines from the corresponding files in the foo1 folder and... (2 Replies)
Discussion started by: mohamad
2 Replies

8. Shell Programming and Scripting

Loop through the dir and Rename zip files and their underlying text file.

I have files in the ABC_YYYYMMDD.zip format under a directory. Each zip file contains A text file in the ABC_YYYYMMDD.txt format. I am trying to create a script that will Rename the zip files and their underlying text file replacing the datepart in them with . For eg: in the case of... (1 Reply)
Discussion started by: bash987
1 Replies

9. UNIX for Dummies Questions & Answers

Rename files based on a list

Hi, I have a directory with a lot of files like this: a.bam b.bam c.bam I like to rename these files based on a list where the name of the files in the first column will be replasced by the names in the second column. Here is my list which is a tab-delimited text file: a x b y c ... (4 Replies)
Discussion started by: a_bahreini
4 Replies

10. Shell Programming and Scripting

Rename files based on name in text file

Hello, I have a text file "file.list" with the contents below. file1 filename1 file2 filename2 file3 filename3 file1, file2 and file3 are files existing in the same directory as the text file file.list. I want to rename file1 to filename1, file2 to filename2, as show in the text... (1 Reply)
Discussion started by: james2009
1 Replies
Template::Plugin::String(3pm)				User Contributed Perl Documentation			     Template::Plugin::String(3pm)

NAME
Template::Plugin::String - Object oriented interface for string manipulation SYNOPSIS
# create String objects via USE directive [% USE String %] [% USE String 'initial text' %] [% USE String text => 'initial text' %] # or from an existing String via new() [% newstring = String.new %] [% newstring = String.new('newstring text') %] [% newstring = String.new( text => 'newstring text' ) %] # or from an existing String via copy() [% newstring = String.copy %] # append text to string [% String.append('text to append') %] # format left, right or center/centre padded [% String.left(20) %] [% String.right(20) %] [% String.center(20) %] # American spelling [% String.centre(20) %] # European spelling # and various other methods... DESCRIPTION
This module implements a "String" class for doing stringy things to text in an object-oriented way. You can create a "String" object via the "USE" directive, adding any initial text value as an argument or as the named parameter "text". [% USE String %] [% USE String 'initial text' %] [% USE String text='initial text' %] The object created will be referenced as "String" by default, but you can provide a different variable name for the object to be assigned to: [% USE greeting = String 'Hello World' %] Once you've got a "String" object, you can use it as a prototype to create other "String" objects with the "new()" method. [% USE String %] [% greeting = String.new('Hello World') %] The "new()" method also accepts an initial text string as an argument or the named parameter "text". [% greeting = String.new( text => 'Hello World' ) %] You can also call "copy()" to create a new "String" as a copy of the original. [% greet2 = greeting.copy %] The "String" object has a "text()" method to return the content of the string. [% greeting.text %] However, it is sufficient to simply print the string and let the overloaded stringification operator call the "text()" method automatically for you. [% greeting %] Thus, you can treat "String" objects pretty much like any regular piece of text, interpolating it into other strings, for example: [% msg = "It printed '$greeting' and then dumped core " %] You also have the benefit of numerous other methods for manipulating the string. [% msg.append("PS Don't eat the yellow snow") %] Note that all methods operate on and mutate the contents of the string itself. If you want to operate on a copy of the string then simply take a copy first: [% msg.copy.append("PS Don't eat the yellow snow") %] These methods return a reference to the "String" object itself. This allows you to chain multiple methods together. [% msg.copy.append('foo').right(72) %] It also means that in the above examples, the "String" is returned which causes the "text()" method to be called, which results in the new value of the string being printed. To suppress printing of the string, you can use the "CALL" directive. [% foo = String.new('foo') %] [% foo.append('bar') %] # prints "foobar" [% CALL foo.append('bar') %] # nothing CONSTRUCTOR METHODS
These methods are used to create new "String" objects. new() Creates a new string using an initial value passed as a positional argument or the named parameter "text". [% USE String %] [% msg = String.new('Hello World') %] [% msg = String.new( text => 'Hello World' ) %] copy() Creates a new "String" object which contains a copy of the original string. [% msg2 = msg.copy %] INSPECTOR METHODS
These methods are used to examine the string. text() Returns the internal text value of the string. The stringification operator is overloaded to call this method. Thus the following are equivalent: [% msg.text %] [% msg %] length() Returns the length of the string. [% USE String("foo") %] [% String.length %] # => 3 search($pattern) Searches the string for the regular expression specified in $pattern returning true if found or false otherwise. [% item = String.new('foo bar baz wiz waz woz') %] [% item.search('wiz') ? 'WIZZY! :-)' : 'not wizzy :-(' %] split($pattern, $limit) Splits the string based on the delimiter $pattern and optional $limit. Delegates to Perl's internal "split()" so the parameters are exactly the same. [% FOREACH item.split %] ... [% END %] [% FOREACH item.split('baz|waz') %] ... [% END %] MUTATOR METHODS
These methods modify the internal value of the string. For example: [% USE str=String('foobar') %] [% str.append('.html') %] # str => 'foobar.html' The value of "str" is now '"foobar.html"'. If you don't want to modify the string then simply take a copy first. [% str.copy.append('.html') %] These methods all return a reference to the "String" object itself. This has two important benefits. The first is that when used as above, the "String" object '"str"' returned by the "append()" method will be stringified with a call to its "text()" method. This will return the newly modified string content. In other words, a directive like: [% str.append('.html') %] will update the string and also print the new value. If you just want to update the string but not print the new value then use "CALL". [% CALL str.append('.html') %] The other benefit of these methods returning a reference to the "String" is that you can chain as many different method calls together as you like. For example: [% String.append('.html').trim.format(href) %] Here are the methods: push($suffix, ...) / append($suffix, ...) Appends all arguments to the end of the string. The "append()" method is provided as an alias for "push()". [% msg.push('foo', 'bar') %] [% msg.append('foo', 'bar') %] pop($suffix) Removes the suffix passed as an argument from the end of the String. [% USE String 'foo bar' %] [% String.pop(' bar') %] # => 'foo' unshift($prefix, ...) / prepend($prefix, ...) Prepends all arguments to the beginning of the string. The "prepend()" method is provided as an alias for "unshift()". [% msg.unshift('foo ', 'bar ') %] [% msg.prepend('foo ', 'bar ') %] shift($prefix) Removes the prefix passed as an argument from the start of the String. [% USE String 'foo bar' %] [% String.shift('foo ') %] # => 'bar' left($pad) If the length of the string is less than $pad then the string is left formatted and padded with spaces to $pad length. [% msg.left(20) %] right($pad) As per left() but right padding the "String" to a length of $pad. [% msg.right(20) %] center($pad) / centre($pad) As per left() and right() but formatting the "String" to be centered within a space padded string of length $pad. The "centre()" method is provided as an alias for "center()". [% msg.center(20) %] # American spelling [% msg.centre(20) %] # European spelling format($format) Apply a format in the style of "sprintf()" to the string. [% USE String("world") %] [% String.format("Hello %s ") %] # => "Hello World " upper() Converts the string to upper case. [% USE String("foo") %] [% String.upper %] # => 'FOO' lower() Converts the string to lower case [% USE String("FOO") %] [% String.lower %] # => 'foo' capital() Converts the first character of the string to upper case. [% USE String("foo") %] [% String.capital %] # => 'Foo' The remainder of the string is left untouched. To force the string to be all lower case with only the first letter capitalised, you can do something like this: [% USE String("FOO") %] [% String.lower.capital %] # => 'Foo' chop() Removes the last character from the string. [% USE String("foop") %] [% String.chop %] # => 'foo' chomp() Removes the trailing newline from the string. [% USE String("foo ") %] [% String.chomp %] # => 'foo' trim() Removes all leading and trailing whitespace from the string [% USE String(" foo ") %] [% String.trim %] # => 'foo' collapse() Removes all leading and trailing whitespace and collapses any sequences of multiple whitespace to a single space. [% USE String(" foo bar ") %] [% String.collapse %] # => "foo bar" truncate($length, $suffix) Truncates the string to $length characters. [% USE String('long string') %] [% String.truncate(4) %] # => 'long' If $suffix is specified then it will be appended to the truncated string. In this case, the string will be further shortened by the length of the suffix to ensure that the newly constructed string complete with suffix is exactly $length characters long. [% USE msg = String('Hello World') %] [% msg.truncate(8, '...') %] # => 'Hello...' replace($search, $replace) Replaces all occurences of $search in the string with $replace. [% USE String('foo bar foo baz') %] [% String.replace('foo', 'wiz') %] # => 'wiz bar wiz baz' remove($search) Remove all occurences of $search in the string. [% USE String('foo bar foo baz') %] [% String.remove('foo ') %] # => 'bar baz' repeat($count) Repeats the string $count times. [% USE String('foo ') %] [% String.repeat(3) %] # => 'foo foo foo ' AUTHOR
Andy Wardley <abw@wardley.org> <http://wardley.org/> COPYRIGHT
Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. SEE ALSO
Template::Plugin perl v5.14.2 2011-12-20 Template::Plugin::String(3pm)
All times are GMT -4. The time now is 07:58 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy