|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
That's not perl..it's a shell script...!!!
|
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
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
|
|||
|
|||
|
Just ${i} instead of ${i%.*} .
For information on what that means, see String Operations in the Advanced BASH Scripting Guide. |
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|