Help in writing a find/replace script using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in writing a find/replace script using awk
# 1  
Old 10-07-2009
Help in writing a find/replace script using awk

Hi All,

I need to write a script that will go through 600+ files and perform find and replace. I was going to use sed but there is a level of complexity that is doing my head in.

To explain: I have 600+ files that have a line in them that reads (for example)

FILE=DCLCLHST
REMARKS='CLAIM_HISTORY' $
SEGNAME=DCLCHST, SEGTYPE=SO
--Followed by anothe 50 lines or so---

In the line REMARKS='CLAIM_HISTORY' $ the $ is supposed to have a , in front so it reads REMARKS='CLAIM_HISTORY' ,$

I have played around with awk and came up with awk '/REMARKS/{if ($2=="$") $2=",$"; print > FILENAME}' *.mas

Of course the problem here is that the entire file gets over written and only the REMARKS line (correct mind you) exists within the file.

So I guess the question is how can I perform a find and replace on the $ only on the line that contains the word REMARKS without overwriting the rest of the file. Thanks in advance
# 2  
Old 10-08-2009
thats because u missing the else part.. in else part simply print the $0
# 3  
Old 10-08-2009
Code:
sed -i.bak '/^REMARKS/s/\$/,$/' *.mas

Now, your *.mas files are modified, and originals are backed up as *.mas.bak
# 4  
Old 10-08-2009
Bug

Quote:
Originally Posted by daPeach
Code:
sed -i.bak '/^REMARKS/s/\$/,$/' *.mas

Now, your *.mas files are modified, and originals are backed up as *.mas.bak
Forgive me picking holes in this but I am trying to get to grips with sed.

In ksh I get an illegal option message for the -i. Taking that away the original files aren't modified, just echoed to stdout.

Thanks for showing me the '/^REMARKS pattern in front of the substitution, I wasn't aware you could do that.

Cheers
# 5  
Old 10-08-2009
-i option is not POSIX® standard , see man sed Smilie

OP you have to use a temporary file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script using awk to find and replace a line, how to ignore comment lines

Hello, I have some code that works more or less. This is called by a make file to adjust some hard-coded definitions in the src code. The script generated some values by looking at some of the src files and then writes those values to specific locations in other files. The awk code is used to... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

2. UNIX for Beginners Questions & Answers

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

3. Shell Programming and Scripting

awk script to find data in three file and perform replace operation

Have three files. Any other approach with regards to file concatenation or splitting, etc is appreciated If column55(billngtype) of file1 contains YMNC or YPBC then pick the value of column13(documentnumber). Now find this documentnumber in column1(Billdoc) of file2 and grep the corresponding... (4 Replies)
Discussion started by: as7951
4 Replies

4. Shell Programming and Scripting

help with writing a awk/sed script

Hi, I thought I am getting pretty good with sed and awk, but now I dont have a way out of this question. I have a table 0.5 16 1.3 14 0.25 15 0.85 16 I want to make a column 3 which contains values that are (corresponding $2 value/sum of all $2). Please help me out here. Thanks. (6 Replies)
Discussion started by: jamie_123
6 Replies

5. UNIX for Dummies Questions & Answers

awk: issues for writing a script

%%%%% (7 Replies)
Discussion started by: lucasvs
7 Replies

6. Shell Programming and Scripting

Need some help writing a find script

hello all! well, here is my problem: i have to write a script which waits for a directory as an argument and scans it recuresively and than it prints those files, which have write permission for everyone (as relative path from the argument directory so the find will be the key). and it shouldnt... (9 Replies)
Discussion started by: Mixe3y
9 Replies

7. Shell Programming and Scripting

writing shell script to find line of invalid characters

Hi, I have to write s script to check an input file for invalid characters. In this script I have to find the exact line of the invalid character. If the input file contain 2 invalid character sat line 10 and 17, the script will show the value 10 and 17. Any help is appreciated. (3 Replies)
Discussion started by: beginner82
3 Replies

8. UNIX for Dummies Questions & Answers

Help writing a script to find a file

I just started learning about Unix and I cant figure out what im doing wrong. I'm trying to write a script that will ask for the file name and tell what type it is. This is what i have so far. http://i63.photobucket.com/albums/h123/wacand/untitled.jpg (2 Replies)
Discussion started by: wacand
2 Replies

9. Shell Programming and Scripting

plz help in writing awk script

hi buddies pls help in this matter i have file like this input file -------------------------- (PARTITION PARTITION_1 VALUES LESS THAN (101, 16383 ) TABLESPACE PART_1 ,PARTITION PARTITION_2 VALUES LESS THAN (101, 32766 ) TABLESPACE PART_2 ,PARTITION PARTITION_3 VALUES LESS THAN (101,... (3 Replies)
Discussion started by: LAKSHMI NARAYAN
3 Replies

10. Shell Programming and Scripting

hi help in writing awk script(urgently)

hi to all i have file like this file.txt this is naryana expect hyderabad is a cool place now climate VISAKHSAPATNAM became very cool #vizag is my birth place #hyderabad is a cool place #now climate of hyd became very cool #vizag is my birth place #hyderabad is a cool place ... (7 Replies)
Discussion started by: LAKSHMI NARAYAN
7 Replies
Login or Register to Ask a Question