Grep for multiple string

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Grep for multiple string
# 1  
Old 02-09-2018
Grep for multiple string

im learning grep and i need some help.



i have input word file like this

Code:
fish map.this is
go to.that is

i want sed,awk or grep command to
extract the following in this format

Code:
someword SPACE someword.

so output will be

Code:
fish map.
go to.

Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 02-09-2018 at 04:17 AM.. Reason: Added CODE tags.
# 2  
Old 02-09-2018
No hard criteria given... try
Code:
sed 's/\..*$/./' file
fish map.
go to.

This User Gave Thanks to RudiC For This Post:
# 3  
Old 02-09-2018
thanks.
ok ill be a bit more specific.
input is a huge text file with lots of sentences.

Code:
fish map.this is (Kubernetes, Mesos DC/OS, and Docker Swarm) and Azure Service Fabric are indispensable for any
production-ready microservice-based application and for any multi-container applica
go to.that is needs are moving you toward complex containerized apps, you will find it useful to seek out
additional resources for learning more about orchestrators


now i want output to be
Code:
fish map.
go to.

with your command the output is now
Code:
fish map.
production-ready microservice-based application and for any multi-container applica
go to.
additional resources for learning more about orchestrators


can you do anything about this?
# 4  
Old 02-09-2018
You see the importance of a careful, detailed, and precise specification. Try
Code:
sed -n '/\..*$/s//./p' file

or even
Code:
sed -n 's/\..*$/./p' file

This User Gave Thanks to RudiC For This Post:
# 5  
Old 02-09-2018
use extension regular expression option:
Code:
sed -E 's/^(\w+)\s+(\w+\.).*$/\1 \2/g' file


Last edited by abdulbadii; 02-12-2018 at 07:50 PM..
# 6  
Old 02-12-2018
guys i tried all the commands you gave me but it fails if the input file is a bit long and complex(longer sentences).

i am trying for this input
Code:
A circumferential abdominoplasty is an extended abdominoplasty plus a d lift. The resulting scar runs all the way around the body, and the operation is also called a Belt Lipectomy or lower body lift. This operation is most appropriate for patients who have undergone massive weight loss.
      -only Pharmacokinetic dataProtein binding
Esterases, CYP3A4, SULT2A1[1]Biological half-life
12 ± 5 hours[1]Excretion

output should be
Code:
d lift.
body lift.
weight loss.

please note the output should be in format
Code:
someword SPACE someword.

can you guys help?
# 7  
Old 02-12-2018
The commands don't fail, esp. NOT for long or complex lines, they behave exactly as specified previously. This is the second time you change your mind. Will this now be the final version? Phrased like: find the second last and last word before any full stop and print them separated by a space character.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep multiple words in a file with help of fixed string switch

I have multiple strings in a file which have special character $, when i search strings by ignoring $ with \ using single quotes it returns empty results. My search strings are set char_1($lock) and set new_char_clear_3($unlock) I tried searching with but it returns empty results.However... (3 Replies)
Discussion started by: g_eashwar
3 Replies

2. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

3. Shell Programming and Scripting

Single grep to multiple strings with separate output per string

I need to grep multiple strings from a particular file. I found the use of egrep "String1|String2|String3" file.txt | wc-l Now what I'm really after is that I need to separate word count per each string found. I am trying to keep it to use the grep only 1 time. Can you guys help ? ... (9 Replies)
Discussion started by: nms
9 Replies

4. Shell Programming and Scripting

Multiple Results of Grep in one Line/String?

Hello, I have a Textfile sees like this "Word1":aksdfjaksdf "Word2":askdfjalsdkfdlsjfasldfj "This is Word3":asdfkjalskdfj what i need is a string which sees like this Word1;Word2;This is Word3 Conclusion always the text within "" which is before the : i tried it with grep.... (10 Replies)
Discussion started by: SwordMaster
10 Replies

5. Shell Programming and Scripting

Grep multiple string from input

How can I grep multiple strings from an input, strings are separated by space. Strings to grep: 7680FR 6791HH 1234AA Input: AA 7680FR AA 6891HH AA 6791UA BB 9834HA BB 1434AB DD 1234AA DD 6991HH DD 6791HH Required output: AA 7680FR (9 Replies)
Discussion started by: aydj
9 Replies

6. Shell Programming and Scripting

Grep from multiple patterns multiple file multiple output

Hi, I want to grep multiple patterns from multiple files and save to multiple outputs. As of now its outputting all to the same file when I use this command. Input : 108 files to check for 390 patterns to check for. output I need to 108 files with the searched patterns. Xargs -I {} grep... (3 Replies)
Discussion started by: Diya123
3 Replies

7. UNIX for Dummies Questions & Answers

Grep multiple strings in multiple files using single command

Hi, I will use below command for grep single string ("osuser" is search string) ex: find . -type f | xarg grep -il osuser but i have one more string "v$session" here i want to grep in which file these two strings are present. any help is appreciated, Thanks in advance. Gagan (2 Replies)
Discussion started by: gagan4599
2 Replies

8. UNIX for Dummies Questions & Answers

how to use grep: finding a string with double quotes and multiple digits

I have a file with a lot of lines (a lot!) that contain 10 digits between double quotes. ie "1726937489". The digits are random throughout, but always contain ten digits. I can not for the life of me, (via scouring the internet and grep how-to manuals) figure out how to find this when I search.... (3 Replies)
Discussion started by: titusbass
3 Replies

9. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

10. UNIX for Dummies Questions & Answers

problem with grep on search string in a txt file over multiple files

I have a couple of things I got stuck on 1) I have a text file containing 25k search string that I need to search against compressed file. I have used this command but somehow it doesn't seems to use all the search terms. I have put one search string per line in the txt file (I clean up... (2 Replies)
Discussion started by: m00
2 Replies
Login or Register to Ask a Question