Replace multiple positions in records which match crireria


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace multiple positions in records which match crireria
# 15  
Old 06-02-2017
Quote:
Originally Posted by Soham
Great. It worked. But a small issue again. If the input line has one or more blanks in between they are converted to single blank and if at the end of teh line they are truncated.

Naturally
Code:
BEGIN{OFS=FS=""}

will not work.
different tack:
Code:
nawk '/^001 / {$0="001   " substr($0, index($0," ")+3)}1' myFile

or try this - Solaris' awk/nawk are finicky...
Code:
nawk 'BEGIN {FS=SUBSEP}/^001 / {$0="001   " substr($0, index($0," ")+3)}1' myFile

This User Gave Thanks to vgersh99 For This Post:
# 16  
Old 06-02-2017
Setting FS and OFS to an empty string to treat character positions as fields is a GNU extension that is not supported by nawk, /usr/xpg4/bin/awk, nor required by the POSIX standards. Just using standard interfaces in awk to change character positions 5 and 6 to <space>s if character positions 1, 2, and 3 are the string 001 can be done simply on a Solaris system with:
Code:
nawk '/^001/ {$0 = substr($0, 1, 4) "  " substr($0, 7)} 1' input_file

or:
Code:
/usr/xpg4/bin/awk '/^001/ {$0 = substr($0, 1, 4) "  " substr($0, 7)} 1' input_file

These 2 Users Gave Thanks to Don Cragun For This Post:
# 17  
Old 06-02-2017
It works !!!

Thanks
# 18  
Old 06-02-2017
Try also
Code:
awk 'sub ("^001.","&##") {sub (/##../, "  ")} 1'  file

You may want to choose a better placeholder pattern / value.
This User Gave Thanks to RudiC For This Post:
# 19  
Old 06-02-2017
Great. I never thought it is so simple !!!
# 20  
Old 06-03-2017
Substitutions at character positions are often easier with sed
Code:
sed '/^001/ s/\(.\{4\}\)../\1  /' file

The \1 puts back what matched within the \( \) and that is the first 4 characters.
# 21  
Old 06-03-2017
@MadeInGermany
In which case this could be, even, simpler
Code:
 sed 's/\(^001.\)../\1  /' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 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

2 files replace multiple occurances based on a match

Hi All, I need some help trying to achieve the below but everything I've tried has failed, I have 2 files which i'm trying to carry out a match based on the first column from file 1, take that value find it in file 2 if found replace it with the second column from File 1 Lookup File: File 1... (3 Replies)
Discussion started by: mutley2202
3 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

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

6. Shell Programming and Scripting

Match records from multiple files

Hi, I have 3 tab delimited text files which look like this. File1: PROTEINID DESCRIPTION PEPTIDES FRAMES GB://115298678 _gi_115298678_ref_NP_000055.2_ complement C3 precursor 45 55 GB://4502027 _gi_4502027_ref_NP_000468.1_ serum albumin preproprotein 34 73 Entrez://strain 11128 /... (3 Replies)
Discussion started by: Vavad
3 Replies

7. 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

8. Shell Programming and Scripting

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. 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. (9 Replies)
Discussion started by: halplessProblem
9 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

10. AIX

grep - multiple positions

I was wondering if anybody can help me with this. I have the following code to look for a space in position #48 and I want to change it so it looks in position 48, 59, and 50 for spaces. How can I do that? Here's the current code - grep -v '^.\{48\}].*' <infile> > <outfile> Any help would... (3 Replies)
Discussion started by: nelson553011
3 Replies
Login or Register to Ask a Question