Grep and replace multiple strings in a file with multiple filenames in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep and replace multiple strings in a file with multiple filenames in a file
# 1  
Old 07-24-2013
Grep and replace multiple strings in a file with multiple filenames in a file

Hi,

I have a file containing list of strings like

i:
Pink
Yellow
Green

and I have file having list of file names in a directory

j :
a
b
c
d

Where j contains of a ,b,c,d are as follows
a:
Pink
White

b
Yellow
Pink

c
Pink
Yellow
Green

d
Red
white

My intention is to check all the strings in i for all the file names in j and if that strings are present in any of listed files in j those strings should be modified as new_<string> instead of <string> in original files

o/p :

a:
new_Pink
White

b
new_Yellow
new_Pink

c
new_Pink
new_Yellow
new_Green

d
Red
white

Thanks
# 2  
Old 07-24-2013
Please use code tags for posting code fragments or data samples!

Show us what you have tried so far.
# 3  
Old 07-24-2013
Use sed to make lines of file i into a sed script lines: s/color/new_color/. Call that script with all files in j, using sed -i.
# 4  
Old 07-24-2013
Please use code tags as required by forum rules!

Try this and rename new_filenames to original filenames afterwards:
Code:
awk     'NR==FNR        {R[$1]; next}
         $1 in R        {$1="new_"$1}
                        {print >"new_"FILENAME}
        ' i $(cat j)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep multiple patterns(file) and replace whole line

I am able to grep multiple patterns which stored in a files. However, how could we replace the whole line with either the pattern or new string? For example: pattern_file: *Info in the () is not part of the pattern file. They are the intended name to replace the whole line after the pattern... (5 Replies)
Discussion started by: wxboo
5 Replies

2. Shell Programming and Scripting

Replace multiple strings of a file

Hi, I have an array variable "arr" that reads string from a file "vari.txt". Thus, the array will be of variable length depending how many entries are present in "vari.txt" I use a for loop to traverse through the array. vari.txt (in this sample we have 2 entries, but it can have more... (5 Replies)
Discussion started by: mohtashims
5 Replies

3. Shell Programming and Scripting

Grep multiple strings in a file

Consider i have the below data in my log file. i want to grep using "Monday" and "Working" So the only output i expect is Can you help me with the grep query for Sun Sparc ? Usage: grep -hblcnsviw pattern file . . . (8 Replies)
Discussion started by: mohtashims
8 Replies

4. Shell Programming and Scripting

Grep from multiple patterns multiple file multiple output

Hi, I want to grep multiple patterns from multiple files and save to multiple outputs. As of now its outputting all to the same file when I use this command. Input : 108 files to check for 390 patterns to check for. output I need to 108 files with the searched patterns. Xargs -I {} grep... (3 Replies)
Discussion started by: Diya123
3 Replies

5. Shell Programming and Scripting

Grep multiple strings in multiple files

Hi, every one! I have a file with multiple strings. file1 ATQRGNE ASQGVKFTE ASSQYRDRGGLET SPEQGARSDE ASSRDFTDT ASSYSGGYE ASSYTRLWNTGE ASQGHNTD PSLGGGNQPQH SLDRDSYNEQF I want to grep each string in hundreds of files in the same directory, further, I want to find out the string... (7 Replies)
Discussion started by: xshang
7 Replies

6. Shell Programming and Scripting

Search & Replace: Multiple Strings / Multiple Files

I have a list of files all over a file system e.g. /home/1/foo/bar.x /www/sites/moose/foo.txtI'm looking for strings in these files and want to replace each occurrence with a replacement string, e.g. if I find: '#@!^\&@ in any of the files I want to replace it with: 655#@11, etc. There... (2 Replies)
Discussion started by: spacegoose
2 Replies

7. Shell Programming and Scripting

replace a string with contents of a txt file containing multiple lines of strings

Hello everyone, ive been trying to replace a string "kw01" in an xml file with the contents of a txt file having multiple lines. im a unix newbie and all the sed combinations i tried resulted to being garbled. Below is the contents of the txt file: RAISEDATTIME --------------------... (13 Replies)
Discussion started by: 4dirk1
13 Replies

8. UNIX for Dummies Questions & Answers

Grep multiple strings in multiple files using single command

Hi, I will use below command for grep single string ("osuser" is search string) ex: find . -type f | xarg grep -il osuser but i have one more string "v$session" here i want to grep in which file these two strings are present. any help is appreciated, Thanks in advance. Gagan (2 Replies)
Discussion started by: gagan4599
2 Replies

9. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

10. Shell Programming and Scripting

Replace multiple strings in a file.

Hello Freinds, Hope, you all are doing well. I have to replace the static strings from one file (File 1) with the dynamic strings from the another file (File2). I've written a shell script for this.Below is the contents. while read line do field=`echo $line | awk '{print $1}'` ... (9 Replies)
Discussion started by: singh.chandan18
9 Replies
Login or Register to Ask a Question