Sponsored Content
Top Forums Shell Programming and Scripting Fast way of find and replace, help Post 302173266 by jkl_jkl on Thursday 6th of March 2008 06:01:41 AM
Old 03-06-2008
Fast way of find and replace, help

Hi All,

I have nearly 200 files in a directory, each file is of nearly of 5000 lines.
Each line of each file is having its 3rd field to be replaced by corresponding replace_string(2nd field of file1.out below).

i.e.
-Search the 3rd field of each line of each of 200 files in file1.out, get the replace_string(2nd field of file1,out),
-replace that in each line(and in turn each file)

Code:
file1.out (is more than 5000 lines)
++++++++++++++++++++++++++++++++++++

1204675041      1204675051
1204675042      1204675063
1204675111      1204675142
1204675112      1204675154
1204675113      1204675166
1204675114      1204675177
1204675115      1204675189
1204675116      1204675200
<nearly 5000 more lines>

Code:
$ ls | while read FILE
> do
> awk -F "|" '{print $3}' $FILE | while read TIME
> do
> TORP=`awk '$1=="'"$TIME"'" {print $2}' file1.out`
> awk 'BEGIN{OFS=FS="|"} $3=="'"$TIME"'" {$3="'"$TORP"'"}{print}' $FILE > $FILE.tmp
> done
> done

I tried using bash with AWK to do the same(like above), the performance is not that good(its just hanging).

Could anyone suggest me a fast search and replace technique considering my scenario.

HTH,
jkl_jkl
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

find and replace

Hi In our html pages, we have the image path as "/dir1/dir2/image.gif". We are changing the location of the image directory. So now we wish to do a global search and replace the path. I think we are having trouble with forward slash character. Please help Thanks in advance Vikas a... (3 Replies)
Discussion started by: vikas_j@hotmail
3 Replies

2. Shell Programming and Scripting

find and replace

Hi, There are some "n" files in a directory which contains comman string.A command to find and replace the string in all the files without looping. like if i am in a directory : # find ./ -name ".txt" | xargs sed -e 's/test/tst' Upto here is performed correctly and i want to... (4 Replies)
Discussion started by: rakshit
4 Replies

3. Shell Programming and Scripting

find replace

Hi I am replacing some string occurances with empty string in all files under one directory using find ./ -name "*.dmp" | xargs perl -pi -e 's/\\N//g' | its taking too much time for replacing and redirecting to same file in the same directory. Similarly afterwards i am finding last... (2 Replies)
Discussion started by: dbsurf
2 Replies

4. Shell Programming and Scripting

fast search and replace in all files

Hi I need to find one string in all files and replace tht string with blank space and need to redirect all the files into the same directory again. now i am using find ./ -name "*.dmp" | xargs perl -pi -e 's/\\N//g' | sed 's/.$//g' but now its not redirrecting properly . its taking... (21 Replies)
Discussion started by: dbsurf
21 Replies

5. UNIX for Dummies Questions & Answers

Find and Replace

After running a command like grep -ir files2/ * This will find all the files that contain "files2/" in it. For example if it finds files2/dir/today files2/dir/yesterday files2/dir/2daysago Now it may find 100 instances, so is there a quick find and replace command I can use? I... (4 Replies)
Discussion started by: NycUnxer
4 Replies

6. UNIX for Advanced & Expert Users

Help in find and replace.

Hi All, I have the file in the following format.I need to change the password "tomcat","admin","mgr" and "testing" in the file with the encrypted passwords. The encrypted passwords are given to me by another script. <?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role... (22 Replies)
Discussion started by: nua7
22 Replies

7. Shell Programming and Scripting

find and replace and keep

Hi All I've file in which has these lines in it create fil23456 read on 3345 create fil23456_1 read on 34567 create fil23456_2 read on 36789 I'm trying to replace the lines in such a way that in the end the file will look like create fil23456 read on 3345 alter fil23456 read on... (3 Replies)
Discussion started by: Celvin VK
3 Replies

8. Shell Programming and Scripting

Find and replace

In a directory I have many XML files, how to search for the string <text>You are here</text> and replace it with <text>YOU Are HERE</bc-text> in a unix command find . -name "*.xml" (1 Reply)
Discussion started by: sandy1028
1 Replies

9. UNIX for Dummies Questions & Answers

find and replace

I have a tab-delimited inFile: cat inFile A B C D E F 1 2 3 4 5 6 a b c d e fI would like to replace the first 3 tabs in each row with underscore to get outFile: A_B_C_D E F 1_2_3_4 5 6 a_b_c_d e fhow can I modify the following... (5 Replies)
Discussion started by: jdhahbi
5 Replies

10. Shell Programming and Scripting

Fast processing(mv command) of 1 million+ files using find, mv and xargs

Hi, I'd like to ask if anybody can help improve my code to move 1 million+ files from a directory to another: find /source/dir -name file* -type f | xargs -I '{}' mv {} /destination/dir I learned this line of code from this forum as well and it works fine. However, file movement is kinda... (6 Replies)
Discussion started by: agentgrecko
6 Replies
join(1) 						      General Commands Manual							   join(1)

Name
       join - join files

Syntax
       join [ -a n] [ -e string] [ -j  n m] [ -o list] [ -t c]	file1 file2

Description
       The  command  compares a field in file1 to a field in file2.  If the two fields match, the command combines the line in file1 that contains
       the field with the line in file2 that contains the field.  The command writes its output to standard output.  If you specify a  hyphen  (-)
       in the file1 argument, compares standard input to the contents of file2.

       The command compares and combines the input files one line at a time. Each line in the input file contains one field that uses to determine
       if two lines should be joined.  This field is called the join field. By default, the command uses the first field in each line as the  join
       field.	The  command  compares	the join field in the first line of file1 to the join field in the first line of file2.  If the two fields
       match, the command joins the lines.  The command then compares the join fields in the second line of both files, and so on.

       In the input files, fields are separated by tab or space characters.  The command reads data from the first field until it encounters a tab
       or  space  character,  which  terminates the first field.   By default, the command ignores tab and space characters, so the next character
       that is not a tab or space begins the second field.  The second field is terminated by the tab or space that  follows  it,  and	the  third
       field begins with the next character that is not a tab or space.  The command reads fields in this way until it encounters a new line char-
       acter.  Any number of tabs or spaces can separate two fields, and any number of newline characters can separate two lines.

       Both file1 and file2 must be ordered in the collating sequence of the command on the fields that  the  two  files  are  to  be  joined.	By
       default, uses the first field in each line and collates the same as

       To  create  output,  the  command writes the join field, followed by the remaining fields in the line from file1, followed by the remaining
       fields in the line from file2 to the output file.  The following demonstrates how lines in the  output appear by default:
       join_field file1.field2 file1.field3 file1.field4 file2.field2 file2.field3

       By default, the command ignores lines that do not contain identical join fields.  The command writes no output for these lines.

       You can change how creates output using command options.  For example, you can cause the command to write output for lines that do not con-
       tain  identical	join  fields.	You  can  also	specify  a  list  using  the option.  In list, you supply a list of specifiers in the form
       file.field, where file is either 1 or 2 and field is the number of the field.  For example, 1.2 specifies the second  field  in	the  first
       file  and 2.4 specifies the fourth field in the second file. The following demonstrates how lines in the output appear if you use these two
       specifiers:
       file1.field2 field2.field4

   International Environment
       LC_COLLATE     If this environment variable is set and valid, uses the international language database named in the definition to determine
		      collation rules.

       LC_CTYPE       If this environment variable is set and valid, uses the international language database named in the definition to determine
		      character classification rules.

       LANG	      If this environment variable is set and valid uses the international language database named in the definition to  determine
		      collation  and character classification rules.  If LC_COLLATE or LC_CTYPE is defined their definition supercedes the defini-
		      tion of LANG.

Options
       -a[n]	   Write lines that contain unmatched join fields to the output file.  You can cause the command to  write  unmatched  lines  from
		   only  one  file  using  n.  If you specify 1 in n, writes unmatched lines only from file 1.	If you specify 2, writes unmatched
		   lines only from file 2.

		   If you omit the option, writes no output for unmatched lines.

       -e s	   Writes the string you specify in s to the output if you specify a nonexistent field in the list for the option.   For  example,
		   if lines in file 2 contain only three fields, and you specify 2.4 in list, writes s in place of the nonexistent field.

       -jn m	   Defines  field  m  in file n to be the join field. The command compares the field you specify in the option to the default join
		   field in the other file.  If you omit n, the command uses the mth field in both files.

       -1 m	   Use the m th field in the first file as the join field.  This option is equivalent to using m.

       -2 m	   Use the m field in the second file as the join field.  This option is equivalent to using m.

       -o list	   Output the joined data according to list.  The specifiers in list have the format file.field, where file is either 1 or  2  and
		   field is the number of the field.

       -tc	   Recognize the tab character c.  The presence of c in a line is significant, both for comparing join fields and creating output.

Restrictions
       If you specify the option, the command collates the same as with no options.

Examples
       Suppose that by issuing the following commands, you display the files shown in the example:
       % cat file_1
       apr     15
       aug     20
       dec     18
       feb     05
       % cat file_2
       apr     06
       aug     14
       date
       feb     15
       Both files are sorted in ascending order.

       If you issue the command without options, the output appears as follows:
       % join file_1 file_2
       apr 15 06
       aug 20 14
       feb 05 15
       The third line in each input file is not joined in the output because the join fields (date and dec) do not match.

       To  join  the  lines  in these files and format the output so that the second field from each file appears first and the first (join) field
       appears second, issue the following command:
       % join -o 1.2 1.1 2.2 2.1 file_1 file_2
       15 apr 06 apr
       20 aug 14 aug
       05 feb 15 feb
       To write lines that are unmatched to the output, issue the following command:
       % join -a file_1 file_2
       apr 15 06
       aug 20 14
       date
       dec 18
       feb 05 15

See Also
       awk(1), comm(1), sort(1), sort5(1), environ(5int)

																	   join(1)
All times are GMT -4. The time now is 05:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy