?

Log in

No account? Create an account
 
 
14 December 2012 @ 09:27 am
Some bash command line tricks I wish I would remember: Part 2  
My new job has me learning a lot of new bash stuff by neccessity. Here's some new stuff I have learned, and wanted to jot down.

I am using awk, but I need to use more than one field separator

awk -F "=|," 
I wish I had found this earlier, and I searched for it, but never found what I was looking for because my Google-fu was weak. I stumbled upon it quite by accident. Here's what I used to do: Suppose I wanted to cut out some text and isolate it, like the system times from a dmesg dump for my sda deveice.
glarson@shrm-excelsior:~$ grep sda  /var/log/dmesg
[    1.170644] sd 0:0:0:0: [sda] 488397168 512-byte logical blocks: (250 GB/232 GiB)
[    1.170945] sd 0:0:0:0: [sda] Write Protect is off
[    1.170950] sd 0:0:0:0: [sda] Mode Sense: 00 3a 00 00
[    1.171077] sd 0:0:0:0: [sda] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[    1.220626]  sda: sda1 sda2 < sda5 >
[    1.221922] sd 0:0:0:0: [sda] Attached SCSI disk
[    3.241926] EXT4-fs (sda1): mounted filesystem with ordered data mode. Opts: (null)
[    7.291400] Adding 6273020k swap on /dev/sda5.  Priority:-1 extents:1 across:6273020k
[    7.505029] EXT4-fs (sda1): re-mounted. Opts: errors=remount-ro
Then I'd have to do this:
glarson@shrm-excelsior:~$ grep sda  /var/log/dmesg | awk -F'[' '{print $2}' | awk -F']' '{print $1}'
    1.170644
    1.170945
    1.170950
    1.171077
    1.220626
    1.221922
    3.241926
    7.291400
    7.505029
Now I can do this:
glarson@shrm-excelsior:~$ grep sda  /var/log/dmesg | awk -F"\[|\]" '{print $2}'
    1.170644
    1.170945
    1.170950
    1.171077
    1.220626
    1.221922
    3.241926
    7.291400
    7.505029

I have this long-assed command, I'd like to put it in an editor but not save it as a script

ctrl-x-e
This brings up whatever editor your shell has. I found vi to work the best, but some clown inists on nano for Debioan distros, so...

How do I change my default CLI editor?

export EDITOR=/usr/bin/vim 
(put in .bashrc to make permenant)

I want to replace a whole line based on one match string

sed -e "s/.*FARISLAND.*/###########/" cland.txt
In your sed statement, put "dot star" at each end of the search string. It will replace that line with the replacement. In that example, anything line that has "FARIS" in it will be replaced etriely with ########### My termian gives me a lot of " â " when I try and draw lines
 set your terminal to UTF-8
The ISO standard emulation fonts mess up ASCII 256 graphics.

Maybe this will help others.