Sunday, November 25, 2012

Beginning of line in UNIX screen session


As you know every "screen" command begins with "Ctrl-a", then how to go to beginning of the line when you are working under a "screen" UNIX session (which we achieve by "Ctrl-a" in a normal UNIX session) ?

Solution:
Under a "screen" UNIX session you can do "Ctrl-a a" to move to the beginning of the line.

So in order to print ^A (hex: \x01) in a UNIX "screen" session, usual  "Ctrl-v Ctrl-a" will not work, you will type "Ctrl-v Ctrl-a a" to print ^A.

Related posts:
- Save command without executing it in Bash history
- Ctrl s freezes UNIX shell and vi editor
- UNIX - prevent exit of shell with Ctrl-d
- Print UNIX command line history with time-stamp of command execution

Wednesday, November 21, 2012

Transpose using UNIX paste command

I have already posted about performing transpose of a matrix using Awk in my earlier posts post1 and post2 . This example shows a not so efficient way of achieving the same.

Input file:
$ cat file.txt 
 PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                                                                                      
32420 testuser 20   0 1219m 969m  49m R   34 12.1   1043:14 worker
Required output:
PID 32420
USER testuser
PR 20
NI 0
VIRT 1219m
RES 969m
SHR 49m
S R
%CPU 34
%MEM 12.1
TIME+ 1043:14
COMMAND worker
$ for i in $(seq 12); do awk -v X=$i '{print $X}' file.txt | paste - - ; done
Related posts:
- How to access external variable in Awk and SED
- Generate bash loop arguments using seq command
- UNIX seq command format option example
- Some good usage of UNIX paste command
- Compare two numeric fields of two files in Bash

Wednesday, November 14, 2012

UNIX - append text to filename using Awk

I have some files in my current directory whose file-name is of this pattern:
$ ls -1
log.1024.94.1326776200.1326776300.172.16.12.6.1326844995.0.s-1326528000.r-8192.txt
log.1024.94.1326776400.1326776400.172.16.12.5.1326844995.0.s-1326528000.r-2234.txt
log.1024.95.1326776420.1326776460.172.16.12.5.1326844995.0.s-1326528000.r-8192.txt
Requirement: Append a text "MY-2" as the 6th field (dot delimited) of the filename. E.g.
 
log.1024.94.1326776200.1326776300.172.16.12.6.1326844995.0.s-1326528000.r-8192.txt 
should be renamed to
log.1024.94.1326776200.1326776300.MY-2.172.16.12.6.1326844995.0.s-1326528000.r-8192.txt
A bash script using awk to achieve this:
for file in $(ls)
 do 
  newfilename=$(echo $file | awk 'BEGIN {FS=OFS="."} {$6="MY-2" OFS $6} {print}')
  mv -v $file $newfilename
done
All the files are renamed to:
$ ls -1
log.1024.94.1326776200.1326776300.MY-2.172.16.12.6.1326844995.0.s-1326528000.r-8192.txt
log.1024.94.1326776400.1326776400.MY-2.172.16.12.5.1326844995.0.s-1326528000.r-2234.txt
log.1024.95.1326776420.1326776460.MY-2.172.16.12.5.1326844995.0.s-1326528000.r-8192.txt
Feel free to post (as comment below) any alternative to this, much appreciated and a big thank you in advance.
Related posts:

- Padding zeros in filename using Bash in UNIX
- Rename file to uppercase except extension - Bash
- Rename multiple files using UNIX rename command

© Jadu Saikia http://unstableme.blogspot.com