qshaan
March 25, 2020, 3:26pm
1
Dear team,
Here is my input file below. I need awk script which insert new column $8 with same value as $7 but in case $7=blank then same value as previous value .
$cat sample.txt:
ACT 1J AD-300 TIME 200322 1554 HLRBC03
ORDERED
ACT 1J AD-300 TIME 200322 1554 HLRBC08
ORDERED
ACT 1J AD-300 TIME 200322 1554 HLRBC12
ORDERED
ACT 1J AD-300 TIME 200322 1554 HLRBC18
ORDERED
ACT 1J AD-300 TIME 200322 1554 HLRBC04
ORDERED
ACT 1J AD-300 TIME 200322 1554 HLRBC07
ORDERED
ACT 1J AD-300 TIME 200322 1554 HLRBC15
ORDERED
ACT 1J AD-300 TIME 200322 1554 HLRBC19
ORDERED
Like below
ACT 1J AD-300 TIME 200322 1554 HLRBC03 HLRBC03
ORDERED HLRBC03
HLRBC03
I tried this
awk '{a[++i]=$0;}END{for (j=1;j<NR;j++) {if (a[j]~"HLRBC") {for (y=1;y<NR;y++) $7==$8 print $8}}}' sample.txt
Kindly need support.. really appreciated
the requirement and the provided sample input/output are a bit fuzzy and somewhat badly formatted.
See how far this will help you:
awk 'NF>1 {$(NF)=$NF OFS $NF}1' myFile
Hi
sed -r 's/\s+\w+$/&&/' myFile
Good when there is an alternative
1 Like
That works with many awk versions.
The following works with all awk versions:
awk '{ if (NF>1) print $0, $7; else print }' myFile
But nobody here yet understood the requirement "in case $7=blank then same value as previous value".
1 Like
MadeInGermany:
That works with many awk versions.
The following works with all awk versions:
awk '{ if (NF>1) print $0, $7; else print }' myFile
@MadeInGermany , do you know what incarnation/version of awk does not handle what was proposed initially?
I cannot recall where this would fail....
That's correct. I couldn't understand what it meant and couldn't correlate it to the sample input file(s).
1 Like
Across the seventh field
sed -r 's/(\s*\S+){7}/&\1/' myFile
produces a funny ORDEREDD with GNU awk 4.0
Perhaps the O/P wants a repetition of the new column when there is no $7? Then consider the following:
awk '(NF==7) { newcol=$7 } { print $0, newcol }' MyFile
Or a bit finer formatted:
awk '(NF==7) { newcol=$7; newpos=length+16 } { printf "%s%*s\n", $0, newpos-length, newcol }' MyFile
@vgersh99 , maybe your code works with all awk versions (besides the Solaris oawk of course), maybe there is only a difference what NF is after the forced reformatting.
But generally I think the forced reformatting is often not the best idea.
awk '$7 {print $0,$7} !$7' myFile
qshaan
March 27, 2020, 9:49am
9
MadeInGermany:
awk '(NF==7) { newcol=$7; newpos=length+16 } { printf "%s%*s\n", $0, newpos-length, newcol }' MyFile
Thanks a lot .. Really appreciate if you explain it as well. as i need slightly variation
here instead $7 need to put condition that newcolumn should be printed if $7 start with ^HLRBC ...
like in below example instead of RESTART value should be "HLRBC03"
ACT 1J AD-300 TIME 200322 1554 HLRBC03 HLRBC03
ORDERED HLRBC03
1 RELOAD COMMAND ORDERED CP CLUSTER RESTART RESTART
ACT 1J AD-300 TIME 200322 1554 HLRBC08 HLRBC08
ORDERED HLRBC08
MadeInGermany:
awk '(NF==7) { newcol=$7; newpos=length+16 } { printf "%s%*s\n", $0, newpos-length, newcol }' MyFile
Replace the condition (NF==7)
(number of fields == 7) with ($7~/^HLRB/)
Also it is smarter to set the field width for the first %s (that is the $0).
awk '( $7 ~ /^HLRB/ ) { newcol=$7; newpos=length } { printf "%-*s %s\n", newpos, $0, newcol }'
%-s
left adjusted
%*s
the * is the field width given by an extra argument
%-*s
left adjusted and field width by argument
2 Likes
qshaan
March 27, 2020, 1:26pm
11
That's great..
Thanks a lot for your support.
qshaan
March 27, 2020, 2:09pm
12
dear MadeIn,
Can we have another new column which has count of number of times same value in column 8
like below
ACT HR1J AD-300 TIME 200322 1554 HLRBC01 HLRBC01 3
ORDERED HLRBC01 HLRBC01 3
HLRBC01 HLRBC01 3
ACT HR1J AD-300 TIME 200322 1554 HLRBC16 HLRBC16 2
ORDERED HLRBC16 HLRBC16 2
qshaan
March 27, 2020, 2:17pm
13
Dear Madelin,
Here i tried and its worked
awk '( $8 ~ /^HLRBC/ ) { newcol=T[$8]++; newpos=length } { printf "%*-s %s\n", newpos, $0, newcol }'