These are Perl implementations of the examples used in Awesome awk oneliners. See perl1line.txt for a very educational set of Perl code in this vein.
Replace multiple spaces with single space #
command | awk '{ gsub(/[ ]+/," "); print }' # awk
command | perl -pE 's/\s+/ /g' # perl
Explanation #
- -p tells perl to process & print each line
- -E tells perl to run a line of code
- s/PATTERN/REPLACEMENT/options searches and replaces text.
/\s+/
matches one or more whitespace characters/ /
is a single space to replace the matchg
is an option to find all occurences
Trim leading and trailing spaces #
command | awk '{ gsub(/^[ \t]+|[ \t]+$/, ""); print }' # awk
command | perl -pE 's/^\s+|\s+$//g' # perl
Explanation #
- -p tells perl to process & print each line
- -E tells perl to run a line of code
- s/PATTERN/REPLACEMENT/options searches and replaces text.
/^\s+|\s+$/
includes two patterns separated by a|
and matches the first OR second pattern:^\s+
matches one or more whitespace characters at the beginning of a line\s+$
matches one or more whitespace characters at the end of a line
//
is an empty replacement, i.e. remove any matches from the lineg
is an option to find all occurences
Replace string with another one only in lines containing specific string #
command | awk '/,/{gsub(/ /,""); print}' # awk
command | perl -pE 'if (m/,/) { s/ //g }' # perl
command | perl -pE 'if (/,/) { s/ //g }' # perl - remove optional m
command | perl -pE 's/ //g if (/,/)' # perl - even more concise
Explanation #
- -p tells perl to process & print each line
- -E tells perl to run a line of code
- s/PATTERN/REPLACEMENT/options searches and replaces text.
/ /
matches a single space//
is an empty replacement, i.e. remove any matches from the line
- m/PATTERN/options searchs a string for a pattern, returning true it succeeds
/,/
matches a commam
is optional if the delimiter is/
Print lines where column equals values based on conditions #
command | awk -F, '(tolower($6)~"rhel" || tolower($6)~"linux"){print $0}' # awk
command | awk -F, '(tolower($6)~"rhel|linux"){print $0}' # awk
command | perl -F, -naE 'print if ($F[5] =~ /rhel|linux/i)' # perl
Explanation #
- -F specifies "," as the column separator to split the line
- -n tells perl to process each line
- -a turn on autosplit mode, splitting fields to @F
- -E tells perl to run a line of code
($F[5] =~ m//)
compares the sixth (6th) field to the regexp/rhel|/linux/i
matches "rhel" or "linux"- i option specifies a case-insenstive patten match
Example #
$ { cat <<EOF
1,2,3,4,5,6,7
1,2,3,4,5,rhel,6,7
1,2,3,4,5,Linux,6,7
EOF
} | perl -F, -naE 'print if ($F[5] =~ /rhel|linux/i)'
1,2,3,4,5,rhel,6,7
1,2,3,4,5,Linux,6,7
Print specific column numbers #
command | awk -F, '{print $1,$2,$7}' # awk
command | perl -F, -naE 'print "$F[0],$F[1],$F[6]\;n' # perl
Explanation #
$F[0], $F[1], $F[6]
specifies the 1st, 2nd, and 7th column of the line
Replace first occurrence of string #
command | awk '{sub(/,/,"1,")}1' # awk
command | perl -pE 's/,/1,/' # perl
Example #
1$ echo "foo,bar,baz" | perl -pE 's/,/1,/'
2foo1,bar,baz
Replace last occurrence of string #
command | rev | awk '{sub(/,/,"1,")}1' | rev # awk
command | rev | perl -pE 's/,/1,/' | rev # perl
command | perl -pE 's/(.*),/${1},new-text/' # perl - no other commands required
Explanation #
The final example does more with regular expressions.
(.*)
captures everything matched by the expression in the parenthesis (.*
is any string of characters) into a variable that can be used in the replacement operation./(.*),/
matches the longest possible string followed by a,
capturing the string as$1
./$1,1
replaces the match with the string, a,
, andnew-text
.
Example #
1$ echo "foo,bar,baz" | rev | awk '{sub(/,/,"1,")}1' | rev
2foo,bar,1baz
3$ echo "foo,bar,baz" | perl -pE 's/(.*),/$1,new-text/'
4foo,bar,new-textbaz