(g)awk conditional substitution issues when attempting to delete character


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers (g)awk conditional substitution issues when attempting to delete character
# 1  
Old 11-14-2016
(g)awk conditional substitution issues when attempting to delete character

A portion of my input is as follows:

Code:
 1087  IKON01,49 A WA-                                 -1 .       -1 .           0 W               WA-                                 -1 .        -1 .         0 .     -1 .        -1 -1 -1 -1 -1 -1 W
 1088  IKON01,49 A J.@QU80MW.                           2 !J.@!    0 .           0 QWM[            QUM                                  7 [W.       0 .         0 .      0 .         0 11  3  3  2 -1 JQMW
 1089  IKON01,49 A K.@L&                               -1 .       -1 .          -6 KL/             K.@L                                -1 .         1 /         0 .      0 .        -1 -1 -1  1  2  1 KL

I would like the following desired output:

Code:
 1087  IKON01,49 A WA-                                 -1 .       -1 .           0 W               WA-                                 -1 .        -1 .         0 .     -1 .        -1 -1 -1 -1 -1 -1 W
 1088  IKON01,49 A J.@QU80MW.                           2 !J.@!    0 .           0 QM[             QUM                                  7 [W.       0 .         0 .      0 .         0 11  3  3  2 -1 JQMW
 1089  IKON01,49 A K.@L&                               -1 .       -1 .          -6 KL/             K.@L                                -1 .         1 /         0 .      0 .        -1 -1 -1  1  2  1 KL

In essence, I would like to delete every W in field $9 while preserving the original, pre-substitution formatting, given the following regex condition:

Code:
if($9 ~/^.W[^H]=*\[$/)

However, I would want the formatting of the file to be preserved. I realize this has been dealt with in previous posts and I know how to use
Code:
printf

and/or
Code:
FIELDWIDTH

(with gawk), but since my file is 61 fields long (NF==61; I've only presented a portion here), this is tremendously cumbersome and messy. In addition, I do not know every fieldwidth and so would like to avoid figuring this out to reformat the file.

I've had a similar issue in the past, and RudiC helped me via a very nifty trick taking advantage of NF being recomputed when there is an assignment to $0. Thus, I attempted the following:

Code:
gawk '$9 ~/^.W[^H]=*\[$/{X=$9; sub(/W/,"",X); sub ($9, X, $0)}' file.txt

This time however, it seems as those when doing the field substitution, the operation is aborted because of the non-escaped meta-character "[" that is in my data. This produces the following error in field 9 of a different line:

Code:
fatal: Invalid regular expression: /BW>[/

In light of this, I've also attempted:

Code:
gawk '{if($9 ~/^.W[^H]=*\[$/); sub(/W/,"",$9); print}' file.txt

Not only does this ruin the formatting of the file, but it is also matching lines I wouldn't expect it to such as:

Code:
1  IKON01,01 A W:-                                 -1 .       -1 .           0 W               W:-                                 -1 .        -1 .         0 .     -1 .        -1 -1 -1 -1 -1 -1 W

Thank you so much in advance for helping me through this quagmire.

---------- Post updated at 11:00 AM ---------- Previous update was at 10:49 AM ----------

This is probably fairly obvious, but I should say that in my data examples from my post, the numbers on the far left are line numbers and not $1.

Thank you.
# 2  
Old 11-14-2016
This seems to work for me:
Code:
awk '$9 ~ "^.W[^H]=*\[$" {X=$9; sub(/W/, "", X); sub ($9 "[]", X " ", $0)} 1' file
IKON01,49 A WA-                                 -1 .       -1 .           0 W               WA-                                 -1 .        -1 .         0 .     -1 .        -1 -1 -1 -1 -1 -1 W
IKON01,49 A J.@QU80MW.                           2 !J.@!    0 .           0 QM[             QUM                                  7 [W.       0 .         0 .      0 .         0 11  3  3  2 -1 JQMW
IKON01,49 A K.@L&                               -1 .       -1 .          -6 KL/             K.@L                                -1 .         1 /         0 .      0 .        -1 -1 -1  1  2  1 KL


We're making the field ending "[" part of a "bracket expression" (c.f. man regex) by treating itself as the opening bracket, adding the char (the "[") and the closing bracket as char constants in the second sub statement. We need to add a space when substituting $9 to maintain the filed length and thus the $0 formatting.
These 2 Users Gave Thanks to RudiC For This Post:
# 3  
Old 11-14-2016
Thanks so much for this RudiC.

With your current line of code, the conditional, viz.,

Code:
$9 ~ "^.W[^H]=*\[$"

threw up the error code

Code:
gawk: cmd. line:1: warning: escape sequence `\[' treated as plain `['
gawk: cmd. line:1: (FILENAME=Kings.qdf FNR=1) fatal: Unmatched [, [^, [:, [., or [=: /^.W[^H]=*[$/

When I changed it to a string constant, i.e.,

Code:
$9 ~ /^.W[^H]=*\[$/

it did the trick!

I cannot thank you enough for this and look forward to studying this to see how you got it to work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditional delete -- New glitch

Hi Please dont consider this as duplicated post.. I am using below pattern to find delete files to bringdown disc size.. however how i can make sure ist going to correct folder and searching for files... while print "echo rm " LastFile correctly print files names for deletion, but when i... (7 Replies)
Discussion started by: onenessboy
7 Replies

2. Shell Programming and Scripting

Conditional delete

Hi Friends, I have somefiles like 20180720_1812.tar.gz 20180720_1912.tar.gz 20180720_2012.tar.gz 20180720_2112.tar.gz 20180721_0012.tar.gz 20180721_0112.tar.gz 20180721_0212.tar.gz 20180721_0312.tar.gz in a directory and so on..these files gets created every 3 hours where as... (28 Replies)
Discussion started by: onenessboy
28 Replies

3. UNIX for Beginners Questions & Answers

Bad substitution issues.. but why?

i am trying to prepare a train and test dataset, for which i need to randomly split the data into corresponding folders (train,test).. I began on a simple script, but seem to get som weird error messages, that i cannot make sense of?.. what am I doing wrong? #!/bin/bash RED='\033] then... (13 Replies)
Discussion started by: kidi
13 Replies

4. Shell Programming and Scripting

How to delete a character using sed and or awk?

Hi, 1/ i have file test.txt 1 Jul 28 08:35:29 2014-07-28 Root::UserA 1 Jul 28 08:36:44 2014-07-28 Root::UserB i want to delete the seconds of the file, and the Root:: and the output will be: 1 Jul 28 08:35 2014-07-28 UserA 1 Jul 28 08:36 2014-07-28 UserB 2/i have another file test2.txt:... (8 Replies)
Discussion started by: fxsme
8 Replies

5. Shell Programming and Scripting

sed or awk delete character in the lines before and after the matching line

Sample file: This is line one, this is another line, this is the PRIMARY INDEX line l ; This is another line The command should find the line with “PRIMARY INDEX” and remove the last character from the line preceding it (in this case , comma) and remove the first character from the line... (5 Replies)
Discussion started by: KC_Rules
5 Replies

6. Shell Programming and Scripting

any savant ? using AWK/SED to remove newline character between two strings : conditional removal

I'd like to remove (do a pattern or precise replacement - this I can handle in SED using Regex ) ---AFTER THE 1ST Occurrence ( i.e. on the 2nd occurrence - from the 2nd to fourth occurance ) of a specific string : type 1 -- After the 1st occurrence of 1 string1 till the 1st occurrence of... (4 Replies)
Discussion started by: sieger007
4 Replies

7. Shell Programming and Scripting

KSH: substitution character, AWK or SED?

Hi Gurus, I am working with a korn shell script. I should replace in a very great file the character ";" with a space. Example: 2750;~ 2734;~ 2778;~ 2751;~ 2751;~ 2752;~ what the fastest method is? Sed? Awk? Speed is dead main point, Seen the dimensions of the files Thanks (6 Replies)
Discussion started by: GERMANICO
6 Replies

8. Shell Programming and Scripting

Delete character in determinate position with sed/awk

Hello. I'm trying to delete one character in determinate position. Example: qwEtsdf123Ecv34 <delete character in positión 3> Result: qwtsdf123Ecv34 Plase, help me. Thanks (4 Replies)
Discussion started by: maria_florencia
4 Replies

9. Shell Programming and Scripting

Sed character substitution

Hi I have to replace in around 60 files a word an replcae it by another Suppose all the files have a word intelligent i want to replace it by idiot I am planning to use sed for executing this job sed 's/\intelligent/idiot/g' I plan to have a file (test.txt) which contains... (1 Reply)
Discussion started by: ultimatix
1 Replies

10. Shell Programming and Scripting

character substitution

Hi , I have a problem , I need to devlope a script where in the user inputs file name , line number , and character position , and a substitution variable , the character at that character position should be substituted by the substitution value for ex say i have a file abc.txt which... (3 Replies)
Discussion started by: viv1
3 Replies
Login or Register to Ask a Question