replacing a string with another string in a txt file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers replacing a string with another string in a txt file
# 1  
Old 03-18-2011
replacing a string with another string in a txt file

Dear all,

I have a file like below. I want to replace all the '.' in the 3rd column with 'NA'. I don't know how to do that. Anyone has an iead? Thanks a lot!

8 70003200 21.6206
9 70005700 17.5064
10 70002200 .
11 70005100 19.1001
17 70008000 16.1970
32 70012400 26.3465
33 70014900 19.2607
34 70015500 19.1406
35 70007800 18.0802
36 70015300 .
37 70008600 16.1844
# 2  
Old 03-18-2011
Code:
sed 's/\./NA/g' infile

Or ... if you never have more than 1 dot per line you can go with
Code:
sed 's/\./NA/' infile

if you wan only replace the single dot , not those like 12.65849

then
Code:
sed 's/\.$/NA/' infile

This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 03-18-2011
If it is literally a replace of the full stop character with NA, then you could try:-
Code:
echo ":%s /\./NA/\nwq"|vi filename

It may not work if:-
  1. Your file has long lines over 2048 bytes,
  2. Your file is very large so it runs out of temp space with the edit,
  3. Your operating system version of vi is a bit different to mine, or
  4. I've missed the point

It is likely that sed could do it better, but I'm not a good writer. If you don't mind it being slow, you could also try (in korn shell):-
Code:
cat filename||tr "." " "|while read col1 col col3 col4
do
   echo $col1 $col2 ${col3}NA$col4
done>new_filename

Be aware that you cannout write to the file you are reading from. This could be rather slow if you are reading in a large file.



I hope that this helps, but like I said, a sed would probably be better.




Robin
Liverpool/Blackburn
UK
This User Gave Thanks to rbatte1 For This Post:
# 4  
Old 03-18-2011
Thanks a lot! it works. one more question: why do me need '\.' but not simply '.'

Thanks
# 5  
Old 03-18-2011
because dot has a special meaning when interpreted as a regular expression it means 'any single character' so it needs to be "escaped" using the backslash
Code:
printf ",s/\./NA/\nw\nq\n" | ed -s infile


Last edited by ctsgnb; 03-18-2011 at 02:39 PM..
This User Gave Thanks to ctsgnb For This Post:
# 6  
Old 03-18-2011
thanks! One more question: if I want only replace the dot in a certain column, saying 3rd column, how to do that? sorry, I am new to unix. Thanks again!
# 7  
Old 03-18-2011
Using awk
If you just want to replace the . in the 3rd column
Code:
awk '{if($3==".") print $1" "$2" NA";else print}' infile

This User Gave Thanks to gaurab For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Replacing a particular string in all files in folder and file contents

I need to replace all filesnames in a folder as well as its content from AK6 to AK11. Eg Folder has files AK6-Create.xml, AK6-system.py etc.. the files names as well as contents should be changes to AK9-Create.xml, AK9-system.py etc All files are xml and python scripts. ---------- Post... (0 Replies)
Discussion started by: Candid247
0 Replies

2. Shell Programming and Scripting

Replacing string values from a File

I want to replace string values from a file to a file For eg : File1 has 30 lines of string with values, those specific values needs to be changed in file2 and remaining values in file2 should be as it is. For example: From file (File1) cluster.name=secondaryCluster To replace File... (9 Replies)
Discussion started by: sriram003
9 Replies

3. Programming

Need help for replacing a string in a text file at runtime !

Hi All, I am facing an issue... I need to replace some string in a text file while the same file is read by some other user at the same time. The other user is using it in the Read only mode. So I can't create a temporary file and write the content first and then write it back into the original... (2 Replies)
Discussion started by: agupta2
2 Replies

4. Shell Programming and Scripting

replacing a string in multiple subdirs to a new string??

I have following set of dirs: /dir1/dir2/subdir1 file1 file2 /dir1/dir3/subdir1 file4 file5 /dir1/dir4/subdir1 file6 file7 All of these files have a common string in them say "STRING1", How can I... (3 Replies)
Discussion started by: Hangman2
3 Replies

5. UNIX for Advanced & Expert Users

Convert delimiter to string in txt file

Hi, I need to convert tab delimiter to #*# in txt file. Does anybody know how to do it? If i'm using: tr -s '\t' '#*#' < name.txt > name_new.txt It converts only to #, but I need 3 chars. Thanks a lot. (2 Replies)
Discussion started by: borsud
2 Replies

6. Shell Programming and Scripting

Replacing a character string in a file

Hi there, I have a paramater file that looks like this :- IRL|07122005|27389|VTIEpay|email address|5|200 When my program finishes I want to replace the seventh field. the existing code is like this cat <<-EOF | ed -s $PARFILE 1,$ g/^$ICO/s/$prvdate/$TODAY/ 1,$... (13 Replies)
Discussion started by: rjsha1
13 Replies

7. Shell Programming and Scripting

replacing string in an XML file

Hi all, I need to replace string in XML file..XML file like <dependency> <groupId>fr.orange.portail.ear</groupId> <artifactId>_AdminServicesEAR</artifactId> <version>1.0.0-20080521.085352-1</version> <type>ear</type> </dependency> <dependency> ... (2 Replies)
Discussion started by: subin_bala
2 Replies

8. Shell Programming and Scripting

replacing a string in a file with command line parameter

Hello, I am trying to replace a string with a paramter given along with the script. I am replacing application1 to application2 with the script: ./change_app.sh application2 change_app.sh: #!/bin/ksh grep $1 applications.dat 2>&1 >/dev/null echo $1 file=pckage.new sed 's/Name:... (5 Replies)
Discussion started by: chiru_h
5 Replies

9. UNIX for Dummies Questions & Answers

How to redirect date with string command into txt file

Hello im trying to redirect the standard output into txt file but with combination of string if I do : date >! foo.txt there is no problem and im getting the date into the foo.txt but what should I do if I like to add string in the same command so the result will be in the txt : The date... (2 Replies)
Discussion started by: umen
2 Replies
Login or Register to Ask a Question