grep particular string from multiple lines

grep multiple lines after match
grep multiple strings
grep consecutive lines
pcregrep multiline
sed multiple lines
grep xml tag multiple lines
how to grep multiple words in a single line in unix
zgrep multiple strings

i have file which contain the information of many users. for example .

Name: firstname:jessica middlename:albert lastname:peters
phone: 872634823468
Mobile: 72364623646
Address: Apacrtment : CT APT , Road : MG Road , City : Mumbai
Job : Software developer.

Name: firstname:ram middlename:kishan lastname:pichai
phone: 3242342342
Mobile: 12343545435
Address: Apacrtment : Antop APT , Road : AT Road , City : Delhi
Job : Senior developer.

I want to grep particular value from particular lines . i am able to find the lines with egrep command .

for example

 egrep "Name|Mobile|Address" test.log
    Name: jessica albert peters
    Mobile: 72364623646
    Address: Apartment : CT APT , Road : MG Road , City : Mumbai

how can i find out out particular string from above lines and save them in comma separated value . for example i am looking output like

expected output
Name,Mobile,City
jessica,72364623646,Mumbai
ram,12343545435,Delhi

If your actual Input_file is same as shown samples then following may help you here.

awk '
BEGIN{
  OFS=","
  print "Name,Mobile,City"
}
/^Name:/{
  sub(/.*:/,"",$2)
  val=$2
  next
}
/^Mobile/{
  val=val OFS $2
  next
}
/City/{
  match($0,/City : .*/)
  print val OFS substr($0,RSTART+7,RLENGTH-7)
  val=""
}'   Input_file

How can I "grep" patterns across multiple lines?, It seems I am misusing grep / egrep . I was trying to search for strings in multiple line and could not find a match while I know that what I'm looking for should match. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more grep particular string from multiple lines

You may use this awk with an empty RS:

awk -v OFS=, -F ' *[,:] *|[[:space:]]+' -v RS= 'BEGIN{print "Name", "Mobile", "City"}
{print $3, $11, $20}' file

Name,Mobile,City
jessica,72364623646,Mumbai
ram,12343545435,Delhi

How do I grep for multiple patterns on multiple lines?, How to use grep to match a string with spaces in it? 0 · How to search start and end with square bracket with multiple line · Hot Network Questions. The Linux grep command is a string and pattern matching utility that displays matching lines from multiple files. It also works with piped output from other commands. We show you how.

Considering that file follow this pattern that you showed

The following commands help you to catch:

Name: cat teste.txt | grep -e "Name" | awk -F" " '{ print $2 }' | awk -F":" '{ print $2 }'

Mobile: cat teste.txt | grep -e "Mobile" | awk -F " " '{ print $2 }'

Address: cat teste.txt | grep -e "Address" | awk -F"City : " '{ print $2 }'

After this just use a foreach loop to write in a csv file

7 Linux Grep OR, Grep AND, Grep NOT Operator Examples, pl. Grep searching two words in a line. Here are all other possibilities for grep and egrep command: grep 'word1  Hi, I want to search multiple string using grep and want to display only matching string. can any one pl guide me in this regards. eg. cat abc.txt |grep -e ‘ab’ -e ‘bc’ -e ‘cd’

How to give a pattern for new line in grep?, Finding doubled words in a single line is easy using GNU grep and similarly with GNU sed : $ cat two-cities-dup1.txt It was the best of times, it was the worst of  GREP AND: Match Multiple Patterns It is also often required to grep a file for multiple patterns – when it is needed to find all the lines in a file, that contain not one, but several patterns. Note, that you can both find the lines in a file that match multiple patterns in the exact order or in the any order.

Search Multiple Words / String Pattern Using grep Command on , How do you grep 'select * from table_name' string from a script if the select * and from table_name are on 2 different lines ? like select * from table_name Any  The grep command is primarily used to search text or search any given file for lines containing a match to the supplied words/strings. By default, grep displays the matching lines, and it may be used to search for lines of text matching one/many regular expressions in a fuss-free, and it outputs only the matching lines.

Text search across multiple lines (sed, a stream editor), To search for a particular character string in a file, use the grep command. The basic syntax of the grep command is: Note that more than one line may match the pattern you give: 4.1.2 grep with Multi-Word Strings. To find a pattern that is​  grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN.

Comments
  • Good that you have showed your efforts which you have put to solve this problem, request you NOT to post real data on forums, I am seeing mobile numbers are there in post, kindly always put dummy data.
  • you make the most difficult awk's possible, it cause of your answer's i begin to use this command so well
  • @Inder, thanks man glad to hear that I could help you, happy learning.
  • Many Many congratulations for achieving 500K, you are too good sir, hats off to you. I really want to become like you 1 day.
  • y not use a single line awk like the other answers suggest, don't you think this wil be a slower approach ???
  • Yes, and the grep is also useless.