sed

Match regex across multiple lines in bash

巧了我就是萌 提交于 2021-01-28 07:02:10
问题 I want to match all patterns that start with [% and end with %] in a file. I've tried multiple tools such as awk, sed, pcregrep and none of them seem to work, although they are suggested as top answers on similar questions. [% FOREACH selection = selections -%] case SELECTION_ID_[% SELECTION_NAME %]: { const [% selectionType %]& source = this->[% selectionName %](); rc = bcem_AggregateUtil::toAggregate(result, d_selectionId, source); } break; [% END -%] [% foo ] [% INCLUDE

Add a line in a specific position with Linux and output to the same file?

点点圈 提交于 2021-01-28 06:30:23
问题 How to add a third line in file.txt: line 1 line 2 line 4 sed could do with sed '3iline 3' file.txt but I want to output to the same file. I tried sed '3iline 3' file.txt >> file.txt which didn't work. It did add the line but it duplicates file.txt, I got this: line 1 line 2 line 4 line 1 line 2 line 3 line 4 回答1: The only way to do this is to write to a second file, then replace the original. You can only append to an arbitrary file; you cannot insert into the middle of one. t=$(mktemp) sed

How to filter out a certain portion of ./gradlew project:dependencies command?

十年热恋 提交于 2021-01-28 05:25:01
问题 Our build.gradle files have your normal depndencies block, like so.. dependencies { compile("com.myco.service:service-discovery:+") compile("com.myco.common:some-common:1.0.84") compile("com.myco.cms:cms-service-client:1.0.145") compile("com.myco.stats:some123-stats-service-client:1.0.140") ... } I run this to get the dependency tree $ ./gradlew project:dependencies | grep com\.myco | sort | uniq and get +--- com.myco.canam:canam:0.1.4 +--- com.myco.cms:cms-service-client:1.0.145 (*) +--- com

I'm trying to replace a string in a specific line number using a variable

安稳与你 提交于 2021-01-28 05:01:05
问题 here's my attempt: sed -i -e "$chs/|0|/|900|/" users The variable "chs" has the line number in which I want to replace the string, but it doesn't work, any ideas? 回答1: If chs contains the line number, say 5, then the following: sed -i -e "$chs/|0|/|900|/" users will expand to: sed -i -e "5/|0|/|900|/" users As a sed command, that is nonsense. It should return an error message about an unknown command . You need to create a substitute command preceded by a line number. Try: sed -i -e "$chs s/

read multiple files in bash

蓝咒 提交于 2021-01-28 02:50:37
问题 I have two .txt files that I want to read line per line simultaneously in .sh script. Both .txt files have the same number of lines. Inside the loop I want to use the sed-command to change the full_sample_name and sample_name in another file. I know how this works if you just read one file, but I cannot get it work for two files. #! /bin/bash FULL_SAMPLE="file1.txt" SAMPLE="file2.txt" while read ... && ... do sed -e "s/\<full_sample_name\>/$FULL_SAMPLE/g" -e "s/\<sample_name\>/$SAMPLE/g"

change the position of a line in a file using sed

南楼画角 提交于 2021-01-27 22:32:47
问题 I would like to know how to change the position of a line in a file (preferably using sed). For example, consider the file that contains goal identifier statement let statement 1 let statement 2 forall statement other statements I would like to be able to do this goal identifier statement forall statement let statement 1 let statement 2 other statements where I change the position of the forall line and bring it after the goal line. forall and goal are regexps that can be used to identify the

Unix - Convert GMT time field to PST time

六眼飞鱼酱① 提交于 2021-01-27 19:05:27
问题 Sorry if this has been asked before. I have a text file that contain a field with GMT time on Unix. I want to convert that field to the PST time zone in the same format. source file test.txt 20200804T221806.214 GMT,2003060015,2003060018 20200804T232027.571 GMT,2005260045,2005260095 20200804T232027.572 GMT,2005260045,2005260095 20200805T000119.715 GMT,2005290022C,2005290042D 20200805T000119.715 GMT,2005290022C,2005290042D 20200801T000326.111 GMT,2005290028C,2005290050D 20200101T000326.111 GMT

Regex for The Same Pattern Multiple Times in One Line

好久不见. 提交于 2021-01-27 16:25:32
问题 The pattern I'm looking for is this: TXT.*\.txt That pattern can occur multiple times in any given line. I would like to either extract each instance of the pattern out or alternatively delete the text that surrounds each instance using sed (or anything, really). Thanks! 回答1: You can use grep as: grep -o 'TXT[^.]*\.txt' file 回答2: You can use Perl as: $ cat file foo TXT1.txt bar TXT2.txt baz foo TXT3.txt bar TXT4.txt baz $ perl -ne 'print "$1\n" while(/(TXT.*?\.txt)/g)' file TXT1.txt TXT2.txt

Add line after matching a pattern [duplicate]

元气小坏坏 提交于 2021-01-27 14:09:48
问题 This question already has answers here : Using sed, Insert a line above or below the pattern? [duplicate] (4 answers) Closed 3 years ago . I have a file say test with following values Linux Solaris Fedora Ubuntu AIX HPUX How to add a line with system hostname after the line matching AIX? If I do echo `hostname` >> test system hostname comes at the last after HPUX. 回答1: With sed: sed "s/^AIX$/& $(hostname)/" file If the line could contain AIX : sed "s/AIX.*/& $(hostname)/" file Edit: To append

how to extract text which matches particular fields in text file using linux commands

蓝咒 提交于 2021-01-27 12:01:17
问题 Hi below is my text file {"Author":"john" "subject":"java" "title":"java cook book.pdf"} {"title":"Php book.pdf" "Author":"Smith" "subject":"PHP"} {"Author":"Smith" "title":"Java book.pdf"} from the above data i want to extract all titles which contains "java" word, i should get the following output java cook book.pdf Java book.pdf Please suggest me Thanks 回答1: GNU sed sed -r '/title.*java/I!d;s/.*:.(.*).}$/\1/' file java cook book.pdf Java book.pdf 回答2: You can try something like this with