Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-28-2012
Registered User
 
Join Date: Jun 2011
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Changing text in multiple files, but with different text for each file

Hello, I have a situation where I want to change a line of text in multiple files, but the problem is that I want to change the text to something unique for each file.

For example, let's say I have five files named bob.txt, joe.txt, john.txt, tom.txt, and zach.txt. Each of these files has a line (1st line) with the text "name = xxx". For each file, I want to replace the "xxx" with the name in each file name, such that bob.txt has the first line changed to "name = bob", the first line in joe.txt is changed to "name = joe", etc. I have some experience with sed and grep, but I'm not sure how I would use those in this situation. Thanks.
Sponsored Links
    #2  
Old 06-28-2012
elixir_sinari's Avatar
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,370
Thanks: 87
Thanked 476 Times in 456 Posts

Code:
for i in bob.txt joe.txt john.txt tom.txt zach.txt
do
 sed '1s/name = xxx/name = '"${i%.*}"'/' $i > $$
 mv $$ $i
done

Sponsored Links
    #3  
Old 06-28-2012
Registered User
 
Join Date: Jun 2011
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
I tried this and got this message:

Missing $ on loop variable at chgnm.pl line 3

I made five files named like I said above and put name = xxx in each file, then I put what you typed into a perl script called "chgnm.pl", and that was the message I got.
    #4  
Old 06-28-2012
elixir_sinari's Avatar
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,370
Thanks: 87
Thanked 476 Times in 456 Posts
Quote:
Originally Posted by Scatterbrain26 View Post
I tried this and got this message:

Missing $ on loop variable at chgnm.pl line 3

I made five files named like I said above and put name = xxx in each file, then I put what you typed into a perl script called "chgnm.pl", and that was the message I got.
That's not perl..it's a shell script...!!!
Sponsored Links
    #5  
Old 06-28-2012
Registered User
 
Join Date: Jun 2011
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Oh... shit. I'm still a n00b, haha. I changed it to bash and it worked, thanks.

---------- Post updated at 03:51 PM ---------- Previous update was at 03:20 PM ----------

Ok, so how would I modify this is I wanted to change the text to "name = bob.txt" instead of just "name = bob"?
Sponsored Links
    #6  
Old 06-28-2012
Mead Rotor
 
Join Date: Aug 2005
Location: Saskatchewan
Posts: 16,374
Thanks: 491
Thanked 2,535 Times in 2,418 Posts
Just ${i} instead of ${i%.*} .

For information on what that means, see String Operations in the Advanced BASH Scripting Guide.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to grep multiple lines from a text file using another text file? evelibertine UNIX for Dummies Questions & Answers 5 06-27-2012 03:05 PM
[bash help]Adding multiple lines of text into a specific spot into a text file cdn_humbucker Shell Programming and Scripting 2 03-06-2010 02:11 AM
Create multiple text file from a single text file on AIX lodhi1978 Shell Programming and Scripting 11 11-19-2009 12:14 AM
read list of filenames from text file and remove these files in multiple directories fxvisions Shell Programming and Scripting 5 08-07-2008 03:59 PM
grep multiple text files in folder into 1 text file? coppertone UNIX for Dummies Questions & Answers 7 08-23-2002 02:50 PM



All times are GMT -4. The time now is 04:58 PM.