sed

How can I use xargs to run a function in a command substitution for each match?

妖精的绣舞 提交于 2021-01-29 08:36:16
问题 While writing Bash functions for string replacements I have encountered a strange behaviour when using xargs. This is actually driving me mad currently as I cannot get it to work. Fortunately I have been able to nail it down to the following simple example: Define a simple function which doubles every character of the given parameter: function subs { echo $1 | sed -E "s/(.)/\1\1/g"; } Call the function: echo $(subs "ABC") As expected the output is: AABBCC Now call the function using xargs:

Regex to extract version from string

痞子三分冷 提交于 2021-01-29 08:20:48
问题 I have a string which has below value String1=winos-app-1.2.0-4.20120308.InLinuxOS.x86_64 I need to extract only version from this string which is "1.2.0-4" I have tried regular expression as mentioned below with sed sed -ne 's/[^0-9]*\(\([0-9]\.\)\{0,1\}[0-9][^.]\).*/\1/p' but I am only getting result "1.2.0-", missing number after "-" which is 4. I tried correcting it but it is returning null. Kindly, advise 回答1: how about grep: grep -Po "(?<=-)[\d.-]*(?=.\d{8})" 回答2: This should work, and

Stress testing URI using xargs + curls bash script failing with status empty

半腔热情 提交于 2021-01-29 05:09:47
问题 I'm trying to do user acceptance testing on an application which becomes unresponsive on a particular URL parameter included in the GET request. Steps I have curl and run the GET req (crafted) copied curl syntax for Unix and copied to ubuntu server along with some changes. 'https://abc.ai/getMultiDashboard/demouser' -H 'Cookie: _ga=GA1.2.561275388.1601468723; _hjid=ecd3d778-b7f5-4f7f-b3ef-6f9f12b13d66; 54651cc_an=4; _gid=GA1.2.1366208807.1601560229; _hjTLDTest=1; 54651cc_data

How to insert an arbitrary string after pattern with sed

只愿长相守 提交于 2021-01-29 04:18:12
问题 It must be really easy, but somehow I don't get it… I want to process an HTML-file via a bash script and insert an HTML-String into a certain node: org.html: <div id="wrapper"></div> MYTEXT=$(phantomjs capture.js www.somesite.com) # MYTEXT will look something like this: # <div id="test" style="top: -1.9%;">Something</div> sed -i "s/\<div id=\"wrapper\"\>/\<div id=\"wrapper\"\>$MYTEXT/" org.html I always get this error: bad flag in substitute command: 'd' which is probably because sed

bash using awk or sed to search backwards from occurance to a specific string

自古美人都是妖i 提交于 2021-01-29 03:56:40
问题 I've an xml file and am searching for a string in this file. Once (and if) the string is found I need to be able to search back to the position of another string and output the data. ie: <xml> <packet> <proto> <field show="bob"> </proto> </packet> <packet> <proto> <field show="rumpelstiltskin"> </proto> </packet> <packet> <proto> <field show="peter"> </proto> </packet> My input would be known: show="rumpelstiltskin" and <packet> I need to get the following result (which is basically the

bash using awk or sed to search backwards from occurance to a specific string

荒凉一梦 提交于 2021-01-29 03:48:50
问题 I've an xml file and am searching for a string in this file. Once (and if) the string is found I need to be able to search back to the position of another string and output the data. ie: <xml> <packet> <proto> <field show="bob"> </proto> </packet> <packet> <proto> <field show="rumpelstiltskin"> </proto> </packet> <packet> <proto> <field show="peter"> </proto> </packet> My input would be known: show="rumpelstiltskin" and <packet> I need to get the following result (which is basically the

How to use variables in linux regex pattern?

ぐ巨炮叔叔 提交于 2021-01-28 20:57:56
问题 I need to execute a sed command with pattern /^03.06.2014/ in a .sh script & command line. The date is a variable not a constant. How could i implement this? When I use a variable inside a regex pattern, the command breaks. Do I need to escape something here? Any help is appreciated. Thanks! date=$(date +%m.%d.%Y) sed -n '/^$date/,$p' filename 回答1: Use double quotes for variable expansion in sed sed -n "/^$date/,\$p" filename 回答2: You need to use double quotes to allow for shell expansion.

sed replace positional match of unknown string divided by user-defined separator

瘦欲@ 提交于 2021-01-28 20:41:27
问题 Want to rename the (known) 3th folder within a (unknown) file path from a string, when positioned on 3th level while separator is / Need a one-liner explicitly for sed. Because i later want use it for tar --transform=EXPRESSION string="/db/foo/db/bar/db/folder" echo -e "$string" | sed 's,db,databases,' sed replace "db" only on 3th level expected result /db/foo/databases/bar/db/folder 回答1: You could use a capturing group to capture /db/foo/ and then match db . Then use use the first caputring

Extract values from specific row, column using bash

拜拜、爱过 提交于 2021-01-28 15:14:58
问题 I have a sample log file with fields and values in this format value1 value2 50 100 value3 value4 10 150 value5 200 I need to extract fields and values to something of this format value1=50 value2=100 value3=10 value4=150 value5=200 回答1: Try this awk: gawk '{split($0,n_arr," "); getline; n=split($0,v_arr," "); getline; for (i=1;i<=n;i++){print n_arr[i] "=" v_arr[i]}}' 回答2: awk '/value/ { if ((getline values) > 0) { split(values, array) for (i = 1; i <= NF; i++) print $i "=" array[i] } }'

Extract values from specific row, column using bash

给你一囗甜甜゛ 提交于 2021-01-28 15:10:44
问题 I have a sample log file with fields and values in this format value1 value2 50 100 value3 value4 10 150 value5 200 I need to extract fields and values to something of this format value1=50 value2=100 value3=10 value4=150 value5=200 回答1: Try this awk: gawk '{split($0,n_arr," "); getline; n=split($0,v_arr," "); getline; for (i=1;i<=n;i++){print n_arr[i] "=" v_arr[i]}}' 回答2: awk '/value/ { if ((getline values) > 0) { split(values, array) for (i = 1; i <= NF; i++) print $i "=" array[i] } }'