I have lines with a single :
and a'
in them that I want to get rid of. I want to use awk
for this. I've tried using:
awk '{gsub ( "[:\\']","" ) ; print $0 }'
and
awk '{gsub ( "[:\']","" ) ; print $0 }'
and
awk '{gsub ( "[:']","" ) ; print $0 }'
non of them worked, but return the error Unmatched ".
. when I put
awk '{gsub ( "[:_]","" ) ; print $0 }'
then It works and removes all :
and _
chars. How can I get rid of the '
char?
awk has two functions; sub and gsub that we can use to perform substitutions. sub and gsub are mostly identical for the most part, but sub will only replace the first occurrence of a string. On the other hand, gsub will replace all occurrences.
awk '{ print $2; }' prints the second field of each line. This field happens to be the process ID from the ps aux output. xargs kill -${2:-'TERM'} takes the process IDs from the selected sidekiq processes and feeds them as arguments to a kill command.
This method works as our given string pipes with the awk and then in awk, the string is processed. Here the length($0)-1 means removing the last character by deducting '1' from the string's total length. Through this process, the command will print the string from the 1st character up to the 2nd character.
The AWK Field Separator (FS) is used to specify and control how AWK splits a record into various fields. Also, it can accept a single character of a regular expression. Once you specify a regular expression as the value for the FS, AWK scans the input values for the sequence of characters set in the regular expression.
tr
is made for this purpose
echo test\'\'\'\':::string | tr -d \': teststring $ echo test\'\'\'\':::string | awk '{gsub(/[:\47]*/,"");print $0}' teststring
You could use:
Octal code for the single quote:
[:\47]
The single quote inside double quotes, but in that case special characters will be expanded by the shell:
% print a\': | awk "sub(/[:']/, x)" a
Use a dynamic regexp, but there are performance implications related to this approach:
% print a\': | awk -vrx="[:\\\']" 'sub(rx, x)' a
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With