awk - Conditionally print line only when column entry doesn't match previous line's column -



awk - Conditionally print line only when column entry doesn't match previous line's column -

part 1:

so, i've got file (inputfile) looks this:

inputfile unimportant stuff ... col1 col2 col3 26 ace 0 27 ace 0 28 ace 0 ... 32 ccy 1 33 ccy 1 34 ccy 1 ... 42 nme 2 43 nme 2 44 nme 2 ... 48 mmp 3 49 mmp 3 50 mmp 3 ... 54 scy 1 55 scy 1 56 scy 1 ... 65 mmp 2 66 mmp 2 67 mmp 2 ... etc 422 xxx 0 423 xxx 1

desired output

outputfile1 col1 col2 col3 26 ace 0 32 ccy 1 42 nme 2 48 mmp 3 54 scy 1 65 mmp 2

any ideas how approach using awk/sed/grep (some other program) produce desired output? in words, i'm trying develop script start when col1 = 26 , print when col3 changes, until end of file. also, want remove xxx in col2.

part 2:

following this, produce new file (outputfile2) depends on col3 of outputfile1. every time count in col3 resets (or decreases 0/1 , starts counting again) want print outputfile2 like:

outputfile2 26 - 53 ace_ccy_nme_mmp 54 - ... scy_mmp_...

ideally, print

line1: "col1 entry" - "col1 entry minus 1" line2: "all col 2 entries inbetween col2_col2_col2_col2" etc

how best accomplish these results?

part 1 solved:

awk '$1 == "26" {f=1}f {print $0}' inputfile | uniq -f 2 | sed '/xxx/d' > outputfile1

which produces:

26 ace 0 32 ccy 1 42 nme 2 48 mmp 3 54 scy 1 64 mmp 2 ...

explanation: awk prints first instance of finding '26' in col1 until end of file, piped uniq deletes lines have repeat values in col3 (of adjacent lines), sed deletes lines contain unwanted string 'xxx'. if can explain awk {f=1}f part in more detail appreciated?

this first output:

uniq -f 2 input > outputfile1

awk sed grep uniq

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -