Tested & verified powerful python oneliners
1. Reverse the words in odd position of a string and maintain the title cases for the reversed words.
python -c "for p in [word[::-1].title() if index % 2==0 else word for index,word in enumerate('A Quick Lazy Dogs Jumps Over The Fox'.split()) ]: print p,"
2. Print only the lines of a file whose 3rd field is either of '123','234','245'.
python -c "for p in [ line.strip('\n') for line in open('file.txt').readlines() if int(line.split(',')[2]) in [123,234,245] ]: print p"
--------------OR------------------
cat file.txt | python -c "import sys; [sys.stdout.write(line) for line in sys.stdin if int(line.split(',')[2]) in [123,234,245] ]"
3. Print all the unique words in a file.
python -c "for p in set(word for line in open('file.txt').readlines() for word in line.strip('\n').split(',')): print p, "
4. Awk equivalent: print 3rd field of a file.
python -c "for p in [line.split(',')[2] for line in open('file.txt').readlines() ]: print p"
-------------OR---------
python -c "import sys;[sys.stdout.write(line.split(',')[2]+'\n') for line in sys.stdin]" < file.txt
5. Convert IP to integer
python -c "print sum( [int(i)*2**(8*j) for i,j in zip( '10.20.30.40'.split('.'), [3,2,1,0]) ] )"
6. Convert Interger to IP
python -c "print '.'.join( [ str((169090600 >> 8*i) % 256) for i in [3,2,1,0] ])"
python -c "for p in [word[::-1].title() if index % 2==0 else word for index,word in enumerate('A Quick Lazy Dogs Jumps Over The Fox'.split()) ]: print p,"
2. Print only the lines of a file whose 3rd field is either of '123','234','245'.
python -c "for p in [ line.strip('\n') for line in open('file.txt').readlines() if int(line.split(',')[2]) in [123,234,245] ]: print p"
--------------OR------------------
cat file.txt | python -c "import sys; [sys.stdout.write(line) for line in sys.stdin if int(line.split(',')[2]) in [123,234,245] ]"
3. Print all the unique words in a file.
python -c "for p in set(word for line in open('file.txt').readlines() for word in line.strip('\n').split(',')): print p, "
4. Awk equivalent: print 3rd field of a file.
python -c "for p in [line.split(',')[2] for line in open('file.txt').readlines() ]: print p"
-------------OR---------
python -c "import sys;[sys.stdout.write(line.split(',')[2]+'\n') for line in sys.stdin]" < file.txt
5. Convert IP to integer
python -c "print sum( [int(i)*2**(8*j) for i,j in zip( '10.20.30.40'.split('.'), [3,2,1,0]) ] )"
6. Convert Interger to IP
python -c "print '.'.join( [ str((169090600 >> 8*i) % 256) for i in [3,2,1,0] ])"
Comments