Replace value in files


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Replace value in files
# 1  
Old 11-19-2009
Replace value in files

Hello All,

Please if I could get some help on the following, I have data files (due to 3rd party mistake) within each data file on line/row 3 column/field 3 - there is value '1' that need to be changed to '0'.

For example:

HEAD, TYPE OF FILE
NAME,ADDRESS
M,TYPE, 1


Now initially only two data files had the problem, so I was manually changing the 1 to 0 (as the 1 shown above) - but now I've found over 1000 files that actually have the problem (and apparently need to be changed to 0 in the next two hours). I trying to write a Unix script that does all files in one go by using AWK and SED command, or any, But I'm getting no way, any suggestion (all are welcome) .....
# 2  
Old 11-19-2009
Try this:

Code:
awk -F, 'NR==3{$3=0}{print}' OFS="," file > newfile

# 3  
Old 11-19-2009
thanx... I get awk error when trying something like that, what I was originally try was to highlight the row through grep then edit it:

for example

for i in `ls *`; do grep M,TYPE, 1 $i ....

then to a replace through the awk command ....
# 4  
Old 11-19-2009
Quote:
Originally Posted by killos
thanx... I get awk error when trying something like that, what I was originally try was to highlight the row through grep then edit it:

for example

for i in `ls *`; do grep M,TYPE, 1 $i ....

then to a replace through the awk command ....
What kind of errors? Have you resolve the problem ?
# 5  
Old 11-19-2009
I've been getting syntax errors... but I've looked at this from different approaches (also taking in account your suggestion) and I've come up with this:

---

for x in `find . -size -9000` ;do sed 's/TYPE,1/TYPE,0/' <$x> $x.new;
rm $x;
mv "$x.new" /usr/local/data/backdata"$x";
echo "$x" >> ./logfile
done

-------

But I haven't been able to test it 'cause to add to my great day the server gone done, ha!!


---------- Post updated at 10:20 AM ---------- Previous update was at 06:41 AM ----------

thanx for the Franklin52....I have it working, it was jerkin me about on the rename, but with a suggestion I moved it to another directory first.

---------- Post updated at 10:20 AM ---------- Previous update was at 10:20 AM ----------

thanx for the Franklin52....I have it working, it was jerkin me about on the rename, but with a suggestion I moved it to another directory first.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find all .sh files in file system and need to replace the string inside .sh files

Hi All, I need to write a script to find all "*.sh" files in /home file system and if any string find "*.sh" files with the name vijay@gmail.com need to replace with vijay.bhaskar@gmail.com. I just understood about the find the command to search .sh files. Please help me on this. find / -name... (3 Replies)
Discussion started by: bhas85
3 Replies

2. Shell Programming and Scripting

Replace values between 2 files

I want to replace the third and fourth lines of a 2nd file by the first two lines of a file. Input: file_1 file_1.line_1 file_1.line_2 file_2 file_2.line_1 <file_2.line_2_blank> file_2.line_3 file2.line_4 <file_2.line_5_blank> Output: file_2.line1 <file_2.line_2_blank>... (1 Reply)
Discussion started by: arpagon
1 Replies

3. UNIX for Dummies Questions & Answers

Search and replace across files

Hi All, HP-UX oradev3 B.11.11 U 9000/800 3251073457 I have following files all contains text like "919642990137@". I need to search this text across all files and replace it with something like "919717298765@". I was wondering, how to accomplish this task? cpmprod_status.sh... (8 Replies)
Discussion started by: alok.behria
8 Replies

4. Shell Programming and Scripting

search and replace in many files

I have a directory full of files. Most of the code in the files is the same accept for a few parameter values. I want to remove hard coded values and replace it with a parameter that I can pass. This is oracle pl/sql anonymous blocks. so the code is similiar to this function myfunc (p_myvar ... (1 Reply)
Discussion started by: guessingo
1 Replies

5. UNIX for Advanced & Expert Users

Find and Replace from files

Is there any command which I can apply from the command line to find and replace a particular text say "00:00:00:00" with "00" from all the files( where ever this text exists) of the current directory? Thanks in advance (1 Reply)
Discussion started by: cobroraj
1 Replies

6. Shell Programming and Scripting

Replace a string containing :/=\ in files

I have read many forums about replacing a string in file. I tested but it doesn't work with my string For example I want to replace =x=ks24Y\:LOP= with h\P6qwx:aUE/. Thank you (4 Replies)
Discussion started by: lalelle
4 Replies

7. Shell Programming and Scripting

compare 2 files and replace

Hi, I have a file which contains names with counts for eg: 0622 0031 JOHN MAX 20080622003104. STAT 1. 0622 0031 BILL MAX 20080622003104. STAT 7. and I have an exception file containing. BILL Can anyone help to write a script... (7 Replies)
Discussion started by: antointoronto
7 Replies

8. Shell Programming and Scripting

Match and replace in different files

Hi, I want to replace the content in 3rd column in the first file by the line in the 2nd file (a list) if matches with 1st column of 2nd file. There are many 1st files in different directories: File1 172.27.1.15 222 I_J_Mar_Sci_34_27.pdf 172.27.28.1 489 Sci_Cult_71_60.pdf... (3 Replies)
Discussion started by: srsahu75
3 Replies

9. Shell Programming and Scripting

How to replace one word across too many files at once

I'm trying to replace the word "core3" with the word "core2" across too many scripts which they all contain that word "core3" (Shell & XML scripts) all at once. Is there easy way with "sed" to do it? Thanks Folks. (2 Replies)
Discussion started by: moe2266
2 Replies

10. UNIX for Dummies Questions & Answers

how find and replace into different files

Hi, I have a serie of files in a directory. Into this files I´ve got lines with a serie of numbers like "12344332","1","2","3","4","5","6","7","3","7","7","8","3","7" I need make some changes on the line where is a specific string, for example numberToIdentify = "12344332". And once... (1 Reply)
Discussion started by: ran
1 Replies
Login or Register to Ask a Question