usding sed to replace multi-words [duplicate] - linux

This question already has an answer here:
replace multiple strings in one line with sed
(1 answer)
Closed 4 years ago.
Is there an example of if you want to replace multi-words with one word
for example input: dog apple orange banana pear
output dog cat cat banana cat
I have solved with this below is there is a better way(better than sed)? by not typing cat three times:
sed -e 's/apple/cat/g;s/orange/cat/g;s/pear/cat/g'
thanks in advance

Simply replace the strings at once using OR operator
sed -e 's/apple\|orange\|pear/cat/g' filename

Related

How to grep a word from a specific line till the end of file? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How to find a word for eg
Find "abc" starting from line 6 till the end of file ?
You can grep the output of tail command like this.
tail -n+6 file.txt | grep abc
In sed, it's easy to say "ignore lines in this address range" and then grep in the rest.
sed -n -e '1,5d' -e '/abc/p' file
The -n option says to not print all lines by default, we then simply delete lines 1 through 5, and then print any matching lines in the remainder.
There's also a block syntax, so you can say
sed -n '6,${/abc/p;}' file
but the precise syntax differs slightly between dialects (I think Linux would not demand a semicolon before the closing brace?) The address range 6,$ selects lines from the sixth through the end of the file.
One solution is to use awk, checking both the text and the line number (this script contains only the rule to select lines, it uses the default action which is to print the line):
awk 'NR >= 6 && /abc/' inputfile.txt
The following transcript shows this in action:
pax:~> cat inputfile.txt
1 abc
2 def
3 abc
4 xxx
5 yyy
6 abc
7 xyz
8 abc
9 abc
pax:~> awk 'NR >= 6 && /abc/' inputfile.txt
6 abc
8 abc
9 abc
Delete (d) lines from 1 to 5 (1,5); and also delete (the other d) lines not (!) containing abc (/abc/):
sed '1,5d;/abc/!d' input.txt

bash command to remove lines which are present in other text file [duplicate]

This question already has answers here:
Deleting lines from one file which are in another file
(11 answers)
Closed 3 years ago.
I am on bash I have two files added.txt and unmatched.txt , now imagine that all lines from added.txt are present in unmatched.txt . I want to remove lines from unmatched.txt which are present in added.txt . for example
1) added.txt
apple
ball
2) unmatched.txt
cat
dog
apple
rar
ball
3) required output.txt
cat
dog
rar
Trivial to do with grep:
$ cat added.txt
cat
dog
$ cat unmatched.txt
aardvark
cat
dog
giraffe
civet cat
$ grep -F -vx -f added.txt unmatched.txt
aardvark
giraffe
civet cat
Prints just lines of unmatched.txt that don't exactly match lines of added.txt (-v inverts the usual meaning of grep).

Print new line as string literal in unix or shell [duplicate]

This question already has answers here:
How to replace one character with two characters using tr
(5 answers)
Closed 3 years ago.
Hi I have a shell script that has
variable="apple banana monkey"
I want it to be
apple\nbanana\nmonkey
But when I try and execute
echo $variable | tr ' ' '\n'
It results to
apple
banana
monkey
I want to get the actual literal of new line and not the evaluated value.
I have tried echo -e or echo -n or even put numerous escapes \\ but to no avail.
Please help. Thanks
tr command translates chars into chars by performing a 1 to 1 mapping. You are asking the tool to translate a space into two chars, which is something that cannot be done with tr.
If you accept a command switch, you can try with sed:
echo "$variable" | sed 's/ /\\n/g'

Sed - How to switch two words in a line [duplicate]

This question already has answers here:
exchange two words using sed
(5 answers)
Closed 5 years ago.
I'm trying to write a shell script that switches the first and third words in a line. In this case only strings that contain letters (both upper- and lowercase) count as words, everything else (numbers, punctuation, whitespace) is considered whitespace.
For example:
abc123def. ghi...jkl
would turn into:
ghi123def. abc...jkl
I tried the following, but it doesn't work:
sed 's/\([a-zA-Z][a-zA-Z]*\)[^A-Z^a-z]\([a-zA-Z][a-zA-Z]*\)[^A-Z^a-z]\([a-zA-Z][a-zA-Z]*\)/\3 \2 \1/' input.txt
With sed:
$ echo "abc123def. ghi...jkl" | sed -r 's/([A-Za-z]*)([^A-Za-z]*[A-Za-z]*[^A-Za-z]*)([A-Za-z]*)(.*)/\3\2\1\4/g'
$ ghi123def. abc...jkl

insert underscore in columns of a text file shell [duplicate]

This question already has answers here:
Replace whitespace with a comma in a text file in Linux
(9 answers)
Closed 6 years ago.
I have a tab separated text file f.txt like :
APPLE 10 5
BALL 20 6
CAT 30 7
I want the output to be
APPLE_10_5
BALL_20_6
CAT_30_7
I wrote the following to partially accomplish this, but I am stuck at the "paste" step. Can you help?
cat f.txt | cut -f 1,2,3 | paste ???
When the are sperated by one space using sed is a one liner.
sed -i "s/ /_/" input.txt

Resources