sed

Replacing string in linux using sed/awk based

血红的双手。 提交于 2021-02-04 18:16:13
问题 i want to replace this #!/usr/bin/env bash with this #!/bin/bash i have tried two approaches Approach 1 original_str="#!/usr/bin/env bash" replace_str="#!/bin/bash" sed s~${original_str}~${replace_str}~ filename Approach 2 line=`grep -n "/usr/bin" filename` awk NR==${line} {sub("#!/usr/bin/env bash"," #!/bin/bash")} But both of them are not working. 回答1: You cannot use ! inside a double quotes in BASH otherwise history expansion will take place. You can just do: original_str='/usr/bin/env

Replacing string in linux using sed/awk based

余生颓废 提交于 2021-02-04 18:16:09
问题 i want to replace this #!/usr/bin/env bash with this #!/bin/bash i have tried two approaches Approach 1 original_str="#!/usr/bin/env bash" replace_str="#!/bin/bash" sed s~${original_str}~${replace_str}~ filename Approach 2 line=`grep -n "/usr/bin" filename` awk NR==${line} {sub("#!/usr/bin/env bash"," #!/bin/bash")} But both of them are not working. 回答1: You cannot use ! inside a double quotes in BASH otherwise history expansion will take place. You can just do: original_str='/usr/bin/env

What is the difference between `sed -i -e` and `sed -ie`?

偶尔善良 提交于 2021-02-04 17:33:26
问题 What is the difference between sed -i -e and sed -ie ? It's not very clear from help sed --help -e script, --expression=script add the script to the commands to be executed In second case it creates some backup file? In general Unix utils do not permit to combine flags? Just an example to show what is happening: echo "bla" > 1.txt cat 1.txt bla sed -i -e 's:bla:blakva:g' 1.txt cat 1.txt blakva sed -ie 's:bla:blakva:g' 1.txt cat 1.txt blakvakva *Note: also 1.txte is created, containing cat 1

Replace an attribute or key in JSON using jq or sed

我怕爱的太早我们不能终老 提交于 2021-02-04 16:01:07
问题 Have a big json like this "envConfig": { "environmentName": { "versions": [ { "name": "version1", "value": "Dev" }, { "name": "version2", "host": "qa" } ], "userRoles": [ { "name": "Roles", "entry": [ { "name": "employees", "value": "rwx" }, { "name": "customers", "value": "rx" } ] } ] } }, I wanted to change the JSON attribute from "environmentName" to "prod". Below is the output i am expecting "envConfig": { "prod": { "versions": [ ... ], "userRoles": [ ... ] } } Tried with sed command as

unix sort for 2 fields numeric order

心不动则不痛 提交于 2021-02-04 13:56:26
问题 I need to sort some data with unix sort but I can't figure exactly the right syntax, the data looks like 3.9.1 Step 10: 3.9.1 Step 20: 3.8.10 Step 20: 3.10.2 Step 10: 3.8.4 Step 90: 3.8.4 Step 100: 3.8.4 Step 10: I want to sort it using first the major number, then the step number, e.g. the data sorted above would look like. 3.8.4 Step 10: 3.8.4 Step 90: 3.8.4 Step 100: 3.8.10 Step 20: 3.9.1 Step 10: 3.9.1 Step 20: 3.10.2 Step 10: I have found the way to sort by first number on this site:

How to delete all the lines after the last occurence of pattern?

给你一囗甜甜゛ 提交于 2021-02-04 07:25:08
问题 i want to delete all the lines after the last occurence of pattern except the pattern itself file.txt honor apple redmi nokia apple samsung lg htc file.txt what i want honor apple redmi nokia apple what i have tried sed -i '/apple/q' file.txt this deletes all the line after the first occurence of pattern - honor 回答1: Simple, robust 2-pass approach using almost no memory: $ awk 'NR==FNR{if (/apple/) hit=NR; next} {print} FNR==hit{exit}' file file honor apple redmi nokia apple If that doesn't

How to delete lines before a match perserving it?

怎甘沉沦 提交于 2021-01-29 20:50:38
问题 I have the following script to remove all lines before a line which matches with a word: str=' 1 2 3 banana 4 5 6 banana 8 9 10 ' echo "$str" | awk -v pattern=banana ' print_it {print} $0 ~ pattern {print_it = 1} ' It returns: 4 5 6 banana 8 9 10 But I want to include the first match too. This is the desired output: banana 4 5 6 banana 8 9 10 How could I do this? Do you have any better idea with another command? I've also tried sed '0,/^banana$/d' , but seems it only works with files, and I

Why doesn't this simple RegEx work with sed?

旧时模样 提交于 2021-01-29 17:41:50
问题 This is a really simple RegEx that isn't working, and I can't figure out why. According to this, it should work. I'm on a Mac (OS X 10.8.2). script.sh #!/bin/bash ZIP="software-1.3-licensetypeone.zip" VERSION=$(sed 's/software-//g;s/-(licensetypeone|licensetypetwo).zip//g' <<< $ZIP) echo $VERSION terminal $ sh script.sh 1.3-licensetypeone.zip 回答1: Looking at the regex documentation for OS X 10.7.4 (but should apply to OP's 10.8.2), it is mentioned in the last paragraph that Obsolete (basic)

Modify an xml server on several servers

♀尐吖头ヾ 提交于 2021-01-29 15:41:02
问题 I asked Previously how to replace statement in xml file <app-connector port="${APP_CONNECTOR_PORT}" address="192.168.0.254" By <app-connector port="${APP_CONNECTOR_PORT}" address="0.0.0.0" Someone Helped me by giving a sed command which works in single host. sed -i '/<app-connector port="${APP_CONNECTOR_PORT}" address="192.168.0.254"$/s/192.168.0.254/0.0.0.0/' file So Now the question is to apply this to serveral hosts in text file: 192.168.0.1 192.168.0.2 192.168.0.3 192.168.0.4 192.168.0.5

How to use Awk to create a new field but retain the original field?

喜你入骨 提交于 2021-01-29 11:04:21
问题 Can this be done in Awk? FILE_IN (Input file) ID_Number|Title|Name 65765765|The Cat Sat on the Mat|Dennis Smith 65765799|The Dog Sat on the Catshelf|David Jones 65765797|The Horse Sat on the Sofa|Jeff Jones FILE_OUT (Desired Results) ID_Number|Title|Nickname|Name 65765765|The Cat Sat on the Mat|Cat Sat|Dennis Smith 65765799|The Dog Sat on the Catshelf|Dog|David Jones 65765797|The Horse Sat on the Sofa||Jeff Jones Logic to apply: IF Title contains “ Cat Sat ” OR " cat sat " THEN Nickname =