Need help to replace a pattern on specific line in a data file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help to replace a pattern on specific line in a data file
# 1  
Old 08-14-2012
Need help to replace a pattern on specific line in a data file

Hi,

I want to replace specific pattern "-2.0000 2" by "1.0000 3" on a particular line (line #5) in a file 1.dat. I have about 50 more files similar to 1.dat in which I want to do this correction.

Can you please suggest how can I make change only at this particular line of a file and retain remaining file as it is.

Code:
$cat 1.dat
      1     4.619209   -2.737649   16.892469  -2.0000       2      10       1
      2    -4.132976    1.244385   16.892469  -2.0000       2      10       2
      3     7.536607    3.235402   16.892469  -2.0000       2      10       3
      4    12.885178   -2.737649   16.892470  -2.0000       2       9       4
      5    -7.050373   -0.248878   16.892469  -2.0000       2      12       5
      6     5.591674    1.244385   16.892469  -2.0000       2      10       6
      7    -7.536606   -0.248878   16.892469  -2.0000       2      11       7
      8    -5.591674    2.239894   16.892469  -2.0000       2       9       8
      9     1.215581   -2.737649   16.892469  -2.0000       2       9       9
     10     6.564140    3.235402   16.892469  -2.0000       2      10      10
$

required output

Code:
$cat 1.dat
      1     4.619209   -2.737649   16.892469  -2.0000       2      10       1
      2    -4.132976    1.244385   16.892469  -2.0000       2      10       2
      3     7.536607    3.235402   16.892469  -2.0000       2      10       3
      4    12.885178   -2.737649   16.892470  -2.0000       2       9       4
      5    -7.050373   -0.248878   16.892469  1.0000       3      12       5
      6     5.591674    1.244385   16.892469  -2.0000       2      10       6
      7    -7.536606   -0.248878   16.892469  -2.0000       2      11       7
      8    -5.591674    2.239894   16.892469  -2.0000       2       9       8
      9     1.215581   -2.737649   16.892469  -2.0000       2       9       9
     10     6.564140    3.235402   16.892469  -2.0000       2      10      10
$


I tried
Code:
sed -n '5p' 1.dat |"s/-2.0000       1/1.0000       3/" > 1.dat

, but this just print line #5 and do not give remaining content.

Thank you,
Anuj

Last edited by Scott; 08-14-2012 at 03:11 PM.. Reason: Use code tags, please...
# 2  
Old 08-14-2012
Code:
awk 'NR==5{sub("-2.0000 2","1.0000 3",$0)}1' 1.dat > 1.fixed.dat

# 3  
Old 08-14-2012
Code:
while read file
do
    ed -s "$file" <<-EOF
        5s/-2.0000 1/1.0000 3/
        w
        q
    EOF
done < list

# 4  
Old 08-14-2012
Quote:
Originally Posted by bartus11
Code:
awk 'NR==5{sub("-2.0000 2","1.0000 3",$0)}1' 1.dat > 1.fixed.dat

@bartus11

Thank you , it works !

---------- Post updated at 01:15 PM ---------- Previous update was at 01:12 PM ----------

Quote:
Originally Posted by Don Cragun
Code:
while read file
do
    ed -s "$file" <<-EOF
        5s/-2.0000 1/1.0000 3/
        w
        q
    EOF
done < list

@Don

Thank you, for your reply. But, it gives me error :

"syntax error: unexpected end of file"

Am I making some mistake.

here is the script I used


Code:
$ cat replace.sh 
#!/bin/bash

while read file
do
    ed -s "$file" <<-EOF
        21s/-2.0000       2/1.0000       3/
        w
        q
    EOF
done < list

---------- Post updated at 01:29 PM ---------- Previous update was at 01:15 PM ----------

Anyway, thank you guys ! Its working now

Last edited by Scott; 08-14-2012 at 03:12 PM.. Reason: Code tags
# 5  
Old 08-14-2012
Quote:
Originally Posted by anuj06
@bartus11

Thank you , it works !

---------- Post updated at 01:15 PM ---------- Previous update was at 01:12 PM ----------



@Don

Thank you, for your reply. But, it gives me error :

"syntax error: unexpected end of file"

Am I making some mistake.

here is the script I used


$ cat replace.sh
#!/bin/bash

while read file
do
ed -s "$file" <<-EOF
21s/-2.0000 2/1.0000 3/
w
q
EOF
done < list

---------- Post updated at 01:29 PM ---------- Previous update was at 01:15 PM ----------

Anyway, thank you guys ! Its working now
Sorry,
I had some cut and paste errors. The script I supplied should have been:
Code:
while read file
do
        ed "$file" <<-EOF
                5s/-2.0000 2/1.0000 3/
                w 
                q
        EOF
done < list

This should work with ksh, bash, or any POSIX conforming shell.

The <space>s at the start of the lines in the while loop should have been <tab>s. (That kept the EOF from being recognized as the end of the here-document.)

The s/-2.0000 1/1.0000 3/ was copied from the sed you said you used in the 1st message in this thread. The 1st part of your problem statement had the desired replacement listed correctly; I should have read what you said instead of copying your code. (That would have prevented a successful replacement.)

In your quote above you said you used the ed command:
Code:
21s/-2.0000       2/1.0000       3/

but I don't know where the 21 at the start of that line came from. I had 5 there instead of 21 because you said you wanted to make the change on line 5 in all of the files you're going to process. Also note that in this substitution you're looking for eight spaces between fields, but the input file you provided only has a single space between fields. If the number of spaces in your substitute command doesn't match the number of spaces in the files you're editing, the substitution will fail (in awk, ed, and sed).

Hope this helps,
Don
# 6  
Old 08-14-2012
@Don

Thank you.

Yes, I made some changes in line#21, so I put it there.
# 7  
Old 08-14-2012
If Perl is an option, then :

Code:
perl -i.bak -pe 's/-2.0000       2/ 1.0000       3/ if $.==5; close ARGV if eof' *.dat

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Search for a pattern and replace a space at specific position with a Character in File

In file, we have millions of records each of 1000 in length. And at specific position say 800 there is a space, we need to replace it with Character X if the ID in that row starts with 123. So far i have used the below which is replacing space at that position to X but its not checking for... (3 Replies)
Discussion started by: Jagmeet Singh
3 Replies

2. Shell Programming and Scripting

Grep pattern after specific line number in a file

Hi guys, I am running a while loop in a script ro read a file line by line. Now I want to run a grep only on the lines below the line I am that is being read by the while loop. Eg: If my while loop is on line 4 of the file, the grep only runs below line 4 and does not include line 1,2... (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

3. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

4. Shell Programming and Scripting

Replace specific line in file

Hello I am working on a script where I need to replace the particular line with new line Old line is <outputFileNameMapping>${artifact.artifactId}-${project.version}.${artifact.extension}</outputFileNameMapping> and new line is ... (5 Replies)
Discussion started by: anuragpgtgerman
5 Replies

5. Shell Programming and Scripting

Replace the line with specific pattern

Hello All I'm trying to change one string from a file contening this patern: xxxx-xxxx 4 numbers - end 4 other numbers This is a sample of the file: LDR 00679 am a2200205 4500 =001 3617 =008 030219s2000\\\\xxx|||||\||||\00|\0\spa\d =020 \\$a0211-1942 =041 \\$aCastellà =093 ... (5 Replies)
Discussion started by: ldiaz2106
5 Replies

6. Shell Programming and Scripting

Replace string in line below specific pattern?

Hi, I'm trying to replace a string with sed, in a text file containing this pattern: location alpha value x location beta value y location gamma value y location delta value y location theta value z ... What I want to achieve is: Find location beta into text file... (1 Reply)
Discussion started by: TECK
1 Replies

7. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

8. Shell Programming and Scripting

Help with replace line based on specific pattern match

Input file data20714 7327 7366 detail data20714 7327 7366 main data250821 56532 57634 detail data250821 57527 57634 main data250821 57359 57474 main data250821 57212 57301 main data250821 57140 57159 detail data250821 56834 57082 main data250821 56708 56779 main ... (3 Replies)
Discussion started by: perl_beginner
3 Replies

9. Shell Programming and Scripting

Merge two file data together based on specific pattern match

My input: File_1: 2000_t g1110.b1 abb.1 2001_t g1111.b1 abb.2 abb.2 g1112.b1 abb.3 2002_t . . File_2: 2000_t Ali england 135 abb.1 Zoe british 150 2001_t Ali england 305 g1111.b1 Lucy russia 126 (6 Replies)
Discussion started by: patrick87
6 Replies

10. Shell Programming and Scripting

serach and replace a specific pattern or value in a xml file

can some one help me with a perl command i have to search and replace a version from a xml-file so i use in a ksh script a command like this ssh $GLB_ACC@$GLB_HOST "/usr/contrib/bin/perl -pi -e "s/$curVersion/$new_Version/g" $Dest_dir/epi.xml" this command worked so far, but the problem... (1 Reply)
Discussion started by: kiranreddy1215
1 Replies
Login or Register to Ask a Question