4℃
我们就以下面的数据为例吧:
$ cat data
123456
12
123
1234
要求找出data文件最长的行(也就是123456)以及最短的行(也就是12)并打印。用awk和sed实现。
I. AWK实现:
毋庸置疑,此题用awk很简单,如下:
1). 最短的行:
[Bob]@[Fck_without_U]-> awk '(NR==1||length(min)>length()){min=$0}END{print min}' data
12
2). 最长的行:
[Bob]@[Fck_without_U]-> awk '{if (length(max)<length()) max=$0}END{print max}' data
123456
II. SED实现:
重点说下sed的实...
Awk, Linux, Sed, Shell阅读全文
5℃
跨行匹配在sed里算不上是个难点,但绝对能考验一个人的sed基础……
这里就以《sed & awk》一书中的例子(高级sed命令章节)作为对象,或者直接参看CU的这篇帖子:http://bbs.chinaunix.net/thread-1685452-1-1.html,具体分析一下代码思路和执行过程,我也是大菜鸟,有不对的地方,还望各位指出……
首先明确一下几个概念:
b label
Branch to label; if label is omitted, branch to end of script.
h H Copy/append pattern space to hold space.
g G Copy/append hold space to pattern space.
D Del...
Linux, Programme, Sed, Unix阅读全文
5℃
Today I want share with you various ways(what I can call to mind are at least 10 ways) to combine two lines to one line...Those ways are used by awk, sed, xargs, paste, etc...
Pls feel free to give your comments and welcome to point out if I missed something or I got something wrong...Thanks...
Following is an original file named urfile(notes that there're blank spaces at the beginning of even lines):
[12:37:20-Bob@hzling20:~/test]-(1083)No.83->$ cat urfile
AA
...
Awk, Combine, Linux, paste, Programme, Sed, xargs阅读全文
7℃
虽然在CU-Shell版置顶帖里上传了很多关于sed和awk的经典书籍:
http://bbs.chinaunix.net/thread-1776727-1-1.html
不过很惭愧,自己没看过几本,倒不是因为不想看,实际上工作中全然用不到这些高级用法,一个原则,能干活就行……
不过现在看来,这样是不行滴,至少坛子里的很多问题都帮不上忙,对自己的提高也是一种滞后……
这样就引出了这篇帖子的主题,这个问题是之前在浏览“白云苍狗”兄的旧帖子里,看到一个sed的回复而引申出来的:http://bbs.chinaunix.net/redirec ... 1354674&pid=9947995
以下是一些简...
Linux, Pattern, Programme, Sed阅读全文
2℃
文章最后已经update了awk的几种方法,感谢黑哥,yinyuemi等大拿……
首先声明一下,写这篇文章的重点不是介绍我的实现方法,真正的目的是希望各位前辈,各位大拿们能够给出其他的方案(感觉perl处理起来应该也和shell大同小异,所以我个人还是比较期待纯awk来实现),希望浏览此文的人能否发挥自己的想象,给出方案,互相学习,thx...
Luma是我的一位来自QQ群的朋友,前两天他碰到了以下这个问题:
如何把如下文本
172.20.0.9 114.80.92.95 114.80.92.85 114.80.92.39 E4 114.80.92.9
172.20.0.9 114....
Array, Awk, Linux, Programme, Sed阅读全文
0℃
Here I list a example to show how to change the first letter of all the words from lower-case to upper-case by using sed:
Example:
xiabao@6P9SN2X /home/xiaopan$ cat urfile
#!/bin/bash
if [ $# == 0 ]
then
echo "usage: search.sh -f filename -t target -o output";
fi
xiabao@6P9SN2X /home/xiaopan$ sed 's/\b[^ ]*\b/\u&/g' urfile
#!/Bin/bash
If [ $# == 0 ]
Then
Echo "Usage: Search.sh -F Filename -T Target -O Output";
Fi
But, as you can see, there's still a problem: the...
Command, Sed阅读全文
2℃
We can use awk and sed, or even vi to give some ways to show how to generate the odd and even numbers, see bellow:
I. Shell commands:
Odd numbers:
seq 1 100|sed -n 'p;n'
seq 1 10|gawk 'NR%2'
Even numbers:
seq 1 100|sed -n 'n;p'
seq 1 10|gawk '!(NR%2)'
II. In vi/vim, we can use two ways:
1. By invoking external command(awk, sed):
:%!sed -n 'p;n'
:%!awk 'NR%2'
2. Modify directly:
vi file <<END
:g/^/.+1d
:wq
END
#"." means cur...
Awk, Sed, Vim阅读全文
3℃
Pls also see my other 2 posts about:
1. The emacs pictures: http://scmbob.org/emacs_manual_pictures.html
2. The vim pictures: http://scmbob.org/vim_mind_mapping.html
Here I list some of very useful technical pictures, you must be like them.
1. Unix history:
2. Following is the login frame of my laptop cygwin:
3. Thinkabout Sed:
4. Perl REGEX:
5-6. Thinkabout Awk:
7. Thinking iptables:
8. Cron:
9. Git structure:
Awk, Crontab, Git, Iptables, Linux, Perl, Programme, Sed, Unix阅读全文











谢谢分享
赶紧学习一下,谢谢!
谢谢 希望能下
好東西! 下來試一下。
谢谢你,我的问题已经解决了,
评论啊,你太伟大了
感谢分享,不知道现在还能下载
谢谢博主分享!