awk script replace positions if certain positions equal prescribed value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script replace positions if certain positions equal prescribed value
# 1  
Old 09-14-2010
Data awk script replace positions if certain positions equal prescribed value

I am attempting to replace positions 44-46 with YYY if positions 48-50 = XXX.

Code:
awk -F "" '{if (substr($0,48,3)=="XXX") $44="YYY"}1' OFS="" $filename > $tempfile

But this is not working, 44-46 is still spaces in my tempfile instead of YYY. Any suggestions would be greatly appreciated.

Last edited by Scott; 09-14-2010 at 06:09 PM.. Reason: Please use code tags
# 2  
Old 09-14-2010
Can you show sample input?
# 3  
Old 09-14-2010
MySQL

sure can

before:

Code:
RR  20100828 00599 0001230111 +00000000123     XXX 100111
SS  20100828 00599 0001230111 +00000000123     XXX 100222
TT  20100828 00599 0001230111 +00000000123     ABC 100333

hoping for after:

Code:
RR  20100828 00599 0001230111 +00000000123 YYY XXX 100111
SS  20100828 00599 0001230111 +00000000123 YYY XXX 100222
TT  20100828 00599 0001230111 +00000000123     ABC 100333

*used the code tags to keep the spacing.
# 4  
Old 09-14-2010
Code:
# cat file
RR  20100828 00599 0001230111 +00000000123     XXX 100111
SS  20100828 00599 0001230111 +00000000123     XXX 100222
TT  20100828 00599 0001230111 +00000000123     ABC 100333
# awk '{$0=(substr($0,48,3)=="XXX")?substr($0,0,43)"YYY"substr($0,47):$0}1'  file
RR  20100828 00599 0001230111 +00000000123 YYY XXX 100111
SS  20100828 00599 0001230111 +00000000123 YYY XXX 100222
TT  20100828 00599 0001230111 +00000000123     ABC 100333

# 5  
Old 09-14-2010
CPU & Memory

cat file.txt
Code:
RR  20100828 00599 0001230111 +00000000123     XXX 100111
SS  20100828 00599 0001230111 +00000000123     XXX 100222
TT  20100828 00599 0001230111 +00000000123     ABC 100333
RR  20100828 00599 0001230111 +00000000123     XXX 100111
SS  20100828 00599 0001230111 +00000000123     XXX 100222
TT  20100828 00599 0001230111 +00000000123     DEF 100333
RR  20100828 00599 0001230111 +00000000123     XXX 100111
SS  20100828 00599 0001230111 +00000000123     XXX 100222
TT  20100828 00599 0001230111 +00000000123     DDD 100333

Code:
awk '{ if ( substr($0,48,3) == "XXX" ) { print substr($0,0,43) "YYY" substr($0,47)} else { print substr($0,0,43) "   " substr($0,47) } }' file.txt

Code:
RR  20100828 00599 0001230111 +00000000123 YYY XXX 100111
SS  20100828 00599 0001230111 +00000000123 YYY XXX 100222
TT  20100828 00599 0001230111 +00000000123     ABC 100333
RR  20100828 00599 0001230111 +00000000123 YYY XXX 100111
SS  20100828 00599 0001230111 +00000000123 YYY XXX 100222
TT  20100828 00599 0001230111 +00000000123     DEF 100333
RR  20100828 00599 0001230111 +00000000123 YYY XXX 100111
SS  20100828 00599 0001230111 +00000000123 YYY XXX 100222
TT  20100828 00599 0001230111 +00000000123     DDD 100333

# 6  
Old 09-14-2010
if no care of the output format.

Code:
awk '$6=="XXX" {$6="YYY" FS $6}1' infile

# 7  
Old 09-14-2010
Quote:
Originally Posted by sol_nov
Code:
awk '{ if ( substr($0,48,3) == "XXX" ) { print substr($0,0,43) "YYY" substr($0,47)} else { print substr($0,0,43) "   " substr($0,47) } }' file.txt

Let's remove useless code Smilie
Code:
awk '{ if ( substr($0,48,3) == "XXX" ) { print substr($0,0,43) "YYY" substr($0,47)} else {print} }' file.txt

I hope you'll catch the mistake.

---------- Post updated at 10:21 PM ---------- Previous update was at 10:17 PM ----------

Quote:
Originally Posted by rdcwayx
if no care of the output format.

Code:
awk '$6=="XXX" {$6="YYY" FS $6}1' infile

And if we care about output format Smilie
Code:
awk '$6=="XXX" {$1=$1 FS;$6="YYY" FS $6}1' file

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace specific positions in a file

I have a fixed-length positional file. I am trying to replace content of position 4-13 (length=10) with xxxxxxxxxx. Sample 2 rows in this file: H0187459823 172SMITH, JOE H0112345678 172DOE, JANE In this example 87459823 (from 1st line) and 12345678 (from 2nd line) (both in position... (3 Replies)
Discussion started by: Diver181
3 Replies

2. Shell Programming and Scripting

Replace multiple positions in records which match crireria

I have a test file a.txt 001 123 456 789 002 This is just a 001 test data 003 file. I want to clear columns 5 and 6 if the first 3 characters are 001 using awk. I tried following but does not work. Any suggestions? awk 'BEGIN{OFS=FS=""} {if (substr($0,1,3)=="123") $5=" "; $6="... (20 Replies)
Discussion started by: Soham
20 Replies

3. Shell Programming and Scripting

Replace characters at fixed positions

My objective is to replace the 8th, 9th, 10th characters by 1 space per character (total 3 spaces) in a file. I achieved this using following command: sed 's/\(.\)/\1@/7;s/@\(...\)/ /' FileData.txt > FileData_UPDATED.txt Another situation comes when I need to done same but excluding 1st... (5 Replies)
Discussion started by: manishdivs
5 Replies

4. UNIX for Dummies Questions & Answers

Replace alphabets from certain positions

Hi all, I have column 2 full of values like HIVE4A-56 and HIVE4-56. I want to convert all values like HIVE4A-56 to HIVE4-56. So basically I want to delete all single alphabets before the '-' which is always preceded by a number. Values already in the desired format should remain unchanged... (4 Replies)
Discussion started by: ames1983
4 Replies

5. Shell Programming and Scripting

sed to replace specific positions on line with file contents

Hi, I am trying to use an awk command to replace specific character positions on a line beginning with 80 with contents of another file. The line beginning with 80 in file1 is as follows: I want to replace the 000000000178800 (positions 34 - 49) on this file with the contents of... (2 Replies)
Discussion started by: nwalsh88
2 Replies

6. Shell Programming and Scripting

if characters from positions 7-15 are matching 219 then replace al

Script for if characters from positions 7-15 are matching with characters from position 211-219 then replace all char from 211-219 with 9 space. Total length of record is 420. Here is the specification of the data in file. Position Field Data Type... (2 Replies)
Discussion started by: lancesunny
2 Replies

7. Shell Programming and Scripting

awk regardless positions

brw------- 1 oracle dba 49, 21 Apr 05 11:45 dprod_0000018 brw------- 1 oracle dba 49, 26 Apr 05 11:45 dprod_0000019 brw------- 1 oracle dba 43, 93 Feb 02 2011 dprod_000002 brw------- 1 oracle dba 49, 27 Apr 05 11:45 dprod_0000020... (4 Replies)
Discussion started by: Daniel Gate
4 Replies

8. Shell Programming and Scripting

Replace a string within a file.. with help of positions

I have a huge file with lot of rows... with each row around 400 characters.. with spaces as well.. (e.g) Line1: "AC254600606 USDMI000001Anom01130073981 0000000000000.002005040720991231 ... (13 Replies)
Discussion started by: gopeezere
13 Replies

9. Shell Programming and Scripting

Replace 9-16 positions of a text file.

Hi i am having text file like this 40000201040005200213072009000000700000050744820906904421 40069300240005200713072009000000067400098543630000920442 i want to replace 9-16 positions of my txt file...by 1234567...in a single line command i.e 0400052....should be replaced by... (2 Replies)
Discussion started by: suryanarayana
2 Replies
Login or Register to Ask a Question