Handy Bash oneliner commands for tsv file editing
-
@@ -65,7 +46,12 @@
- grep lines without word (e.g. bbo)
grep -v bbo
grep -v bbo filename
+
+ + grep only one/first match (e.g. bbo)grep -m 1 bbo filename
@@ -126,6 +112,32 @@ grep and count (e.g. bbo)
grep $'\t'
+
+ + grep variable from variable$echo "$long_str"|grep -q "$short_str"
+if [ $? -eq 0 ]; then echo 'found'; fi
//grep -q will output 0 if match found +//remember to add space between []!
+ ++
+ + grep strings between a bracket()grep -oP '\(\K[^\)]+'
+
+ + grep number of characters with known strings in between(e.g. AAEL000001-RA)grep -o -w "\w\{10\}\-R\w\{1\}"
// \w word character [0-9a-zA-Z_] \W not word character
+ ++
+ + a lot examples herehttp://www.cyberciti.biz/faq/grep-regular-expressions/
+@@ -165,11 +177,31 @@ Sed
sed '$d'
+
+ + delete last character from end of filesed -i '$ s/.$//' filename
+
+ + add string to end of file (e.g. "]")sed '$s/$/]/' filename
+
+ + add string to end of each line (e.g. "}")sed -e 's/$/\}\]/' filename
add \n every nth character (e.g. every 4th character)
sed 's/.\{4\}/&\n/g'
+
+ + concatenate/combine/join files with a seperator and next line (e.g seperate by ",")sed -s '$a,' *.json > all.json
@@ -231,6 +263,11 @@ e.g. add the filename to every last column of the file substitution (e.g. replace A by B)
for i in $(ls);do sed -i "s/$/\t$i/" $i;done
+
+ + add extension of filename to last columnfor i in T000086_1.02.n T000086_1.02.p;do sed "s/$/\t${i/*./}/" $i;done >T000086_1.02.np
@@ -241,6 +278,11 @@ e.g. add the filename to every last column of the file remove newline\ nextline
sed -n '10,33p' <filename
+
+ + change delimitersed 's=/=\\/=g'
@@ -292,6 +334,11 @@ awk -v a= Awk"$a
awk -v N=7 '{print}/bbo/&& --N<=0 {exit}'
+
+ + print filename and last line of all files in directoryls|xargs -n1 -I file awk '{s=$0};END{print FILENAME,s}' file'
@@ -375,6 +422,44 @@ fileA: add string to the beginning of a column (e.g add "chr" to column $3)
awk '{printf("%s\t%s\n",NR,$0)}'
+
+ + break combine column data into rowse.g. +seperate
+ +David cat,dog
+ +into
+ +David cat
+ +David dog
+ +detail here: http://stackoverflow.com/questions/33408762/bash-turning-single-comma-separated-column-into-multi-line-string
+ +awk '{split($2,a,",");for(i in a)print $1"\t"a[i]}' file
+
+ + sum up a file (each line in file contains only one number)awk '{s+=$1} END {print s}' filename
+
+ + average a file (each line in file contains only one number)awk '{s+=$1}END{print s/NR}'
+
+ + print field start with string (e.g Linux)awk '$1 ~ /^Linux/'
+
+ + sort a row (e.g. 1 40 35 12 23 --> 1 12 23 35 40)awk ' {split( $0, a, "\t" ); asort( a ); for( i = 1; i <= length(a); i++ ) printf( "%s\t", a[i] ); printf( "\n" ); }'
@@ -977,6 +1062,11 @@ group= 5 Xargs
echo ${#foo}
+
+ + echo tabecho -e ' \t '
@@ -987,6 +1077,11 @@ group= 5 array
scp -r directoryname user@ip:/path/to/send
+
+ + split file into lines (e.g. 1000 lines/smallfile)$ split -d -l 1000 bigfilename