Linux下的小知识点总结_0


最近做的事情都比较零散,不太系统,如果不及时记录下来的话,再隔一段时间去看的话就容易忘记之前处理/解决问题时用到的知识、技巧,又得花时间,所以下面就把近一段时间碰到的问题、想到的方法记录一些,方便后期随时查看。

1.Vim跳转

需实现的功能:“vim 跳至上一位置”

''  #跳转到光标上次停靠的地方,是两个单引号,而不是一个双引号
参考链接:

==

2.C语言基础学习
代码片段:
#include <stdio.h>
#include <stdlib.h>

/*
* return 0: s is not an empty string
* return 1: s is an empty string
* http://stackoverflow.com/questions/3981510/getline-check-if-line-is-whitespace
*/
int str_is_empty(const char *s) {
    while (*s != '\0') {
        if (!isspace(*s))
            return 0;
        s++;
    }
    return 1;
}

void main() {
    char * s;
    const char * a_space = " ";
    const char * a_newline = "\n";
    const char * a_test = " \n\t   ";
    const char * a_str = " a_str ";
    printf("' ' is %s\n", s = str_is_empty(a_space) ? "empty" : "not_empty");
    printf("'\\n' is %s\n", s = str_is_empty(a_space) ? "empty" : "not_empty");
    printf("' \\n\\t  ' is %s\n", s = str_is_empty(a_test) ? "empty" : "not_empty");
    printf("' a_str ' is %s\n", s = str_is_empty(a_str) ? "empty" : "not_empty");
}
参考教程:

==

3.Linux下进行字符串的查找和替换、xargs的-i选项
搜索关键字:
  • Linux find and replace
  • find xargs -i
参考内容:
$ find /tmp/ -ctime -1 -name "x*" | xargs -I '{}' mv '{}' ~/play/
$ file * | awk -F: '/image/ { print $1 }' | xargs -I '{}' mv '{}' destination

$ find /tmp/ -ctime -1 -name 'x*' -print0 | xargs -r0 mv -t ~/play/	# GNU's mv command
$ file * | awk -F: '/image/ { print $1 }' | xargs mv -t destination

$ find . -name "*.bak" -print0 | xargs -0 -I {} mv {} ~/old.files
$ find . -name "*.bak" -print0 | xargs -0 -I file mv file ~/old.files
-I replace-str
--replace[=replace-str]
-i[replace-str]
    Replace occurrences of replace-str in the initial arguments with names read from standard input. Also, unquoted blanks do not terminate arguments; instead, the input is split at newlines only. If replace-str is omitted (omitting it is allowed only for ‘-i’), it defaults to ‘{}’ (like for ‘find -exec’). Implies ‘-x’ and ‘-l 1’. The ‘-i’ option is deprecated in favour of the ‘-I’ option.
参考链接:

==

4.Linux下的Immutable files
搜索关键字:
  • Linux Immutable files
  • Linux lsattr
参考链接:

==

5.Awk的函数使用/学习
搜索关键字:
  • awk substr
  • awk match then substr
  • awk str function remove comma
  • awk function
参考写法:
$ cat file.log | awk -F'|' '/bin/ {split($2, a, " "); print $1,a[1],substr(a[5], 0, length(a[5])-1),a[10]}' > test.log

$ cat file.log | awk -F'|' '/bin/ {split($2, a, " "); print $1,a[1],substr(a[5], 0, length(a[5])-1),a[10]"-"substr(a[11], 0, length(a[11])-2)}' > test.log

$ awk '{dic[$3]++}; END{for(x in dic) print dic[x]"\t"x}' test.log

awk的内建函数split允许你把一个字符串分隔为单词并存储在数组中。你可以自己定义域分隔符或者使用现在FS(域分隔符)的值

格式:

  split (string, array, field separator)

split (string, array)   –>如果第三个参数没有提供,awk就默认使用当前FS值

内建函数substr截取字符串{返回从起始位置起,指定长度的子字符串;若未指定长度,则返回从起始位置到字符串末尾的子字符串}

格式:

  substr(s, p)   返回字符串s中从p开始的后缀部分

substr(s, p, n) 返回字符串s中从p开始长度为n的后缀部分

函数length返回整个记录中的字符数
$ echo "123" | awk '{print length}'
3
$ echo "123456" | awk '{print length}'
6
去掉文件中的utf-8 BOM头
awk '{if(NR==1)sub(/^xefxbbxbf/,"") print}' input.txt > output.txt
用sed/awk去掉行尾的逗号
sed 's/,$//' file.txt > file.nocomma
awk '{print substr($0, 0, length($0)-1)}' file.txt
awk '{gsub(/,$/,""); print}' file.txt
参考链接:

==

6.如何用sed一次进行多个替换操作?
sed -e 's/dog/monkey/g' -e 's/orange/cow/g'
sed -e 's/dog/monkey/g; s/orange/cow/g'

==

7.如何用sed对一个多行字符串进行替换操作?

http://unix.stackexchange.com/questions/26284/how-can-i-use-sed-to-replace-a-multi-line-string

==

8.如何用sed对换行符’\n’进行替换?

http://stackoverflow.com/questions/1251999/how-can-i-replace-a-newline-n-using-sed

Use this solution with sed:

sed ':a;N;$!ba;s/\n/ /g'

This will read the whole file in a loop, then replaces the newline(s) with a space.Explanation:

  1. create a label via :a
  2. append the current and next line to the pattern space via N
  3. if we are before the last line, branch to the created label $!ba ($! means not to do it on the last line (as there should be one final newline)).
  4. finally the substitution replaces every newline with a space on the pattern space (which is the whole file).

==

9.Vim中’^M’字符的替换操作

http://stackoverflow.com/questions/64749/m-character-at-end-of-lines

方法一:
:set fileformat=unix
:w
方法二(常用):
:%s/^M//g

To get the ^M hold the CTRL key, press V then M (Both while holding the control key) and the ^M will appear. This will find all occurrences and replace them with nothing.(如何得到上面的’^M’字符:在按住Ctrl键的同时按下V和M键即可

http://stackoverflow.com/questions/71323/how-to-replace-a-character-for-a-newline-in-vim

==

10.Shell编程中的数组
搜索关键字:

bash array

参考内容:
#!/bin/bash

users="one two three"
arr_s=("/etc/passwd" "/etc/group" "/etc/hosts")
limits=( 10,20,26,39,48 )

for ix in ${users[@]}; do
	echo $ix
done

for iy in ${arr_s[@]}; do
	echo $iy
done

for iz in ${limits[@]}; do
	echo $iz
done
bash中的数组/循环

如果希望在上一个循环中得到的结果可以提供给后面的功能用,有两种方法:

1.数组

用declare -A声明

用圆括号括起来

2.以空格连接的字符串{本质上也是一种数组}
users="one two three"
arr_s=("/etc/passwd" "/etc/group" "/etc/hosts")

echo "Total counts of array : ${#users[*]}"
echo "Total counts of array : ${#arr_s[*]}"
参考链接:

==

11.如何在Linux下创建一个“不可变更的”文件

可以使用chattr和lsattr命令来管理额外的文件标志位,来避免文件被篡改(意外或者其他情况)。但要注意你不能将chattr作为一个安全措施,因为“不可变更”标志位可以很容易被取消掉。解决这个问题的一个可能的方式是限制chattr命令自身的可用性,或者去掉CAP_LINUX_IMMUTABLE内核功能。关于chattr以及可用的标志位的更多细节,请参考它的man手册。

搜索关键字:

Linux how to limit a command’s execute user

参考链接:
, ,

《“Linux下的小知识点总结_0”》 有 4 条评论

  1. 来自Unix/Linux的编程启示录
    http://blog.csdn.net/dd864140130/article/details/54922319
    `
    原则一:小即是美
    原则二:让每个程序只做好一件事情
    原则三:建立原型
    原则四:舍弃高效率而取可移植性
    原则五:使用纯文本来存储数据
    原则六:利用软件的杠杆效应
    原则七:使用shell脚本来提高杠杆效应和可移植性
    原则八:避免强制性的用户界面
    原则九:让每个程序都成为过滤器
    原则十:并行思考
    原则十一:各部分之和大于整体
    原则十二:寻找90%的解决方案
    原则十三:层次化思考
    `

  2. Linux C编程一站式学习
    https://www.kancloud.cn/wizardforcel/linux-c-book/134922
    http://akaedu.github.io/book/

    Standard C 语言标准函数库速查 (Cheat Sheet)
    http://ganquan.info/standard-c/

    Linux C 函数 使用手册
    http://www.iteedu.com/os/linux/linuxprgm/linuxcfunctions/

    Section 3: library functions – Linux man pages
    https://linux.die.net/man/3/

    C语言标准库
    http://c.biancheng.net/cpp/u/biaozhunku/

    C语言常见问题集锦【你必须知道的495个C语言问题】
    http://c.biancheng.net/cpp/u/wenti/
    http://www.cprogramming.com/tutorial/c-tutorial.html

    50个C语言经典小程序和C语言编写的小游戏,带注释和解析
    http://c.biancheng.net/cpp/u/yuanma/

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注