The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep a string from 2 text files karthikn7974 Shell Programming and Scripting 15 04-24-2008 03:15 AM
grep string and output filename happyv Shell Programming and Scripting 3 11-20-2007 12:16 AM
appending string to text file based on search string malaymaru Shell Programming and Scripting 1 06-09-2006 08:53 AM
fixing the error message when grep doesn't finds the pattern. silver123 UNIX for Dummies Questions & Answers 1 03-03-2006 05:59 AM
Appending Text To Each Line That Matches Grep sysera Shell Programming and Scripting 4 06-16-2005 01:19 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-06-2005
HLee1981 HLee1981 is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 35
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.

Any hints?

Thanks.
  #2 (permalink)  
Old 09-06-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
#!/bin/ksh

file='A.fileA'
pre='ABC'
post='1234'
pattern="${pre}.*${post}"

[[ $(egrep -c "${pattern}" "${file}") -ge 1 ]] && mv "${file} "${post}_FileChecked_${file}"

Last edited by vgersh99; 09-06-2005 at 02:38 PM..
  #3 (permalink)  
Old 09-06-2005
HLee1981 HLee1981 is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 35
Thanks.

For the line reading "file = 'A.fileA', if there are multiple files I want to apply the mentioned task to, how would the value for that field be filled?

I guess the same question applies for the post value as well, since that value would be different from file to file.

Thanks!
  #4 (permalink)  
Old 09-06-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by HLee1981
Thanks.

For the line reading "file = 'A.fileA', if there are multiple files I want to apply the mentioned task to, how would the value for that field be filled?
how do get a list of the files to process?
you'll need to put a 'loop' around the statement to iterate through all the needed files. For all the files in the current directory:
Code:
#!/bin/ksh

pre='ABC'
post='1234'
pattern="${pre}.*${post}"

find . ! -name . -prune -type f | while read file
do
   [[ $(egrep -c "${pattern}" "${file}") -ge 1 ]] && mv "${file} "${post}_FileChecked_${file}"

done
Quote:
Originally Posted by HLee1981
I guess the same question applies for the post value as well, since that value would be different from file to file.

Thanks!
How would this value change in conjunction with the applicable files?
Sponsored Links
Closed Thread

Bookmarks

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:27 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0