Find and replace single character w/awk given conditions
I have a file that looks like this:
When there are two characters in $2, $3 does not line up when there are other strings in $3. That is to say, when there are three characters in $2, then there are 25 spaces until $3 should begin. However, then there are two characters in $2, then there are 26 spaces and this throws off the justification of both $3 and >=$4.
What I want to do is search for when $3 begins on the 40th character and delete a space so that it begins on the 39th.
Thus:
In order to do this, I've attempted this code awk code, but have had trouble combining conditional statements with substrings and substitutions.
I've also tried this:
...and this with a variable:
Perhaps I'm going at this all wrong, but ideally what I'd like is all of my columns to line up, but since my last column will have multiple spaces in it, I've had difficulty executing printf(). Perhaps there is some iteration of FIXEDWIDTH that is escaping me. Nevertheless, I need to be able to learn how to effectively combine conditionals, substrings, and substitutions in awk so this is why I'm asking for help in this manner.
FIELDWIDTHS seemed to only work on splitting. Then needed printf format specifiers to output them pretty again. This strips whitespace from beginning of all fields.
This User Gave Thanks to neutronscott For This Post:
Thanks so much for this RudiC and sorry for the embarrassing miscue on FIELDWIDTH. While Neutronscott's code nails it, for some reason I cannot get your suggestion to work although it seems as though it should. I must confess that seem to mess up sub() and gsub() quite often. Here is my code, as per your suggestion:
Thanks so much for this RudiC and sorry for the embarrassing miscue on FIELDWIDTH. While Neutronscott's code nails it, for some reason I cannot get your suggestion to work although it seems as though it should. I must confess that seem to mess up sub() and gsub() quite often. Here is my code, as per your suggestion:
Did I do something amiss?
That will remove at most one leading space. I don't have gawk on my system and awk on my system doesn't support the FIELDWIDTHS variable, so I can't test anything using it. But, neutronscott's suggestion might be a good starting point. (Have you tried it?)
It would seem that adjusting field 3 (with:
not:
) would line up field 3; but might not fix alignment of field 4.
Assuming field widths for your 1st three fields of 11 characters, 27 characters, and 26 characters (guessing from jvoot's sample output), the following should work with any awk that adheres to the POSIX standards' requirements (implementing a subset of the gawk field widths option using standard awk features):
This should line up all fields as long as data from one field doesn't spill over into following fields (other than <space>s). This will align any number of fields (not just 4) with their sizes specified by the list provided in the FIELDWIDTHS variable. The last field width specified is unimportant. This code assumes that the last field starts at the calculated starting column (based on earlier field widths) and runs to the end of the line; no trailing spaces are included in the output of the last output field. With jvoot's sample input, it produces the output jvoot requested.
Note that many implementations of awk do not adhere to the standards when using substr() on fields that contain multi-byte characters. (The standards say that start and length parameters count characters; some awk implementations count bytes instead. As long as you're only dealing with single-byte characters, both work correctly.)
If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk.
Hi,
following Perl code i used for finding multiple strings and replace with single string.
code:
#!/usr/bin/perl
my @files = <*.txt>;
foreach $fileName (@files) {
print "$fileName\n";
my $searchStr = ',rdata\)' | ',,rdata\)' | ', ,rdata\)';
my $replaceStr =... (2 Replies)
Hi,
i want find the character '-' in a file from position 284-298, if it occurs i need to replace it with 'O ' for the position in the file. How to do that using SED command.
thanks in advance,
Sara (9 Replies)
Hi Team,
i have 1st cloumn of data containing,
LAMSBA01-BA-COFF-YTD
LAMSBA01-BA-COFF-ITD
LAMSBA01-BA-AGGR-IND .
LAMSBA01-BA-CURR-COFF-BAL
i need to replace the "-" to "_" (underscore) using AWK .
please help me on this.
Thanks,
Baski (4 Replies)
Hi
Does anyone know how to set any character as the field separator with awk/nawk on a solaris 10 box. I have tried using /./ regex but this doesnt work either and im out of ideas.
thanks (7 Replies)
Hi all,
Greetings,
I have the following scenario, The contents of main file are like :
Unix|||||forum|||||||||||||||is||||||the||best
so||||||be|||||on||||||||||||||||||||||||||||||||||||||||||||it
And i need the output in the following form:
Unix=forum=is=the=best
so=be=on=it
... (3 Replies)
Hi,
I have variable inside shell script - from_item.
from_item = 40.1'1/16
i have to first find out whether FROM_ITEM contains single quote(').
If yes, then that need to be replace with two quotes ('').
How to do it inside shell script? Please note that inside shell script........ (4 Replies)
How do you print out a single quote character in AWK? Using the escape character does not seem to work.
{printf "%1$s %2$s%3$s%2$s\n" , "INCLUDE", " \' ", "THIS" }
does not work. Any suggestions? (6 Replies)
Dear all
I need a script for multiple find and replace in a single file.
For example input file is -
qwe wer ert rty tyu
asd sdf dgf dfg fgh
qwe wer det rtyyui
jhkj ert asd asd dfgd
now
qwe should be replace with aaaaaa
asd should be replace with bbbbbbbb
rty should be replace... (6 Replies)
I already have accomplished this task using sed and arrays, but since I get the variable using awk, I figured I'd ask this question and maybe I can get a cleaner solution using strictly awk.. I just can't quite grasp it in awk.
Story: I'm automating the (re)configuration of network interfaces,... (3 Replies)
Hi,
I have a file 'imei_01.txt' having the following contents:
$ cat imei_01.txt
a123456
bbr22135
yet223
where I want to check whether the expression 'first single alphabet followed by 6 digits' is present in the file (here it is the first record 'a123456')
I am using the following... (5 Replies)