使用sed和cut的一些小例子

2021-06-27 07:43:40 字數 1907 閱讀 2189

關於sed:

修改第

23行的內容:

刪除最初三行:sed -i '1,3d' reb_test.txt

顯示第二行:sed -n '2p' reb_test.txt

顯示乙個檔案中

1/3/5

行中每行第1~

5個字元:sed -n "1p;3p;5p" reb_test.txt | cut -c 1-5

列印出檔案的前三行:head -n3 reb_test.txt or sed -n "1,3p" reb_test.txtor sed "3q" reb_test.txt

列印出檔案裡面的全部內容:awk '' ./reb_test.txt

/分割的第一部分

($1)

和第三部分

($3)

awk -f "/" '' ./reb_test.txt

顯示檔案的

1/3

awk 'nr==1 || nr==3' reb_test.txt

關於cut:

rebcheng@beleod22 shell $ who | grep rebcheng | head -n 3

rebcheng pts/8        2014-06-30 09:24 (10.220.115.102:6.0)

rebcheng pts/11       2014-08-15 09:33 (10.220.115.102:6.0)

rebcheng pts/58       2014-06-30 12:29 (10.220.115.102:6.0)

rebcheng@beleod22 shell $ who | grep rebcheng | head -n 3 | cut -b 6e

e e

rebcheng@beleod22 shell $ who | grep rebcheng | head -n 3 | cut -b -6

rebche

rebche

rebche

rebcheng@beleod22 shell $ who | grep rebcheng | head -n 3 | cut -d . -f 1

rebcheng pts/8        2014-06-30 09:24 (10

rebcheng pts/11       2014-08-15 09:33 (10

rebcheng pts/58       2014-06-30 12:29 (10

rebcheng@beleod22 shell $ who | grep rebcheng | head -n 3 | cut -d . -f 2

220

220220

[rocrocket@rocrocket programming]$ cat cut_ch.txt

星期一星期二

星期三星期四

[rocrocket@rocrocket programming]$ cut -b 3 cut_ch.txt �

� �

�[rocrocket@rocrocket programming]$ cut -c 3 cut_ch.txt 一

二 三

四看到了吧,用

-c則會以字元為單位,輸出正常;而

-b只會傻傻的以位元組(

8位二進位制位)來計算,輸出就是亂碼。

cut有哪些缺陷和不足:

cut只擅長處理「以乙個字元間隔」的文字內容。在處理多空格時有問題。

java一些小例子

map map new hashmap for iterator iter map.entryset iterator iter.hasnext 陣列的應用 int array system.out.println 顯示陣列 system.out.println arrays.tostring ar...

mysql一些小例子

在群裡遇到有人問了個問題 其表結構如下 img 他想要用sql 如何更新 站號 列各出現的次數到 頻次 列。注 站號對應 name,頻次對應 no 最後用游標實現了他想的功能 create procedure pro hzcount begin declare cur no,y int declar...

學習boost asio一些小例子

if 0 include include include void handler1 const boost system error code ec void handler2 const boost system error code ec boost asio io service io se...