UNIX: Some useful commands

本文介绍了两种实用的文件处理技巧:一是如何在指定文件类型中查找特定字符串;二是如何从大型文件中提取指定范围内的行内容。这些技巧对于日常开发工作非常有用。
1. To search a string from the content of files.
E.g. Search 'logo.gif' from all jsp,html and xsl files under current folder

find . \( -name '*.jsp' -o -name '*.html' -o -name '*.xsl' \) -exec grep -l logo.gif {} \;


2. To get lines in a certain range from file which is huge
E.g. Get the lines from 20 to 2000 from stdout.txt

sed -n '20,2000p' stdout.txt > test.txt
新的实验: xargs (moderate) Write a simple version of the UNIX xargs program for xv6: its arguments describe a command to run, it reads lines from the standard input, and it runs the command for each line, appending the line to the command's arguments. Your solution should be in the file user/xargs.c. The following example illustrates xarg's behavior: Note that the command here is "echo bye" and the additional arguments are "hello too", making the command "echo bye hello too", which outputs "bye hello too". $ echo hello too | xargs echo bye bye hello too $ Please note that xargs on UNIX makes an optimization where it will feed more than argument to the command at a time. We don't expect you to make this optimization. To make xargs on UNIX behave the way we want it to for this lab, please run it with the -n option set to 1. For instance $ (echo 1 ; echo 2) | xargs -n 1 echo 1 2 $ Some hints: • Use fork and exec to invoke the command on each line of input. Use wait in the parent to wait for the child to complete the command. • To read individual lines of input, read a character at a time until a newline ('\n') appears. • kernel/param.h declares MAXARG, which may be useful if you need to declare an argv array. • Add the program to UPROGS in Makefile. • Changes to the file system persist across runs of qemu; to get a clean file system run make clean and then make qemu. xargs, find, and grep combine well: $ find . b | xargs grep hello will run "grep hello" on each file named b in the directories below ".". To test your solution for xargs, run the shell script xargstest.sh. Your solution is correct if it produces the following output: $ make qemu ... init: starting sh $ sh < xargstest.sh $ $ $ $ $ $ hello hello hello $ $ You may have to go back and fix bugs in your find program. The output has many $ because the xv6 shell doesn't realize it is processing commands from a file instead of from the console, and prints a $ for each command in the file.
11-16
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值