Skip to content

Instantly share code, notes, and snippets.

@geoffrepoli
Created June 23, 2017 23:39
Show Gist options
  • Save geoffrepoli/3898d3a45907e7c97bd92101d6692b97 to your computer and use it in GitHub Desktop.
Save geoffrepoli/3898d3a45907e7c97bd92101d6692b97 to your computer and use it in GitHub Desktop.
## REMOVE FIRST CHARACTER FROM STRING
sed '/^.//'
== Example ==
$ echo "foo" | sed 's/^.//'
oo
____________________________________________________________________
## REMOVE FIRST CHARACTER FROM COLUMN N
awk '{print substr($n,2)}'
== Example ==
$ echo "foo bar" | awk '{print substr($2,2)}'
ar
____________________________________________________________________
## REMOVE PATTERN AND PRECEDING CHARACTERS
sed 's/^.*pattern//'
== Example ==
$ echo "foo,bar" | sed 's/^.*,//'
bar
____________________________________________________________________
## REMOVE ALL LEADING WHITESPACE
sed 's/^[ \t]*//'
== Example ==
$ echo " foo" | sed 's/^[ \t]*//'
foo
____________________________________________________________________
## REMOVE LAST CHARACTER FROM STRING
sed 's/.$//'
== Example ==
$ echo "foo" | sed 's/.$//'
fo
____________________________________________________________________
## REMOVE LAST CHARACTER FROM COLUMN N
awk '{print substr($n,1,length($n)-1)}'
== Example ==
$ echo "foo bar" | awk '{print substr($2,1,length($2)-1)}'
ba
____________________________________________________________________
## REMOVE PATTERN AND SUBSEQUENT CHARACTERS
sed 's/pattern.*//'
== Example ==
$ echo "foo,bar" | sed 's/,.*//'
foo
____________________________________________________________________
## USING AWK IN PLACE OF GREP+AWK
# Method 1: For all rows containing pattern print column n
awk '/pattern/{print $n}'
== Example ==
$ cat example.txt
Hello World
foo bar
foo bar
$ awk '/foo/{print $2}' example.txt
bar
bar
# Method 2: For all rows matching pattern in column x, print column n from first match only
awk '/foo/{print $n;exit}'
== Example ==
$ awk '$1=="foo"{print $2;exit}' example.txt
bar
____________________________________________________________________
## PASSING A SHELL VARIABLE TO AWK
# Method 1: Using var to match pattern
awk -v var="$var" '$0~var{print $n}'
== Example ==
$ var=foo \
$ echo "foo bar" | awk -v var="$var" '$0~var{print $2}'
bar
# Method 2: Using var in a condition
awk -v var="$var" '{$n<=>var{print $x}'
== Example ==
$ cat numbers.txt:
1 2
3 4
5 6
7 8
9 0
$ var=5; awk -v var="$var" '$1>=var{print $2}' numbers.txt
6
8
0
____________________________________________________________________
## PRINT AWK OUTPUT ON SAME LINE
awk '{printf "%s ",$n}'
== Example ==
$ cat list.txt
1
2
3
4
$ awk '$1>=2{printf "%s ",$1}' list.txt
2 3 4
____________________________________________________________________
## MATCH STRING EXACTLY, INCLUDING TOTAL CHARACTERS
grep "\<pattern\>"
== Example ==
$ cat test.txt
foo
foob
fooba
foobar
$ grep "foo" test.txt
foo
foob
fooba
foobar
$ grep "\<foo\>" test.txt
foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment