unix

Using grep and sed to find and replace a string

删除回忆录丶 提交于 2020-01-26 21:54:06
问题 I am using the following to search a directory recursively for specific string and replace it with another: grep -rl oldstr path | xargs sed -i 's/oldstr/newstr/g' This works okay. The only problem is that if the string doesn't exist then sed fails because it doesn't get any arguments. This is a problem for me since i'm running this automatically with ANT and the build fails since sed fails. Is there a way to make it fail-proof in case the string is not found? I'm interested in a one line

Checking from shell script if a directory contains files

青春壹個敷衍的年華 提交于 2020-01-26 08:00:23
问题 From a shell script, how do I check if a directory contains files? Something similar to this if [ -e /some/dir/* ]; then echo "huzzah"; fi; but which works if the directory contains one or several files (the above one only works with exactly 0 or 1 files). 回答1: The solutions so far use ls . Here's an all bash solution: #!/bin/bash shopt -s nullglob dotglob # To include hidden files files=(/some/dir/*) if [ ${#files[@]} -gt 0 ]; then echo "huzzah"; fi 回答2: Three best tricks shopt -s nullglob

server path / vs \

我怕爱的太早我们不能终老 提交于 2020-01-25 23:50:50
问题 In some documentation, I have gotten the instructions to write SERVER_PATH\theme\ When I check _SERVER["DOCUMENT_ROOT"] from php info, it's /storage/content/75/113475/frilansbyran.se/public_html this renders ofcourse /storage/content/75/113475/frilansbyran.se/public_html\theme\ this looks really weird to me what's the difference anyway wich should i use? (unix-server) 回答1: Using backslashes is typically the windows way of paths, eg: C:\Windows\System32 Forward slashes are usually used in Unix

/tmp清理机制

杀马特。学长 韩版系。学妹 提交于 2020-01-25 23:48:13
在linux 的/etc/cron.daily/tmpwatch 里定义了/tmp目录的清理规则,不同的发行版本不一致,但大体都有定制这个定时任务。 [root@tmpdev01:/root]#cat /etc/cron.daily/tmpwatch #! /bin/sh flags=-umc /usr/sbin/tmpwatch "$flags" -x /tmp/.X11-unix -x /tmp/.XIM-unix \ -x /tmp/.font-unix -x /tmp/.ICE-unix -x /tmp/.Test-unix \ -X '/tmp/hsperfdata_*' 10d /tmp /usr/sbin/tmpwatch "$flags" 30d /var/tmp for d in /var/{cache/man,catman}/{cat?,X11R6/cat?,local/cat?}; do if [ -d "$d" ]; then /usr/sbin/tmpwatch "$flags" -f 30d "$d" fi done [root@tmpdev01:/root]#cat /var/log/cron|grep tmpwatch Nov 11 03:29:21 tmpdev01 run-parts(/etc/cron.daily)[21854]:

Sort lines by group and column

ぃ、小莉子 提交于 2020-01-25 20:56:31
问题 I have a csv file separated by semicolons. Which contains lines as shown below.. And I need to sort it by the first and third column, respecting the groups of lines defined by the value of the first column. booke;book;2 booke;booke;1 booke;bookede;6 booke;bookedes;8 booke;booker;4 booke;bookes;7 booke;booket;3 booking;booking;1 booking;bookingen;2 booking;bookingens;3 booking;bookinger;7 booking;bookingerne;5 booking;bookingernes;6 booking;bookingers;8 booking;bookings;4 Expected output:

string replacement using awk command

匆匆过客 提交于 2020-01-25 20:38:55
问题 This is a continuation to my previous post string replacement using awk command which is not 100% fully resolved.. To make it clearer.. (The source file and code I am using are at the bottom of the post) requirement 1 If I dont have a lookup data(Eg, for "u_no" is not in the reference file, the current solution prints an empty field. aaaa uid=1a1a pwd=1b1b u_no= I want that to pick the value from the source file instead.. aaaa uid=1a1a pwd=1b1b u_no=12345 requirement 2 If the data in the

Unix help to extract/print 50 lines after every 3rd occurrence pattern till end of file

江枫思渺然 提交于 2020-01-25 14:33:55
问题 I need help with extract/print 4 lines after every 3rd occurrence pattern till end of file. Consider below is example of log file ERROR_FILE_NOT_FOUND ERROR_FILE_NOT_FOUND ERROR_FILE_NOT_FOUND Extract line 1 Extract line 2 Extract line 3 Extract line 4 ERROR_FILE_NOT_FOUND ERROR_FILE_NOT_FOUND ERROR_FILE_NOT_FOUND Extract line 5 Extract line 6 Extract line 7 Extract line 8 ERROR_FILE_NOT_FOUND ERROR_FILE_NOT_FOUND ERROR_FILE_NOT_FOUND Extract line 9 Extract line 10 Extract line 11 Extract

Unix help to extract/print 50 lines after every 3rd occurrence pattern till end of file

馋奶兔 提交于 2020-01-25 14:31:28
问题 I need help with extract/print 4 lines after every 3rd occurrence pattern till end of file. Consider below is example of log file ERROR_FILE_NOT_FOUND ERROR_FILE_NOT_FOUND ERROR_FILE_NOT_FOUND Extract line 1 Extract line 2 Extract line 3 Extract line 4 ERROR_FILE_NOT_FOUND ERROR_FILE_NOT_FOUND ERROR_FILE_NOT_FOUND Extract line 5 Extract line 6 Extract line 7 Extract line 8 ERROR_FILE_NOT_FOUND ERROR_FILE_NOT_FOUND ERROR_FILE_NOT_FOUND Extract line 9 Extract line 10 Extract line 11 Extract

How to replace catch block in *.java using sed?

做~自己de王妃 提交于 2020-01-25 13:07:22
问题 How to replace the following pattern in a java project catch(SQLException e) { \\TO DO } with catch(SQLException e) { S.O.P(); } Please note that the file will have other patterns like catch(IOException e) { // To Do } which should not be changed. I tried sed 's/catch\(SQLException[^\}]*}/catch(SQLException e)\{S.O.P();\}/g' file.java but it does not work. 回答1: you can use awk $ more file catch(SQLException e) { \\TO DO } catch(IOException e) { // To Do } $ awk -vRS="}" '/catch\(SQLException

Compare a given date is last Business Day of a month using ShellScript

人走茶凉 提交于 2020-01-25 11:38:15
问题 My File has below records ; im using Shell Script - While loop to read these files. In case if i encounter last business date of a month i need to skip them. Name date David 09/30/2013 jack 10/01/2014 Mark 10/02/2014 John 10/13/2014 Daniel 10/30/2014 Rob 10/31/2014 For example in above record set i have to Skip David and Rob considering their Date values falls on last business day of Sep & Oct. 回答1: Assuming that Saturdays and Sundays are not business days, the following script checks the