punkwalrus (punkwalrus) wrote,
punkwalrus
punkwalrus

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.

Tags: awk, bash, cli, computer, tech
Subscribe

  • 158 Days with no sugar

    Well, day 158. Thought I'd tell you how it was going. My first big hurdle was Balticon at the end of May, which came and went with little fuss. I…

  • More on sugar-free endurance test

    Like I said in my last post, I am not sure where I am going with this. I kind of expected this to be harder. That being said, I kind of don't want to…

  • Day 35 with no sugar

    I have had odd cravings. Like pineapple juice. I used to hate pineapple juice as a kid, and only barely tolerated it as an adult. Sour and bitter, I…

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments