随笔分类 -  Linux

摘要:1、建立文件abbr,用以保存目标服务器简称与全称的对应关系,例如:yang@fengtao ~ $ more abbr#productpp1 p1.prodcut1.server.compp2 p2.prodcut1.server.compp3 p3.prodcut1.server.compp4 p4.prodcut1.server.com2、建立可执行文件autologin.sh,例如:yang@fengtao ~ $ more autologin.sh #!/usr/bin/expectset target [lindex $argv 0]spawn ssh $targetexpec.. 阅读全文
posted @ 2013-10-22 15:51 风*依旧 阅读(305) 评论(0) 推荐(0)
摘要:1、System FunctionYou can use the system built-in function to execute systemcommands. Please note that there is a difference between two waycommunication and system command.In "|&", you can pass the output of any awk command as input to anexternal command, and you can receive the output 阅读全文
posted @ 2013-09-10 21:01 风*依旧 阅读(359) 评论(0) 推荐(0)
摘要:1、Bit ManipulationJust like C, awk can manipulate bits.AND andOR or XOR xorComplement complLeft Shift lshiftRight Shift rlshiftAwk Example using Bit Functions$ cat bits.awkBEGIN { number1=15 number2=25 print "AND: " and(number1,number2); print "OR: " or(number1,number2) print &qu 阅读全文
posted @ 2013-09-09 17:17 风*依旧 阅读(341) 评论(0) 推荐(0)
摘要:1、Argument Processing (ARGC, ARGV, ARGIND)The built-in variables we discussed earlier, FS, NFS, RS, NR,FILENAME, OFS, and ORS, are all available on all versions of awk(including nawk, and gawk).• The environment variables discussed in this hack are availableonly on nawk and gawk.• Use ARGC and ARGV 阅读全文
posted @ 2013-09-09 17:03 风*依旧 阅读(424) 评论(0) 推荐(0)
摘要:4、Generic String FunctionsIndex FunctionThe index function can be used to get the index (location) of thegiven string (or character) in an input string.You can also use index to check whether a given string (or character)is present in an input string. If the given string is not present, it willretur 阅读全文
posted @ 2013-09-06 15:56 风*依旧 阅读(344) 评论(0) 推荐(0)
摘要:1、Pretty Printing Using printfSyntax:printf "print format", variable1, variable2, etc.Special Characters in the printf FormatThe following prints "Line 1" and "Line 2" in separate lines using newline:$ awk 'BEGIN { printf "Line 1\nLine 2\n" }'Line 1Lin 阅读全文
posted @ 2013-07-16 21:35 风*依旧 阅读(675) 评论(0) 推荐(0)
摘要:1、Assigning Array ElementsIn Awk, arrays are associative, i.e. an array contains multiple index/value pairs. The index doesn't need to be a continuous set of numbers; in fact it can be a string or a number, and you don't need to specify the size of the array.Syntax:arrayname[string]=valuearr 阅读全文
posted @ 2013-07-10 21:28 风*依旧 阅读(303) 评论(0) 推荐(0)
摘要:1、Simple If Statement语法:if (conditional-expression)action如果是多个action,则语法如下:if (conditional-expression){action1;action2;}Print all the items with quantity <=5:$ awk -F "," '{ if ($5 <= 5) \print "Only",$5,"qty of",$2, "is available"; }' \items.txt 阅读全文
posted @ 2013-06-28 18:53 风*依旧 阅读(416) 评论(0) 推荐(0)
摘要:1、VariablesYou don't need to declare anvariable to use it. If you wish to initialize an awk variable, it is betterto do it in the BEGIN section, which will be executed only once.There are no data types in Awk. Whether an awk variable is a numberor a string depends on the context in which the var 阅读全文
posted @ 2013-06-19 18:42 风*依旧 阅读(475) 评论(0) 推荐(0)
摘要:1、FS - Input Field Separatorawk处理文档时,默认的域分隔符为空格,所以如果你的输入文件的域分隔符不是空格,可以通过-F选项来指定分隔符,如下所示:awk -F ',' '{print $2, $3}' employee.txt我们也可以使用awk内置变量FS来设置分隔符,需要在BEGIN块里设置:awk 'BEGIN {FS=","} {print $2, $3}' employee.txt我们还可以指定多个域分隔符,例如存在以下记录文件,其中的每条记录包含3个不同的域分隔符:逗号、冒号和百分号: 阅读全文
posted @ 2013-06-07 16:34 风*依旧 阅读(539) 评论(0) 推荐(0)
摘要:awk是一个操作处理文本文件的强大工具,尤其是处理记录型的文本,也就是每行文本包含多个用分隔符分隔的域。甚至在没有输入文本的情况下也可以做一些逻辑处理。在接下来的示例中,我们会多次用以下的文档作为操作的对象:employee.txt is a comma delimited file that contains 5 employeerecords in the following format:employee-number,employee-name,employee-titleCreate the file:$ vi employee.txt101,John Doe,CEO102,Jaso 阅读全文
posted @ 2013-06-05 11:31 风*依旧 阅读(350) 评论(0) 推荐(0)
摘要:1、追加下一行文本到Pattern Space中(N command)正如大写的H和G命令是追加而非替代,大写的N命令也是把下一行文本追加到Pattern Space中,而非替换原Pattern Space中的内容。我们先前讨论过,小写的n命令是打印当前的Pattern Space中的内容,清空Pattern Space中的内容,读取下一行内容到Pattern Space中,然后恢复它接下来的命令流程。但是大写的N命令,不会打印当前Pattern Space中的内容,也不会清除Pattern Space;它会在Pattern Space中的当前文本后添加一个新行(\n),从输入文件中读取下一行 阅读全文
posted @ 2013-04-11 16:31 风*依旧 阅读(605) 评论(0) 推荐(0)
摘要:Sed 有两种内部存储空间:Pattern Space:sed执行流程中,将从文件中读取的文本行放置到这个空间,并在此空间中修改文本。Hold Space:这是sed额外的一个可用的缓冲空间,sed可以用之来存放临时数据。Sed允许我们将数据在Pattern Space和Hold Space间来回移动,但是无法在Hold Space中的数据上执行传统的sed 命令。我们已经知道,Pattern Space中的数据在每次的sed执行循环后会清空,但是Hold Space中的数据不会被清空。请创建一个以下的文本来作为示例使用:$ vi empnametitle.txt John Doe CEO J 阅读全文
posted @ 2013-04-08 17:18 风*依旧 阅读(335) 评论(0) 推荐(0)
摘要:1、行后追加命令 a (Append Line After)可以在指定的行后追加一行或多行文本,其基本语法为:$ sed '[address] a the-line-to-append' input-file示例,在最后一行后添加一行文本:$ sed '$ a 106,Jack Johnson,Engineer' employee.txt 101,John Doe,CEO 102,Jason Smith,IT Manager 103,Raj Reddy,Sysadmin 104,Anand Ram,Developer 105,Jane Miller,Sales 阅读全文
posted @ 2013-03-29 12:27 风*依旧 阅读(387) 评论(0) 推荐(0)
摘要:1、正则表达式基础Begining of line (^)$ sed -n '/^103/ p' employee.txt 103,Raj Reddy,Sysadmin End of line ($)$ sed -n '/r$/ p' employee.txt 102,Jason Smith,IT Manager 104,Anand Ram,Developer 105,Jane Miller,Sales Managersingle Character (.)$ sed -n 's/J... /Jason /p' employee.txt 101, 阅读全文
posted @ 2013-03-28 15:24 风*依旧 阅读(317) 评论(0) 推荐(0)
摘要:我们一般情况下使用最频繁的sed命令便是替换命令:s1、基本语法:sed '[address-range|pattern-range] s/original-string/replacement-string/[substitute-flags]' inputfile 地址范围address-range和模式范围pattern-range是可选的,如果没有指定则会在所有的行上执行替换命令s - 告诉sed去执行替换命令original-string - 需要被搜索的字符串,可以为正则表达式replacement-string - sed将会使用这个字符串来替换 original 阅读全文
posted @ 2013-03-27 16:54 风*依旧 阅读(871) 评论(0) 推荐(0)
摘要:1、What's sed? 如果你是一个开发、系统管理员或者数据库管理员又或者是it管理员,或者只是一个经常在unix/linux环境下工作的人,你应该掌握sed和awk。 Sed→Stream Editor,它是一个非常强大的工具,可以用来操作、过滤和转换文本。Sed可以从文件中获取输入,也可以从 阅读全文
posted @ 2013-03-26 11:57 风*依旧 阅读(530) 评论(0) 推荐(0)