Sponsored Content
Top Forums Shell Programming and Scripting replacing text in file1 with list from file2 Post 302217540 by summer_cherry on Wednesday 23rd of July 2008 02:52:56 AM
Old 07-23-2008
Hope below can help you.

Below perl demo is to replace this with a array file by file. If reach out of the array, then stop replace.


Code:
@arr=(This,THis,THIS);
$n=0;
opendir(DIRECTORY, '.') or die "Can't open current directory.";
@files=readdir(DIRECTORY);
closedir DIRECTORY;
for($i=2;$i<$#files;$i++){
print "Begin change file:",$files[$i],"\n";
open FH,"<$files[$i]";
while(<FH>){
if($arr[$n]){
$_=~s/this/$arr[$n]/;
}
print $_;
}
close(FH);
print "End change file:",$files[$i],"\n\n";
$n++;
}



below awk demo is to replace this with value in another replace file file by file, if reach out of the end of replace file, will use the last one to replace the rest of file.
Code:
n=0
for i in *
do
        temp=""
        n=`expr $n + 1`
        temp=`cat ../replace | head -"$n" |tail -1`
        cat $i | nawk -v t="$temp" '{
                gsub(/this/,t,$0)
                print $0
        }'
done


Last edited by summer_cherry; 07-23-2008 at 04:36 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - replacing stings in file1 with column1 in file2

Hello, I've never used awk before, but from what I've read, it will best suit what I'm trying to do. I have 2 files. I need to replace strings in file1 with the first column of a matching string in file2. Below are examples: File1: random-string1 1112 1232 3213 2131 random-string2... (7 Replies)
Discussion started by: upstate_boy
7 Replies

2. Shell Programming and Scripting

append text from file1 to the end of each line in file2

hi; my file2.txt:portname=1;list=10.11;l- portname=2;list=10.12;l- portname=3;list=10.13;l- ... my file1.txt:;"{'sector=%27'}"\&> so; i want to see:portname=1;list=10.11;l-;"{'sector=%27'}"\&> portname=2;list=10.12;l-;"{'sector=%27'}"\&> portname=3;list=10.13;l-;"{'sector=%27'}"\&>... (4 Replies)
Discussion started by: gc_sw
4 Replies

3. UNIX for Dummies Questions & Answers

Delete strings in file1 based on the list of strings in file2

Hello guys, should be a very easy questn for you: I need to delete strings in file1 based on the list of strings in file2. like file2: word1_word2_ word3_word5_ word3_word4_ word6_word7_ file1: word1_word2_otherwords..,word3_word5_others... (7 Replies)
Discussion started by: roussine
7 Replies

4. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

5. Shell Programming and Scripting

Search within file1 numbers from list in file2

Hello to all, I hope somebody could help me with this: I have this File1 (real has 5 million of lines): Number Category --------------- -------------------------------------- 8734060355 3 8734060356 ... (6 Replies)
Discussion started by: Ophiuchus
6 Replies

6. Shell Programming and Scripting

Pattern Matching & replacing of content in file1 with file2

I have file 1 & file 2 with content mentioned below. I want to get the output as shown in file3. Requirement: check the content of column 1 & column 2, if value of column 1 in file1 matches with first column of file2 then remaining columns(2&3) of file2 should get replaced, also if value of... (4 Replies)
Discussion started by: siramitsharma
4 Replies

7. Shell Programming and Scripting

Replacing first field of file2 with the second filed of file1 for matching cases

Dear All, Need your help..:D I am not regular on shell scripts..:( I have 2 files.. Content of file1 cellRef 4};"4038_2_MTNL_KALAMBOLI" cellRef 1020};"4112_3_RAINBOW_BLDG" cellRef 134};"4049_2_TATA_HOSPITAL" cellRef 1003};"4242_3_HITESH_CONSTRUCTION" cellRef... (6 Replies)
Discussion started by: ailnilanjan
6 Replies

8. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

9. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

10. Shell Programming and Scripting

Awk- Indexing a list of numbers in file2 to print certain rows in file1

Hi Does anyone know of an efficient way to index a column of data in file2 to print the coresponding row in file1 which corresponds to the data in file2 AND 30 rows preceding and after the row in file1. For example suppose you have a list of numbers in file2 (single column) as follows:... (6 Replies)
Discussion started by: Geneanalyst
6 Replies
DIRECTORY(3)						     Library Functions Manual						      DIRECTORY(3)

NAME
opendir, readdir, telldir, seekdir, rewinddir, closedir - directory operations SYNOPSIS
#include <sys/types.h> #include <sys/dir.h> DIR *opendir(filename) char *filename; struct direct *readdir(dirp) DIR *dirp; long telldir(dirp) DIR *dirp; seekdir(dirp, loc) DIR *dirp; long loc; rewinddir(dirp) DIR *dirp; closedir(dirp) DIR *dirp; DESCRIPTION
Opendir opens the directory named by filename and associates a directory stream with it. Opendir returns a pointer to be used to identify the directory stream in subsequent operations. The pointer NULL is returned if filename cannot be accessed, or if it cannot malloc(3) enough memory to hold the whole thing. Readdir returns a pointer to the next directory entry. It returns NULL upon reaching the end of the directory or detecting an invalid seekdir operation. Telldir returns the current location associated with the named directory stream. Seekdir sets the position of the next readdir operation on the directory stream. The new position reverts to the one associated with the directory stream when the telldir operation was performed. Values returned by telldir are good only for the lifetime of the DIR pointer from which they are derived. If the directory is closed and then reopened, the telldir value may be invalidated due to undetected direc- tory compaction. It is safe to use a previous telldir value immediately after a call to opendir and before any calls to readdir. Rewinddir resets the position of the named directory stream to the beginning of the directory. Closedir closes the named directory stream and frees the structure associated with the DIR pointer. Sample code which searchs a directory for entry ``name'' is: len = strlen(name); dirp = opendir("."); for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) if (dp->d_namlen == len && !strcmp(dp->d_name, name)) { closedir(dirp); return FOUND; } closedir(dirp); return NOT_FOUND; SEE ALSO
open(2), close(2), read(2), lseek(2), dir(5) 4.2 Berkeley Distribution September 24, 1985 DIRECTORY(3)
All times are GMT -4. The time now is 09:48 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy