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阅读全文
4℃
Reprinted/Referred from: Awk, Nawk and GNU Awk Cheat Sheet
And of course, if you are new to awk, start by reading this Awk introduction tutorial
Awk
AWK is original AWK written by A. Aho, B. W. Kernighan and P. Weinberger.
Nawk
NAWK stands for “New AWK”. This is AT&T’s version of the Awk.
Gawk
GAWK stands for “GNU AWK”. All Linux distributions comes with GAWK. This is fully compatible with AWK and NAWK.
I love cheat sheets. I have at least 30 in front of me at ...
Awk, Cheat Sheet, Gawk, Nawk阅读全文
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阅读全文
2℃
There're at least 4 ways to compare two strings by ignoring cases, and at least 5 ways to calculate the length of string, here I list those 4 and 5 ways, welcome feel free to comment your ideas.
I: Case insensitive when comparing two strings:
1). If you're using bash, then following is a absolutely a good way, open the option "nocasematch":
str1="MATCH"
str2="match"
shopt -s nocasematch
case "$str1" in
$str2) echo "match";;
*) echo "no match";;
esac
2). Another good ...
Awk, grep, Linux, Programme, Strings, tr阅读全文
2℃
本文就进制转换小结一下,欢迎补充……欢迎指正……(printf 就算了……)
注:
I-III部分只涉及bash……
关于其他,请参看IV部分……
I: 利用obase, ibase和bc:
ibase是输入数字的进制,而obase就是输出数字的进制了。很好记,i是input,o是output。
注意,16进制数字的F要大写,否则,结果为0,是错误的。
举例说明:
10进制转2进制:
[15:14:20-Bob@hzling08:~]-(1004)No.4->$ echo "obase=2;ibase=10;255" | bc
11111111
10进制转16进制: (如果输入是10进制的话,ibase可省略)
[15:14:26-Bob@h...
Awk, Base, bc, Binary, dc, Decimal, Hex, Linux, Programme阅读全文
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阅读全文
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阅读全文
2℃
本文已更新至:http://scmbob.org/number_system_conversions.html
最近由于项目的问题,经常需要用到进制转换,这里就进制转换来做一个小结。
1. First let's take “HexBinDecOct” as a e.g, which is using "bc" and "obase":
[10]-tdlteman@hzling05:~ -> echo "obase=16; `seq 9 15`" | bc
9
A
B
C
D
E
F
[12]-tdlteman@hzling05:~ -> echo "obase=16; 30" | bc
1E
2. By using "printf":
[22]-tdlteman@hzling05:~ -> printf "%X\n" 30
1E
3. Awk:
[Bo...
Awk, Base, Binary, Command, Decimal, Hex, Linux, Programme阅读全文














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