grep

determine frequency of string using grep [duplicate]

拥有回忆 提交于 2020-01-14 09:07:40
问题 This question already has an answer here : How to get frequency of word in a sentence in R? (1 answer) Closed 6 years ago . if I have a vector x <- c("ajjss","acdjfkj","auyjyjjksjj") and do: y <- x[grep("jj",x)] table(y) I get: y ajjss auyjyjjksjj 1 1 However the second string "auyjyjjksjj" should count the substring "jj" twice. How can I change this from a true/false computation, to actually counting the frequency of "jj"? Also if for each string the frequency of the substring divided by the

How can I write only certain lines of a file in Perl?

孤者浪人 提交于 2020-01-14 08:18:26
问题 I am looking for a way to read an input file and print only select lines to an output file in Perl. The lines I want to print to the output file all begin with xxxx.xxxx.xxxx , where x is an alphanumeric character (the periods are periods, not wildcards). The lines do not all have the same ending, if that makes a difference. I'm thinking something like the following (the condition of the if statement is all that is really missing as far as I can tell). open(IN, "<$csvfile"); my @LINES = <IN>;

How can I write only certain lines of a file in Perl?

≯℡__Kan透↙ 提交于 2020-01-14 08:18:12
问题 I am looking for a way to read an input file and print only select lines to an output file in Perl. The lines I want to print to the output file all begin with xxxx.xxxx.xxxx , where x is an alphanumeric character (the periods are periods, not wildcards). The lines do not all have the same ending, if that makes a difference. I'm thinking something like the following (the condition of the if statement is all that is really missing as far as I can tell). open(IN, "<$csvfile"); my @LINES = <IN>;

Smarter Removing Unnecessary WhiteSpace CSV

浪子不回头ぞ 提交于 2020-01-14 03:23:27
问题 I have a comma separated file (CSV) that resembles the following 1, 2, 3, "Test, Hello" 4, 5, 6, "Well, Hi There!" I need to be able to transform the above from a Linux command line ideally into 1,2,3,"Test, Hello" 4,5,6,"Well, Hi There!" Now, I am aware of some of the other solutions like: Removing spaces after all commas This, however was not aware of strings which were enclosed in double quotes. For example, the solution on the page: sed -e 's/\s\+,/,/g' Produces... 1,2,3,"Test,Hello" 4,5

grep for a string in a line if the previous line doesn't contain a specific string

家住魔仙堡 提交于 2020-01-14 03:00:11
问题 I have the following lines in a file: abcdef ghi jkl uvw xyz I want to grep for the string "xyz" if the previous line is not contains the string "jkl". I know how to grep for a string if the line doesn't contains a specific string using -v option. But i don't know how to do this with different lines. 回答1: grep is really a line-oriented tool. It might be possible to achieve what you want with it, but it's easier to use Awk: awk ' /xyz/ && !skip { print } { skip = /jkl/ } ' file Read as: for

shell练习题

雨燕双飞 提交于 2020-01-14 00:19:40
计算器 calc.sh #!/bin/bash echo " $* = $(( $ * )) " 比较整数大小 #!/bin/bash read -a num -p "请输入两个整数: " [ ${#num[*]} -eq 2 ] || { echo "参数不是两个" exit 5 } [ [ " ${num[0]} " = ~ ^ [ 0-9 ] +$ ] ] || { echo " ${num[0]} 不是数字" exit 1 } [ [ " ${num[1]} " = ~ ^ [ 0-9 ] +$ ] ] || { echo " ${num[1]} 不是数字" exit 2 } if [ ${num[0]} -gt ${num[1]} ] ; then echo " ${num[0]} > ${num[1]} " elif [ ${num[0]} -eq ${num[1]} ] ; then echo " ${num[0]} = ${num[1]} " else echo " ${num[0]} < ${num[1]} " fi 判断输入是整数 function isnum ( ) { NUM = $1 if [ [ $NUM = ~ ^ [ 0-9 ] +$ ] ] ; then echo $NUM is num else echo $NUM is not num fi }

How to remove lines above and below an inverse grep match?

你离开我真会死。 提交于 2020-01-13 20:25:27
问题 I'm working with some output that is more verbose than I'd like, so I was trying to use grep to whittle it down. The output looks something like this… path/to/file1: No Problems Found path/to/file3: Problem Found I'd like to filter out all the output concerning files without problems. I'm able to remove one line of it by piping the output through grep -v "No Problems Found" . I thought I'd then be able to use -B and -A along the lines of grep -B 1 -A 1 -v "No Problems Found" but it turns out

shell练习(十)

余生长醉 提交于 2020-01-13 14:40:55
1、遍历目录 编写函数: 运行函数:./list_file.sh /usr/local/shell 1 、 统计当前文件夹下文件的个数   ls -l |grep "^-"|wc -l 2 、 统计当前文件夹下目录的个数   ls -l |grep "^d"|wc -l 3 、统计当前文件夹下文件的个数,包括子文件夹里的   ls -lR|grep "^-"|wc -l 4 、统计文件夹下目录的个数,包括子文件夹里的   ls -lR|grep "^d"|wc -l 2、统计当前目录下文件及文件个数 示例:统计当前目前下的文件个数: 3、自动重启start_tomcat.sh,及查看日志 ps -ef |grep tomcat |grep $tomcat_home |grep -v 'grep'|awk '{print $2}' | xargs kill -9 说明: “grep tomcat ” 的输出结果是,所有含有关键字“ tomcat ” 的进程。 “grep -v grep” 是在列出的进程中去除含有关键字“grep”的进程。 “cut -c 9-15” 是截取输入行的第9个字符到第15个字符,而这正好是进程号PID。 awk '{print $2}' 的作用就是打印(print)出第二列的内容 “xargs kill -9” 中的xargs命令是用来把前面命令的输出结果

linux常用命令

泪湿孤枕 提交于 2020-01-13 13:16:55
一、开关机 sync :把内存中的数据写到磁盘中(关机、重启前都需先执行sync) shutdown -rnow 或 reboot :立刻重启 shutdown -hnow :立刻关机 shutdown -h 19:00 :预定时间关闭系统(晚上7点关机,如果现在超过8点则第二天) shutdown -h +10 :预定时间关闭系统(10分钟后关机) shutdown -c :取消按预定时间关闭系统 init 0 : 关闭系统 telinit 0 : 关闭系统 logout : 注销 二、系统信息 arch : 显示机器的处理器架构 uname -m : 显示机器的处理器架构 uname -r : 显示正在使用的内核版本 dmidecode -q : 显示硬件系统部件 - (SMBIOS / DMI) hdparm -i /dev/hda : 罗列一个磁盘的架构特性 hdparm -tT /dev/sda : 在磁盘上执行测试性读取操作 cat /proc/cpuinfo : 显示CPU info的信息 cat /proc/interrupts : 显示中断 cat /proc/meminfo : 校验内存使用 cat /proc/swaps : 显示哪些swap被使用 cat /proc/version : 显示内核的版本 cat /proc/net/dev :

Using grep and ls in FTP client?

不羁岁月 提交于 2020-01-13 13:04:42
问题 How could I use grep and ls in FTP client... I mean if I want to find some specific file I could use: ls -l | grep pattern 回答1: lftp can, exactly the way you typed. 回答2: With the usual Unix commandline interactive ftp, one approach is: Remote system type is UNIX. Using binary mode to transfer files. ftp> dir . foobar output to local-file: foobar [anpqy?]? y 500 Unknown command 227 Entering Passive Mode (62,149,140,15,195,159) 150 Accepted data connection 11966 5.26 KB/s 226-Options: -a -l 226