Changing text in multiple files, but with different text for each file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Changing text in multiple files, but with different text for each file
# 1  
Old 06-28-2012
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.
# 2  
Old 06-28-2012
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

# 3  
Old 06-28-2012
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
Quote:
Originally Posted by Scatterbrain26
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...!!!
# 5  
Old 06-28-2012
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"?
# 6  
Old 06-28-2012
Just ${i} instead of ${i%.*} .

For information on what that means, see String Operations in the Advanced BASH Scripting Guide.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying a file to multiple other files using a text file as input

Hello, I have a file called COMPLIST as follows that contains 4 digit numbers.0002 0003 0010 0013 0015 0016 0022 0023 0024 0025 0027 0030 0031 0032 0033 0035 0038 0041 (3 Replies)
Discussion started by: sph90457
3 Replies

2. UNIX for Dummies Questions & Answers

Pdftotext from multiple pdf files to a single text file

I have a directory having a number of pdf files. I want to convert all the files to text, stored in a single text file The following creates multiple text files ls *.pdf | xargs -n1 pdftotext (1 Reply)
Discussion started by: kristinu
1 Replies

3. Shell Programming and Scripting

Split a text file into multiple text files?

I have a text file with entries like 1186 5556 90844 7873 7722 12 7890.6 78.52 6679 3455 9867 1127 5642 ..N so many records like this. I want to split this file into multiple files like cluster1.txt, cluster2.txt, cluster3.txt, ..... clusterN.txt. (4 Replies)
Discussion started by: sammy777
4 Replies

4. Shell Programming and Scripting

Merge the multiple text files into one file

Hi All, I am trying to merge all the text files into one file using below snippet cat /home/Temp/Test/Log/*.txt >> all.txt But it seems it is not working. I have multiple files like Output_ServerName1.txt, Output_ServreName2.txt I want to merge each file into one single file and... (6 Replies)
Discussion started by: sharsour
6 Replies

5. UNIX for Dummies Questions & Answers

Splitting up a text file into multiple files by columns

Hi, I have a space delimited text file with multiple columns 102 columns. I want to break it up into 100 files labelled 1.txt through 100.txt (n.txt). Each text file will contain the first two columns and in addition the nth column (that corresponds to n.txt). The third file will contain the... (1 Reply)
Discussion started by: evelibertine
1 Replies

6. UNIX for Dummies Questions & Answers

How to grep multiple lines from a text file using another text file?

I would like to use grep to select multiple lines from a text file using a single-column text file. Basically I want to only select lines from the first text file where the second column of the first text file matches the second text file. How do I go about doing that? Thanks! (5 Replies)
Discussion started by: evelibertine
5 Replies

7. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

8. Shell Programming and Scripting

Create multiple text file from a single text file on AIX

Hi I need to create multiple text files from onc text file on AIX. The data of text files is as below: ********************************************** ********************************************** DBVERIFY: Release 10.2.0.4.0 - Production on Tue Nov 10 13:45:42 2009 Copyright (c) 1982,... (11 Replies)
Discussion started by: lodhi1978
11 Replies

9. Shell Programming and Scripting

read list of filenames from text file and remove these files in multiple directories

I have a large list of filenames from an Excel sheet, which I then translate into a simple text file. I'd like to use this list, which contains various file extensions , to archive these files and then remove them recursively through multiple directories and subdirectories. So far, it looks like... (5 Replies)
Discussion started by: fxvisions
5 Replies

10. UNIX for Dummies Questions & Answers

grep multiple text files in folder into 1 text file?

How do I use the grep command to take mutiple text files in a folder and make one huge text file out of them. I'm using Mac OS X and can not find a text tool that does it so I figured I'd resort to the BSD Unix CLI for a solution... there are 5,300 files that I want to write to one huge file so... (7 Replies)
Discussion started by: coppertone
7 Replies
Login or Register to Ask a Question