sed

Print all lines between two patterns, exclusive, first instance only (in sed, AWK or Perl) [duplicate]

白昼怎懂夜的黑 提交于 2021-02-07 09:33:34
问题 This question already has answers here : How to print lines between two patterns, inclusive or exclusive (in sed, AWK or Perl)? (9 answers) Closed 1 year ago . Using sed, AWK (or Perl), how do you print all lines between (the first instance of) two patterns, exclusive of the patterns? 1 That is, given as input: aaa PATTERN1 bbb ccc ddd PATTERN2 eee Or possibly even: aaa PATTERN1 bbb ccc ddd PATTERN2 eee fff PATTERN1 ggg hhh iii PATTERN2 jjj I would expect, in both cases: bbb ccc ddd 1 A

Remove all lines between two strings

馋奶兔 提交于 2021-02-07 09:02:32
问题 In a sh shell script. Given data in a text file: string1 string2 gibberish gibberish string3 gibberish string4 How could you use awk or sed to remove all lines between string2 (inclusive) and string3 (not including string3 )? to end up with: string1 string3 string4 回答1: you can try this. Anything before "string2" will not be deleted. awk 'BEGIN{f=0} { match($0,"string2") if(RSTART){ print substr($0,1,RSTART-1) f=1 next } match($0,"string3") if(RSTART){ $0=substr($0,RSTART) f=0 } } f==0{print}

Remove all lines between two strings

我只是一个虾纸丫 提交于 2021-02-07 09:01:06
问题 In a sh shell script. Given data in a text file: string1 string2 gibberish gibberish string3 gibberish string4 How could you use awk or sed to remove all lines between string2 (inclusive) and string3 (not including string3 )? to end up with: string1 string3 string4 回答1: you can try this. Anything before "string2" will not be deleted. awk 'BEGIN{f=0} { match($0,"string2") if(RSTART){ print substr($0,1,RSTART-1) f=1 next } match($0,"string3") if(RSTART){ $0=substr($0,RSTART) f=0 } } f==0{print}

Remove all lines between two strings

ぃ、小莉子 提交于 2021-02-07 09:00:35
问题 In a sh shell script. Given data in a text file: string1 string2 gibberish gibberish string3 gibberish string4 How could you use awk or sed to remove all lines between string2 (inclusive) and string3 (not including string3 )? to end up with: string1 string3 string4 回答1: you can try this. Anything before "string2" will not be deleted. awk 'BEGIN{f=0} { match($0,"string2") if(RSTART){ print substr($0,1,RSTART-1) f=1 next } match($0,"string3") if(RSTART){ $0=substr($0,RSTART) f=0 } } f==0{print}

sort a file based on a column in another file

大憨熊 提交于 2021-02-07 08:46:12
问题 I have two files both in the format of: loc1 num1 num2 loc2 num3 num4 The first column is the location and I want to use the order of the locations in the first file to sort the second file so that I can put the two files together where the numbers are right for the location. I can write a perl script to do this but I felt there might be some quick/easy shell/awk command to achieve this. Do you have any ideas? Thanks. Edits: Here is the input, now I actually want to use column 2 in file 1 to

sort a file based on a column in another file

試著忘記壹切 提交于 2021-02-07 08:45:25
问题 I have two files both in the format of: loc1 num1 num2 loc2 num3 num4 The first column is the location and I want to use the order of the locations in the first file to sort the second file so that I can put the two files together where the numbers are right for the location. I can write a perl script to do this but I felt there might be some quick/easy shell/awk command to achieve this. Do you have any ideas? Thanks. Edits: Here is the input, now I actually want to use column 2 in file 1 to

First character of a variable in a shell script to uppercase?

这一生的挚爱 提交于 2021-02-06 15:29:33
问题 I have a shell script that starts unit tests for modules. I need the name of the module in all lowercase and with the first character uppercase. So far I have been doing it like this: #!/bin/sh -x # z.B. getbrowser strModuleToTest=$1 # g strModuleToTestUppercaseFirstletter=${strModuleToTest:0:1} # etbrowser strModuleToTestUppercaseLastletters=${strModuleToTest:1} # g -> G strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/a/A}" strModuleToTestUppercaseFirstletter="$

First character of a variable in a shell script to uppercase?

蓝咒 提交于 2021-02-06 15:26:53
问题 I have a shell script that starts unit tests for modules. I need the name of the module in all lowercase and with the first character uppercase. So far I have been doing it like this: #!/bin/sh -x # z.B. getbrowser strModuleToTest=$1 # g strModuleToTestUppercaseFirstletter=${strModuleToTest:0:1} # etbrowser strModuleToTestUppercaseLastletters=${strModuleToTest:1} # g -> G strModuleToTestUppercaseFirstletter="${strModuleToTestUppercaseFirstletter/a/A}" strModuleToTestUppercaseFirstletter="$

How to shave off last character using sed?

六眼飞鱼酱① 提交于 2021-02-06 14:49:23
问题 That is, going from ABCD -> ABC 回答1: You can try: sed s'/.$//' The regex used is .$ . is a regex meta char to match anything (except newline) $ is the end of line anchor. By using the $ we force the . to match the last char This will remove the last char, be it anything: $ echo ABCD | sed s'/.$//' ABC $ echo ABCD1 | sed s'/.$//' ABCD But if you want to remove the last char, only if its an alphabet, you can do: $ echo ABCD | sed s'/[a-zA-Z]$//' ABC $ echo ABCD1 | sed s'/[a-zA-Z]$//' ABCD1 回答2:

How to shave off last character using sed?

ぃ、小莉子 提交于 2021-02-06 14:49:11
问题 That is, going from ABCD -> ABC 回答1: You can try: sed s'/.$//' The regex used is .$ . is a regex meta char to match anything (except newline) $ is the end of line anchor. By using the $ we force the . to match the last char This will remove the last char, be it anything: $ echo ABCD | sed s'/.$//' ABC $ echo ABCD1 | sed s'/.$//' ABCD But if you want to remove the last char, only if its an alphabet, you can do: $ echo ABCD | sed s'/[a-zA-Z]$//' ABC $ echo ABCD1 | sed s'/[a-zA-Z]$//' ABCD1 回答2: