Is there a way to write multiple awk commands on a single line?

verlager

Is there a clear method to write the following with pipes avoiding the temp file(s) redirection?

awk '{gsub("Sat ", "Sat. ");print}' Sat.txt >win.txt
awk '{gsub(" 1-0", "");print}' win.txt >loss.txt
awk '{gsub(" 0-1", "");print}' loss.txt >draw.txt
awk '{gsub(" 1/2-1/2", "");print}' draw.txt >$TARGET
fedorqui 'SO stop harming'

You can have as many gsub() as you want in awk. Each one of them will replace $0, so every time you will be working with the modified string.

However, note you can compress the gsub() into just two of them by using some regular expressions:

awk '{gsub("Sat ", "Sat. "); gsub(/ (1-0|0-1|1\/2-1\/2)/, "")}1' file
#                                    ^^^ ^^^ ^^^^^^^^^
#                                    1-0 0-1  1/2-1/2

The first one replaces Sat with Sat. and the second one removes space + either of 1-0, 0-1 or 1/2-1/2.

Test

$ cat a
hello Sat is now and 1-0 occurs there when 0-1 results happen but 1/2-1/2 also
bye
$ awk '{gsub("Sat ", "Sat. "); gsub(/ (1-0|0-1|1\/2-1\/2)/, "")}1' file
hello Sat. is now and occurs there when results happen but also
bye

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AWK in single line pass multiple commands

From Dev

Multiple commands on a single line in Linux

From Dev

Make bash awk write in single line with separators

From Dev

How to merge multiple piped awk commands into a single awk command

From Dev

KIVY language: multiple commands in a single line

From Dev

Passing an argument to multiple commands in a single line

From Dev

Bash - sed multiple commands in single line

From Dev

Cleaner way to write multiple sed commands?

From Dev

Single line with multiple commands using Windows batch file

From Dev

Store the output from multiple commands on a single line in a file

From Dev

How can I execute multiple find commands in a single line?

From Dev

Write output of multiple batch commands to one line of a text file

From Dev

Concatenate multiple commands and write them in one line to a log file

From Dev

Piping to multiple awk commands

From Dev

awk one single line

From Dev

Is there a way to write a single line javadoc comment, like in doxygen /**<*/

From Dev

How to write multiple lines of code in Python as a single line?

From Dev

how to write multiple variables to a single LCD display line?

From Dev

Is there a way to set patch color for multiple patches with a single line of code in NetLogo?

From Dev

How can I write a single command line that launches a new docker container with interactive bash and executes a few commands in it?

From Dev

single line group of commands as individual?

From Dev

single line group of commands as individual?

From Dev

Can I grep/awk/sed a line for multiple matches in a single line and get the info after it?

From Dev

Parse single log line with awk

From Dev

AWK Multilines into Single line record

From Dev

Combining two awk commands in single command

From Dev

Combining two awk commands in single command

From Dev

write sed commands to multiple lines

From Dev

How to execute the multiple commands with && operator in new lines(not all in single line) :shell script

Related Related

  1. 1

    AWK in single line pass multiple commands

  2. 2

    Multiple commands on a single line in Linux

  3. 3

    Make bash awk write in single line with separators

  4. 4

    How to merge multiple piped awk commands into a single awk command

  5. 5

    KIVY language: multiple commands in a single line

  6. 6

    Passing an argument to multiple commands in a single line

  7. 7

    Bash - sed multiple commands in single line

  8. 8

    Cleaner way to write multiple sed commands?

  9. 9

    Single line with multiple commands using Windows batch file

  10. 10

    Store the output from multiple commands on a single line in a file

  11. 11

    How can I execute multiple find commands in a single line?

  12. 12

    Write output of multiple batch commands to one line of a text file

  13. 13

    Concatenate multiple commands and write them in one line to a log file

  14. 14

    Piping to multiple awk commands

  15. 15

    awk one single line

  16. 16

    Is there a way to write a single line javadoc comment, like in doxygen /**<*/

  17. 17

    How to write multiple lines of code in Python as a single line?

  18. 18

    how to write multiple variables to a single LCD display line?

  19. 19

    Is there a way to set patch color for multiple patches with a single line of code in NetLogo?

  20. 20

    How can I write a single command line that launches a new docker container with interactive bash and executes a few commands in it?

  21. 21

    single line group of commands as individual?

  22. 22

    single line group of commands as individual?

  23. 23

    Can I grep/awk/sed a line for multiple matches in a single line and get the info after it?

  24. 24

    Parse single log line with awk

  25. 25

    AWK Multilines into Single line record

  26. 26

    Combining two awk commands in single command

  27. 27

    Combining two awk commands in single command

  28. 28

    write sed commands to multiple lines

  29. 29

    How to execute the multiple commands with && operator in new lines(not all in single line) :shell script

HotTag

Archive