#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Saving items in a string, and same table lines.


    Hello,

    I don't mean to sound need but I am having to huge problems with a krn-shell script, and I cannot find a way around them.

    The first of which is that I have a list of filenames in a directory and i need to save part of the filename to reference later on. First I need to save the first part (xxxx), and then the second part (yyyy) of filenames which look like this:

    NUMBERS.00xxxxxxxx.yyyyyy

    so barring the zeroes I need to save the xxxxx and yyyy parts seperately. I cannot do this with a basic find.

    Secondly, I need to scan a file for information on the same line of a table as a searched string, and then save the other information on the line, which i'm not even sure is possible.

    I have pretty much exhausted myself for trying to come up with ways to do this, including reg exp, and finding a new shell.

    Any help is thoroughly appreciated. Thank you.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,436
    Rep Power
    1688
    You say you have a list of filenames, is that a file that contains the list or is it a directory listing? In either case I assume it is just the filename, with no path.

    Code:
    for fname in $(...)
    do
      x_bit=$(echo "$fname" | awk -F\. '{print $2}' | cut -c2-)
      y_bit=$(echo "$fname" | awk -F\. '{print $3}')
    done
    Change the ... part to reflect how you are getting the list (ls -1 or cat filelist.txt, etc.)

    Your second thing confuses me, it sounds like all you need is a straight grep command, but there may be an issue that I do not follow:

    Code:
    grep "search string" The_file_to_be_searched.txt
    will return every line that contains the search string. To 'save' that line just assign the data to a variable (or direct the output to another file):
    Code:
    wanted_line=$(grep "search string" The_file_to_be_searched.txt)
    grep "search string" The_file_to_be_searched.txt > wanted_data.txt
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0
    Thank you, this is quite helpful. The filenames are listed in a directory.

    Its not necessarily searching for the file name in the first part. It is searching through a document, and each line contains 3 strings, which I am trying to hold as those variables, line by line, and search for through a different doc.

    The second would be a simple grep command, if I could format the grep to find one column of strings and replace it with another.

    Also, im having difficulty with a command that replaces one variable (string) with another. I tried to use a sed command, but this did not work.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,436
    Rep Power
    1688
    It may be the fact that it's half-past midnight at my end of things, but I am not following you that well ...

    Its not necessarily searching for the file name in the first part
    is not a very helpful phrase!

    Do I gather that you have a file (document) that contains, one each line, three 'strings'? This would be your XXXX_table.txt file that you mention in your other thread?

    From that file you want to extract these strings and then for each file in your chosen directory you wish to do a replace of one string for the other?
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0
    Exactly

    I have a .txt file, with 3 columns per line. For each line, i wish to take the first an third column as variables, then find the first line in a different directory comprised of filenames, search all of the filenames for the first column string, and replace it with the third column string.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,436
    Rep Power
    1688
    Excellent we are getting somewhere!
    Now, this search and replacement: do you mean, in effect, renaming the files found or altering some of their content?

    The three columns you have in your text file, what format - fixed column widths, or is each column comprised of a single word?
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0
    The first column of the .txt file read line for line is contained within one of the filenames in the directory. I would just like to rename the files in the directory by finding which filename in the directory has the first column as part of its name, and replace that with exactly the third column

    so the .txt file has rows made of three columns like this:

    XXXXXX YYYYY ZZZZZZ

    and the filenames in the directory, look like

    AAAAAA.00XXXXXX.XXXXXX

    I would like for the script to take XXXXXX and ZZZZZZ and then find all of the similar X's in the filenames in the directory and make them

    AAAAAAA.00ZZZZZZ.XXXXXX
    or
    AAAAAAA.00XXXXXX.ZZZZZZ

    and keep doing so until all of the rows in the file have been looped, and all of the XXXXX's in the directory filenames have been replaced with ZZZZZZ's

    I am sorry that I was so unclear before.

    After this, I would like to remove all of the leading zeroes from the directorie's filenames, but one step at a time.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,436
    Rep Power
    1688
    Okay - now we are cooking with gas. Hopefully! I think we have enough of a 'spec' to work something out for you.
    Not that it may matter, but is it possible that you may end up renaming the same file multiple times?

    You may get, depending on the contents of that file, plus the file names in the directory, different results (with the same start point) on how you go about processing the files and data. I'd be inclined to use the text file as the 'driver' of the process:

    Code:
    #!/bin/bash
    while read a b c
    do
       for fname in $(ls -1 *${a}* 2>/dev/null)
       do
          echo -e "Renaming $fname \c"
          new_name=$(echo "$fname" | sed "s/${a}/${c}/")
          echo -e "to $new_name \c"
          if [ -f $new_name ]
          then
             echo "already exists, no rename done"
          else
            mv $fname $new_name 2>/dev/null
            mv_cc=$?
            if [ $mv_cc -eq 0 ]
            then
               echo "ok"
            else
               echo "error: code $mv_cc"
            fi
          fi
       done
    done < XXXX_table.txt
    That should take care of the changing the names of the files, based on the content of the file. All you need do is to ensure that it looks into the correct directory and acts upon the directory.
    The removing of the 00 part of all the files can be done with:
    Code:
    for fname in $(ls -1 *.00* 2>/dev/null)
    do
      new_name=$(echo "$fname" | sed 's/.00/./')
      mv $fname $new_name
    done
    I strongly recommend commenting out the mv commands to test things first!
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo