Sponsored Content
Full Discussion: Manipulating Filenames
Top Forums Shell Programming and Scripting Manipulating Filenames Post 302571218 by agama on Sunday 6th of November 2011 09:57:01 PM
Old 11-06-2011
Quote:
Originally Posted by imonkey
Thanks heaps agama that has worked a treat. I can barley understand it but I'll work on it a bit more. Quick question, why does it need to be piped to the kshell?

Not sure if your question was literally why is must be piped to a shell or if you meant must it be kshell -- would bash work. So, here are both answers:

The awk is generating the move commands, but needs kshell to execute them. It could be piped to bash, I just prefer Kshell so that's the way I tested it.

I'll add some comments to the code and maybe that will help you understand it a bit better.

---------- Post updated at 21:57 ---------- Previous update was at 21:45 ----------

Some additional info:
Code:
ls VOD[0-9]*.pdf | awk '
    NF > 1 {        # input line will have more than one field if filename has spaces
        of = $0;                    # save the original filename
        split( $NF, a, "." );       # split the last part of filename into array a using dot as seperator
        n = split( $0, b, " " );    # easy way to get all of the fields into an array
                                    # we put the fields into an array so we can treat both cases identically later
        b[n] = a[1];                # replace last field xxxx.yyyy with just xxxx
        sep = " ";                  # seperator to use when building the move to file
    }

    NF == 1 {                       # if just one field, asssume a filename without spaces
        of = $1;                    # save the original name
        split( $1, a, "." );        # split the name (xxx_yyy_zzzz.eee) on the dot xxx_yyy_zzzz goes into a[1] eee into a[2]
        n = split( a[1], b, "_" );  # split the leading lead part into array b using _ as separator
        sep = "_";                  # seperator to use when building the move to file
    }

      {                               # this block executed for all files; assumes array b has the filename components and n is the size of b
        printf( "mv \"%s\"  \"%s", of, b[n] );      # print command (mv) original name and  the last component of the name
          for( i = 2; i < n; i++ )   # starting with second component in the name print up to, but not including the last component
            printf( "%s%s", sep, b[i] );
        printf( ".%s\"\n", a[2] );      # add the extension (.xxx) and a newline
    }
' #| ksh

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

date manipulating

I'm using Kshell scripts cronjobs to logrolling my daily log files and I stamped the log file at the end of the day by date stamp i.e.: %d_%m_%y-LogFile. but the 22_01_2001-LogFile as example contains 21st Jan data and I want to stamp the log file with previous day. so I'm trying to subtract... (3 Replies)
Discussion started by: tamer
3 Replies

2. Solaris

Manipulating File

Help...please. I have a log that contains Warning Authentication Failed: User GHDT88998HS doesn't exit: The User GHDT88998HS could not be found Mar 22, 2008 5:22:22AM com.hometel.ttm.auth.userlogin. about maybe a thousand entries failed user acct message How can I grab just the username... (2 Replies)
Discussion started by: rivendell500
2 Replies

3. UNIX for Dummies Questions & Answers

string manipulating

I have a file like this: Tue Apr 15 10:41:47 MDT 2008 FINAL RESULT; 6 Tue Apr 15 10:41:47 MDT 2008 FINAL RESULT; 2 Tue Apr 15 10:41:47 MDT 2008 FINAL RESULT; 5 With this command seira=`cut -f 2 -d ';' tes.txt` i take all the results (6,2,5 etc) and i store them in variable seira When i do... (9 Replies)
Discussion started by: psalas
9 Replies

4. UNIX for Dummies Questions & Answers

Help!! manipulating file

Hi all, I need help manipulating the file below. Here is what I needed to do. First, I have to replace INSUPD to DELETE. Then I need to change the content of the file around by flipping the contents in the file from the bottom to the top (start from "CMD") How should I attack this? Here... (2 Replies)
Discussion started by: sirrtuan
2 Replies

5. Emergency UNIX and Linux Support

Manipulating Data

Hi. I haven't had to write bash scripts in a long time and have a simple task to do, but need some help: Input: chrY:22627291-22651542 chrY:23045932-23070172 chrY:23684890-23696359 chrY:25318610-25330083 chrY:25451096-25462570 chr10:1054847-1061799 chr10:1058606-1080131... (7 Replies)
Discussion started by: awknerd
7 Replies

6. Solaris

Manipulating TOP

I need the command top to output as: Memory: 2048M real, 1499M free, 53M swap in use, 5423M swap free on just the memory line. Instead, I have compiled the new version of top that displays as so: Memory: 2048M phys mem, 1499M free mem, 5476 total swap, 5423M swap free I read... (2 Replies)
Discussion started by: adelsin
2 Replies

7. Shell Programming and Scripting

Manipulating a file

Hi everybody, I need an urgent help with a BASH script. I have file which contains (besides the other data) the lines with the following structure identified by with keyword PCList: <PARAMETER NAME="PCList" TYPE="LIST_STRUCTURE" MODEL="{,}" ... (1 Reply)
Discussion started by: sameucho
1 Replies

8. Shell Programming and Scripting

need help on manipulating a file

Hi, I need a shell/command to achieve this task. I've a delimited unloaded file from oracle in a scrambled format as shown below with many blank lines in it, I'm just trying to tailor it in a format that would be compatible to view and load it to a IDS db. Here is the problem ... (1 Reply)
Discussion started by: divak
1 Replies

9. UNIX for Dummies Questions & Answers

Manipulating files

Hi Guys, I'm really new to Unix and Linux and other scripting languages but recently I hv been really enthusiatic about learning more to help out on my work. So I have a file with 3 columns. A sample of it looks like looks like this : K2_537841 AAATCAGCCGCAACATTTGC ... (7 Replies)
Discussion started by: pawannoel
7 Replies

10. Shell Programming and Scripting

Manipulating files

Not sure if the question posted in another forums can be moved by me.So posting the link here. https://www.unix.com/unix-advanced-expert-users/221425-shell-script-manipulate-files.html#post302795379 Need your help here. (1 Reply)
Discussion started by: vedanta
1 Replies
textutil::split(n)				    Text and string utilities, macro processing 				textutil::split(n)

__________________________________________________________________________________________________________________________________________________

NAME
textutil::split - Procedures to split texts SYNOPSIS
package require Tcl 8.2 package require textutil::split ?0.7? ::textutil::split::splitn string ?len? ::textutil::split::splitx string ?regexp? _________________________________________________________________ DESCRIPTION
The package textutil::split provides commands that split strings by size and arbitrary regular expressions. The complete set of procedures is described below. ::textutil::split::splitn string ?len? This command splits the given string into chunks of len characters and returns a list containing these chunks. The argument len defaults to 1 if none is specified. A negative length is not allowed and will cause the command to throw an error. Providing an empty string as input is allowed, the command will then return an empty list. If the length of the string is not an entire multiple of the chunk length, then the last chunk in the generated list will be shorter than len. ::textutil::split::splitx string ?regexp? This command splits the string and return a list. The string is split according to the regular expression regexp instead of a simple list of chars. Note that if you parentheses are added into the regexp, the parentheses part of separator will be added into the result list as additional element. If the string is empty the result is the empty list, like for split. If regexp is empty the string is split at every character, like split does. The regular expression regexp defaults to "[\t \r\n]+". BUGS, IDEAS, FEEDBACK This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category textutil of the Tcllib SF Trackers [http://sourceforge.net/tracker/?group_id=12883]. Please also report any ideas for enhancements you may have for either package and/or documentation. SEE ALSO
regexp(n), split(n), string(n) KEYWORDS
regular expression, split, string textutil 0.7 textutil::split(n)
All times are GMT -4. The time now is 04:39 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy