0

RHEL 7 system. I have an output file that looks like

eDTG=20210210-1540
Sent     74004956
Completed     72185529 (97.54%)
Lost     18119427 (2.46%)
QPS, SERVFAIL QPS     60122 2905
eDTG=20210210-1601

I've been using sed to change the spacing to tabs for saving as a csv. Now it turns out we need to break the QPS line to 2 separate lines to look like this (note the numbers split between the lines)

eDTG=20210210-1540
Sent     74004956
Completed     72185529 (97.54%)
Lost     18119427 (2.46%)
QPS     60122
SERVFAIL QPS     2905    
eDTG=20210210-1601

I can do this with

awk '/QPS/ {printf "QPS\t%s\nSERVFAIL QPS\t%s",$4,$5}' output.txt

But gawk -i inplace is not available so i have the issue with writing to another file. Well of course it only printed the matched line if i didn't put in the ; {print} option. And then it writes the old line as well. I know there has to be a better way to do this than creating the second file and then copying it back to the first filename (cutting the QPS, ...). I would appreciate any help you can give.

2
  • 1
    gawk -i inplace also use temporary file for the inplace job and sed -i as well – αғsнιη Feb 11 at 20:27
  • 1
    awk '/^QPS,/ {printf "QPS\t%s\nSERVFAIL QPS\t%s\n",$4,$5;next} 1' output.txt > output.txt.1 && mv output.txt.1 output.txt – Uncle Billy Feb 11 at 20:30
0

sed could help you more.

sed -E -i 's/^QPS,[[:space:]]+SERVFAIL QPS[[:space:]]+([[:digit:]]+)[[:space:]]([[:digit:]]+$)/QPS\t \1\nSERVFAIL QPS\t \2/' file.txt

the -i flags make the sobstitution in place.

6
  • Your substitution is not tab separated as OP needs. – schrodigerscatcuriosity Feb 11 at 23:38
  • gnu-sed says: Space characters: in the ‘C’ locale, this is tab, newline, vertical tab, form feed, carriage return, and space. – YetAnotherUser Feb 11 at 23:46
  • The substitution is not a regular expresion. – schrodigerscatcuriosity Feb 11 at 23:50
  • Yes ur right. I've edited – YetAnotherUser Feb 11 at 23:55
  • As pointed out in a comment under the question, the -i option does indeed use a temporary file, which OP doesn't seem to like. However, it is unlikely that there is a tool that can perform this task without temporary storage. – berndbausch Feb 12 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.