Visit Our UNIX and Linux User Community


Replacing patterns in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing patterns in a file
# 1  
Old 08-25-2009
Question Replacing patterns in a file

Hi Everybody,

Can we replace the contents of a file without using a temporary file.

For ex:
I need to relpace the value "old" with "new", I can do it using the sed replace pattern, but I do not want to use a temporary file.

Also, from the questions posted in the past I could see a fantastic command to do this
printf '1,$ s/old/new/g\nw! samp.txt\nq!'| ex - samp.txt

But, with the same command I am not able to replace the value of a variable
For ex: var1='new'
printf '1,$ s/new/$var1/g\nw! samp.txt\nq!'| ex - samp.txt

The above command doesn't replace the value of the variable.
# 2  
Old 08-25-2009
You can use sed to replace stings without using a temp file using its -i (edit in place) flag.

Shell variables are not interpreted inside single quotes. You should use double quotes or terminate the single quote before $var and start it after $var.

Code:
$ printf '1,$ s/new/'$var1'/g\nw! samp.txt\nq!'| ex - samp.txt

# 3  
Old 08-25-2009
Code:
$ 
$ echo "this is old data">f1
$ cat f1
this is old data
$ 
$ perl -pi.bak -e 's/old/new/g' f1
$ 
$ ## backup
$ cat f1.bak
this is old data
$ ## new
$ cat f1
this is new data
$

tyler_durden
# 4  
Old 08-25-2009
Hi agn,
Thanks for the input, it is working

Previous Thread | Next Thread
Test Your Knowledge in Computers #1005
Difficulty: Easy
Regarding a circle, one full turn of the circle is equal to 2 radians.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - Find files excluding file patterns and subfolder patterns

Hello. For a given folder, I want to select any files find $PATH1 -f \( -name "*" but omit any files like pattern name ! -iname "*.jpg" ! -iname "*.xsession*" ..... \) and also omit any subfolder like pattern name -type d \( -name "/etc/gconf/gconf.*" -o -name "*cache*" -o -name "*Cache*" -o... (2 Replies)
Discussion started by: jcdole
2 Replies

2. Shell Programming and Scripting

Replacing matched patterns in multiple files with awk

Hello all, I have since given up trying to figure this out and used sed instead, but I am trying to understand awk and was wondering how someone might do this in awk. I am trying to match on the first field of a specific file with the first field on multiple files, and append the second field... (2 Replies)
Discussion started by: karlmalowned
2 Replies

3. Shell Programming and Scripting

Parsing a file with different patterns

I have a text file like this , where the same set of three lines repeat with different values. INFO: processing now 03/25/2015-00:06:05 03/25/2015-00:16:04 ------Invoking myexe table=table1 INFO 25-03 00:20:26,801 - Finished processing (I=0, O=57, R=57, W=57, U=0, E=0) INFO: processing... (4 Replies)
Discussion started by: Lakshmikumari
4 Replies

4. Shell Programming and Scripting

Replacing multiple line patterns with awk

Hi forum, Can you please help me understand how to look for and replace the below pattern (containing line breaks) and return a new result? Rules: Must match the 3 line pattern and return a 1 line result. I have found solutions with sed, but it seems that sed installed in my system is... (5 Replies)
Discussion started by: demmel
5 Replies

5. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

6. Shell Programming and Scripting

Replacing text between two patterns

I would like to replace ], with ]]], between /* SECTION2-BEGIN */ and /* SECTION2-END */ in my file. My file contains the following information: /* SECTION1-BEGIN */ , /* SECTION1-END */ /* SECTION2-BEGIN */ , /* SECTION2-END */ /*... (5 Replies)
Discussion started by: azdps
5 Replies

7. Shell Programming and Scripting

Extract certain patterns from file.

Hi All, I tried extracting this pattern using grep but it did not work. What I have is a file which has contents like this: file:///channel/add-adhd.html file:///channel/allergies.html file:///channel/arthritis.html http://mail.yahoo.com/ http://messenger.yahoo.com/... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

8. Shell Programming and Scripting

Finding patterns in a file

Hi, I have a file with 3 columns and I want to find when the average number of rows on column 3 is a certain value. The output will be put into another file indicating the range. Here is what I mean (file is tab separated): hhm1 2 0 hhm1 4 0.5 hhm1 6 0.3 hhm1 8 -1.4... (2 Replies)
Discussion started by: kylle345
2 Replies

9. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

10. Shell Programming and Scripting

To find 3 patterns in a file

hy i have a requirement in which my script needs to find 3 patterns in a file and if any pattern is missing it should sent a mail Patterns Interval60min_Daily_readings$a.txt Interval_Daily_readings$a.txt Daily_readings$a.txt Basically i want to test for the above Patterns in the... (2 Replies)
Discussion started by: ali560045
2 Replies

Featured Tech Videos