Script for replacing text in a file based on list


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Script for replacing text in a file based on list
# 1  
Old 09-29-2010
Question Script for replacing text in a file based on list

Hi All,
I am fairly new to the world of Unix, and I am looking for a way to replace a line of text in a file with a delimited array of values.
I have an aliases file that is currently in use on our mail server that we are migrating off of. Until the migration is complete, the server must stay up. I need to migrate approximately 50 people per day. In order to do this, I must replace lines in my aliases file without disturbing the distribution lists.
For example, I have a list of users as such:
user1: id1
user2: id2
user3: id3
user4: user2
distributionlist: user1,user3
I want to search that file based on the id# and comment that line out. I then want to insert a line directly underneath it as such:
#user1: id1
user1: alias1@domain.com
The domain is not static, and that is why I would need to reference an array (tab delimited or such).
I would prefer to create a backup of the original file with a rather than output to a new filename as I think this may be easier than changing the script that puts the file into production.
I know this probably isn't the best explanation and I may be missing some info. Thanks in advance!
# 2  
Old 09-29-2010
so you have these files:

Code:
file1

user1: id1
user2: id2
user3: id3
user4: user2
distributionlist: user1,user3

file2 

user1: alias1@domain.com

expect output:

#user1: id1
user1: alias1@domain.com
user2: id2
user3: id3
user4: user2
distributionlist: user1,user3

Confirm first.
# 3  
Old 09-29-2010
That is very close, but to avoid issue I am probably going to have file 2 as:
id1: alias1@domain.com
I want to avoid any possibility that we would accidentally edit a distribution list which may contain a "user#" entry, since the "id#" value is going to be unique.
# 4  
Old 09-29-2010
Something like this?
Code:
awk -F": " '
NR==FNR{a[$1]=$2; next} 
a[$2]{$0= "#" $0 RS $1 FS a[$2]}
1' file2 file1

# 5  
Old 09-29-2010
That looks like it is on the right track. I tried it with a couple test files, but it didn't do what it was supposed to.
I think you may need a bit more info about the file. After each : there is a tab, not a space. Some times there are two tabs, sometimes there is a space after the tab, but each : should be immediately followed by at least a tab.

I really appreciate the assistance. Thanks!
# 6  
Old 09-29-2010
Try this:
Code:
awk -F":[ \t]+" '
NR==FNR{a[$1]=$2; next} 
a[$2]{$0= "#" $0 RS $1 ": " a[$2]}
1' file2 file1

# 7  
Old 09-29-2010
I have tried the adjusted code but have not had any luck on my test files. It doesn't seem to be doing the editing, but it does appear to go through the file.
If you could explain what each item is doing, I may be able to figure this out with a little more assistance.

Thanks again!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Deletion of list of user based on a text file In LDAP UNIX server

Dear All, It would be really nice, if you could help me to write a script for deletion of list of user( more than 15000 users) stored in a file and sorted by email address( i need deletion of only a particular type of mail address). Is the any script to write and take the file as input and... (3 Replies)
Discussion started by: Chand
3 Replies

2. UNIX for Dummies Questions & Answers

Replacing stopwords based on a list

Dear all, I have Files with lines of text in them, I want to replace the stopwords in them with ",". I have create a file which contain the stopwords... I have been trying for last 3 hours but no success I have managed to replace one using "sed" and delete the line containing them using... (3 Replies)
Discussion started by: A-V
3 Replies

3. Shell Programming and Scripting

Replacing the text in a row based on certain condition

Hi All, I felt tough to frame my question. Any way find my below input. (.CSV file) SNo, City 1, Chennai 2, None 3, Delhi 4,None Note that I have many rows ans also other columns beside my City column. What I need is the below output. SNo, City 1, Chennai 2, Chennai_new 3, Delhi... (2 Replies)
Discussion started by: ks_reddy
2 Replies

4. UNIX for Dummies Questions & Answers

print multiple lines from text file based on pattern list

I have a text file with a list of items/patterns: ConsensusfromCGX_alldays_trimmedcollapsedfilteredreadscontiglist(229095contigs)contig12238 ConsensusfromCGX_alldays_trimmedcollapsedfilteredreadscontiglist(229095contigs)contig34624... (1 Reply)
Discussion started by: Oyster
1 Replies

5. Shell Programming and Scripting

Replacing headers based on a second file

I have a file with thousands of sequences that looks like this: I need to replace the headers using a second file Thus, I will end up having the following file: I am looking for an AWK script that I can easily plug in my current pipeline. Any help will be greatly appreciated! (6 Replies)
Discussion started by: Xterra
6 Replies

6. Shell Programming and Scripting

To run a script based on the value in text file

I have a Text file as shown below /* text file begins---------- ----------- Monthly files Loaded ------------- input_file record_count load_count reject_count ------------ ----------- ----------- ----------- 1_IN.txt 221935 221935 0 2_IN.txt 270668 270668 0 3_IN.TXT 231666 80370 151296... (7 Replies)
Discussion started by: nani1984
7 Replies

7. Shell Programming and Scripting

Delete block of text in one file based on list in another file

Hi all I currently use the following in shell. #!/bin/sh while read LINE do perl -i -ne "$/ = ''; print if !m'Using archive: ${LINE}'ms;" "datafile" done < "listfile" NOTE the single quote delimiters in the expression. It's highly likely the 'LINE' may very well have characters in it... (3 Replies)
Discussion started by: Festus Hagen
3 Replies

8. Shell Programming and Scripting

Replacing text based on replacement tables

Dear all, will be grateful for your advices.. The need is (i guess) simple for UNIX experts. Basically, there are replacement tables, which would be used to replace text strings in the data (large volumes..). An exmpl table (a "config file"): VIFIS1_1_PE1836 VIBRIO_FISCHERI VIPAR1_1_PE1662 ... (7 Replies)
Discussion started by: roussine
7 Replies

9. Shell Programming and Scripting

Bash script to delete folder based on text file information

I have been working on a script to list all the name's of a subfolder in a text file then edit that text file and then delete the subfolder base on the edited text file so far I have been able to do every thing I just talked about but can't figure out how to delete the subfolers base on a text file... (8 Replies)
Discussion started by: bone11409
8 Replies

10. Shell Programming and Scripting

replacing text in file1 with list from file2

I am trying to automate a process of searching through a set of files and replace all occurrences of a formatted text with the next item in the list of a second file. Basically i need to replace all instances of T????CLK???? with an IP address from a list in a second file. the second file is one IP... (9 Replies)
Discussion started by: dovetail
9 Replies
Login or Register to Ask a Question