Skip to content

Instantly share code, notes, and snippets.

@dzlab
Last active April 14, 2016 16:29
Show Gist options
  • Save dzlab/343bde060b83e885bbfb to your computer and use it in GitHub Desktop.
Save dzlab/343bde060b83e885bbfb to your computer and use it in GitHub Desktop.
Some useful text manipulation commands (sed, gawk, etc.)
# print the number of columns
gawk '{print NF}' file | sort -nu | tail -n 1
# Use head -n 1 for lowest column count, tail -n 1 for highest column count.
# print columns
gawk '{print $0}' file # all columns
gawk '{print $4}' file # 4th column
gawk -F "," '{print $2}' file # specific separator
# split a string with a delimiter into lines
echo "string1:string2:string3:string4:string5" | sed s/:/\\n/g
# remove first line from a file
sed '1d' file.txt > tmpfile; mv tmpfile file.txt # POSIX
sed -i 's/original_regex/new_regex/g' file.txt # replace text within a file
sed -i.bak '/pattern to match/d' file.txt # remove lines with matched pattern
sed -i '1d' file.txt # GNU sed only, creates a temporary file
sed 's/.*/&&/g' file.txt > file.enum # duplicate matched pattern
paste -d, a.csv b.csv # merge lines of many files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment