UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 23rd, 2011, 02:07 PM
jgrosecl jgrosecl is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2011
Posts: 7 jgrosecl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 39 m 15 sec
Reputation 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.

Reply With Quote
  #2  
Old June 23rd, 2011, 04:54 PM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 48 m 25 sec
Reputation Power: 1485
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

Reply With Quote
  #3  
Old June 30th, 2011, 08:22 AM
jgrosecl jgrosecl is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2011
Posts: 7 jgrosecl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 39 m 15 sec
Reputation 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.

Reply With Quote
  #4  
Old June 30th, 2011, 06:36 PM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 48 m 25 sec
Reputation Power: 1485
It may be the fact that it's half-past midnight at my end of things, but I am not following you that well ...

Quote:
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?

Reply With Quote
  #5  
Old July 1st, 2011, 08:26 AM
jgrosecl jgrosecl is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2011
Posts: 7 jgrosecl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 39 m 15 sec
Reputation 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.

Reply With Quote
  #6  
Old July 1st, 2011, 08:46 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 48 m 25 sec
Reputation Power: 1485
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?

Reply With Quote
  #7  
Old July 1st, 2011, 09:21 AM
jgrosecl jgrosecl is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2011
Posts: 7 jgrosecl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 39 m 15 sec
Reputation 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.

Reply With Quote
  #8  
Old July 1st, 2011, 04:52 PM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 48 m 25 sec
Reputation Power: 1485
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Saving items in a string, and same table lines.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap