sed

Remove specific words from sentences in bash?

笑着哭i 提交于 2021-02-20 19:14:12
问题 I want to remove negative words form sentence using bash script . The negative words that i meant it : [dull,boring,annoying,bad] My file text text.txt contains this sentence : These dull boring cards are part of a chaotic board game ,and bad for people I'm using this script array=( dull boring annoying bad ) for i in "${array[@]}" do cat $p | sed -e 's/\<$i\>//g' done < my_text.txt But I got the following wrong result: These boring cards are part of a chaotic board game ,and bad for people

Calculate the string length in sed

心不动则不痛 提交于 2021-02-20 02:40:32
问题 I was forced to calculate the string length in sed . The string is always a nonempty sequence of a 's. sed -n ':c /a/! be; s/^a/1/; s/0a/1/; s/1a/2/; s/2a/3/; s/3a/4/; s/4a/5/; s/5a/6/; s/6a/7/; s/7a/8/; s/8a/9/; s/9a/a0/; /a/ bc; :e p' It's quite long :) So now I wonder if it is possible to rewrite this script more concisely using the y or other sed command? I know that it is better to use awk or another tool. However, this is not a question here. Note that the sed script basically simulates

Calculate the string length in sed

Deadly 提交于 2021-02-20 02:36:29
问题 I was forced to calculate the string length in sed . The string is always a nonempty sequence of a 's. sed -n ':c /a/! be; s/^a/1/; s/0a/1/; s/1a/2/; s/2a/3/; s/3a/4/; s/4a/5/; s/5a/6/; s/6a/7/; s/7a/8/; s/8a/9/; s/9a/a0/; /a/ bc; :e p' It's quite long :) So now I wonder if it is possible to rewrite this script more concisely using the y or other sed command? I know that it is better to use awk or another tool. However, this is not a question here. Note that the sed script basically simulates

How to get lines from the last match to the end of file?

邮差的信 提交于 2021-02-19 06:39:10
问题 Need to print lines after the last match to the end of file. The number of matches could be anything and not definite. I have some text as shown below. MARKER aaa bbb ccc MARKER ddd eee fff MARKER ggg hhh iii MARKER jjj kkk lll Output desired is jjj kkk lll Do I use awk with RS and FS to get the desired output? 回答1: You can actually do it with awk (gawk) without using any pipe. $ awk -v RS='(^|\n)MARKER\n' 'END{printf "%s", $0}' file jjj kkk lll Explanations: You define your record separator

quickest way to select/copy lines containing string from huge txt.gz file

喜你入骨 提交于 2021-02-19 04:21:49
问题 So I have the following sed one liner: sed -e '/^S|/d' -e '/^T|/d' -e '/^#D=/d' -e '/^##/d' -e 's/H|/,H|/g' -e 's/Q|/,,Q|/g' -e '1 i\,,,' sample_1.txt > sample_2.txt I have many lines that start with either: S| T| #D= ## H| Q| The idea is to not copy the lines starting with one of the first fours and to replace H| (at the beginning of lines) by ,H| and Q| (at the beginning of lines) by ,,Q| But now I would need to: use the fastest way possible (internet suggests (m)awk is faster than sed)

quickest way to select/copy lines containing string from huge txt.gz file

随声附和 提交于 2021-02-19 04:21:25
问题 So I have the following sed one liner: sed -e '/^S|/d' -e '/^T|/d' -e '/^#D=/d' -e '/^##/d' -e 's/H|/,H|/g' -e 's/Q|/,,Q|/g' -e '1 i\,,,' sample_1.txt > sample_2.txt I have many lines that start with either: S| T| #D= ## H| Q| The idea is to not copy the lines starting with one of the first fours and to replace H| (at the beginning of lines) by ,H| and Q| (at the beginning of lines) by ,,Q| But now I would need to: use the fastest way possible (internet suggests (m)awk is faster than sed)

How to print the next word after a found pattern with grep,sed and awk?

北城余情 提交于 2021-02-18 22:26:28
问题 for example, suppose I have logfile.txt which contains "Here is a sample text file" My pattern is "sample" How can I get the word next to sample in my logfile.txt. 回答1: Here is one way to do it with awk: $ awk '{for(i=1;i<=NF;i++)if($i=="sample")print $(i+1)}' file text Explained: $ awk '{ for(i=1;i<=NF;i++) # process every word if($i=="sample") # if word is sample print $(i+1) # print the next }' file and sed: $ sed -n 's/.* sample \([^ ]*\).*/\1/p' file text ie. after sample next space

Parsing HTML on the command line; How to capture text in <strong></strong>?

假如想象 提交于 2021-02-18 06:47:06
问题 I'm trying to grab data from HTML output that looks like this: <strong>Target1NoSpaces</strong><span class="creator"> .... <strong>Target2 With Spaces</strong><span class="creator"> .... I'm using a pipe train to whittle down the data to the targets I'm trying to hit. Here's my approach so far: grep "/strong" output.html | awk '{print $1}' Grep on "/strong" to get the lines with the targets; that works fine. Pipe to 'awk '{print $1}'. That works in case #1 when the target has no spaces, but

sed replace with variable [duplicate]

£可爱£侵袭症+ 提交于 2021-02-17 07:10:15
问题 This question already has answers here : sed substitution with Bash variables (4 answers) Closed 5 years ago . I want to replace a line that says alpha = -pi/... with the correct calculated value in radians of the angle given ie variable n1 #!/bin/bash read -p "Angle in degrees : " n1 ## Convert angle to radians printf -v "n2" "%.0f" $(echo | bc | awk "BEGIN {print 180/$n1}") echo "$n1 Degrees = pi/$n2" printf -v "n3" "alpha = -pi/$n2;" echo "${n3}" cd /home/src/octave_scripts/makemesh_rot

bash replace variable name in string with variable value

纵然是瞬间 提交于 2021-02-17 06:37:05
问题 This is kind of a weird one. I have the following string: I have a variable called REDIRECT set to: https://working.${MYDOMAIN}/blah/blah . I need to replace the ${MYDOMAIN} with the actual value of the variable assigned to ${MYDOMAIN}. Not sure if bash or sed is best for this. I tried bash replace but couldn't get it to work, probably related to escaping the characters or something. Any help appreciated! 回答1: You may use this bash substitution: echo "${REDIRECT/\${MYDOMAIN\}/$MYDOMAIN}" or