Multiple Results of Grep in one Line/String?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple Results of Grep in one Line/String?
# 1  
Old 06-09-2016
Multiple Results of Grep in one Line/String?

Hello,

I have a Textfile sees like this
Code:
"Word1":aksdfjaksdf
"Word2":askdfjalsdkfdlsjfasldfj
"This is Word3":asdfkjalskdfj

what i need is a string which sees like this
Code:
Word1;Word2;This is Word3

Conclusion always the text within "" which is before the :


i tried it with grep.
Code:
SEARCH="\".*\""
echo "$SEARCH"
RESULTS=$(grep -a -o $SEARCH $FILE)


This works, but the grep provides me the "Words" in multiple lines.
when i write
Code:
echo $RESULTS

it gives me the results in multple lines.


How can i bring this multiple lines in one line?
i tried it with this, which should replace all \n with nothing
Code:
RESULTS=${RESULTS//"\n"/}

but it doesn't work.



i also tried it with:
Code:
for i in $ANSWER
do  
  ALL="$ALL;/$i"    #concatenate it to one string
done


But the for loop also split the spaces in one line
so i get
Code:
Word1;Word2;This;is;Word3


Can someone help me? I'm a complete newbe with shell scripting.

Regards
Stefan



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

Last edited by RudiC; 06-10-2016 at 03:34 AM.. Reason: Added code tags.
# 2  
Old 06-09-2016
Code:
  perl -ne '(/"([^"]+)":/) and print ($sep,$1) and $sep=";"; END{print("\n");} ' your-source-file.txt


Last edited by stomp; 06-09-2016 at 03:50 PM..
# 3  
Old 06-09-2016
Quote:
Originally Posted by SwordMaster
when i write
echo $RESULTS
it gives me the results in multple lines.
That is very odd. By doing echo $RESULTS without double quotes around $RESULTS, it should have flattened all whitespace for you. echo "$RESULTS" would show it on several lines but echo $RESULTS should not. Have you changed the value of IFS anywhere in your script?
# 4  
Old 06-09-2016
Code:
awk 'NR%2==0' RS='"' file
awk 'NR%2==0' RS='"' file | paste -sd ';' -

The paste trick also works with your grep
Code:
RESULTS=$(grep -a -o "$SEARCH" file | paste -sd ';' -)


Last edited by MadeInGermany; 06-09-2016 at 04:41 PM..
# 5  
Old 06-09-2016
Code:
perl -anlF\" -e 'push @p, $F[1]; END{print join ";", @p}' textfile

Or
Code:
perl -nle 'push @p, /^"([^"]+)/; END{$"=";"; print "@p"}' textfile

Output:
Code:
Word1;Word2;This is Word3


Code:
while IFS=\" read _ m _; do result="$result;$m"; done < textfile
echo ${result#;}


Last edited by Aia; 06-09-2016 at 09:17 PM..
# 6  
Old 06-10-2016
Quote:
Originally Posted by Corona688
That is very odd. By doing echo $RESULTS without double quotes around $RESULTS, it should have flattened all whitespace for you.
Not necessarily. This is true for bash, but the OP did not mention which shell s/he is using. I just tried it with zsh, and this one does not flatten, but preserves the newlines.
# 7  
Old 06-10-2016
Hi Made in Germany, i looked at your response and im confused about the format of the statment

Code:
awk 'NR%2==0' RS='"' file

I understand its only printing even lines and the record separator is "'" but i dont understand the format.

If
Code:
RS='"'

is an action why isnt it surrounded by braces and why is it outside the quotes ?

Many thanks
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 for multiple string

im learning grep and i need some help. i have input word file like this fish map.this is go to.that is i want sed,awk or grep command to extract the following in this format someword SPACE someword.so output will be fish map. go to.Please use CODE tags as required by forum... (11 Replies)
Discussion started by: ahfze
11 Replies

2. Shell Programming and Scripting

Multiple results as input to grep or awk

Hi: This is my first post in this forum. apologies if I am asking question that has been asked here multiple times. I wrote a python script that prints list of identifiers. I have another file notoriously big and with no particular format. I plan to search the identifiers in this file... (2 Replies)
Discussion started by: oriolebaltimore
2 Replies

3. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

4. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

5. Shell Programming and Scripting

Grep a string and write a value to next line of found string

Hi, I have two variables x and y. i need to find a particular string in a file, a workflow name and then insert the values of x and y into the next lines of the workflow name. basically it is like as below wf_xxxxxx $$a= $$b= $$c= figo $$d=bentley i need to grep the 'wf_xxxx' and then... (6 Replies)
Discussion started by: angel12345
6 Replies

6. Shell Programming and Scripting

grep on string and printing line after until another string has been found

Hello Everyone, I just started scripting this week. I have no background in programming or scripting. I'm working on a script to grep for a variable in a log file Heres what the log file looks like. The x's are all random clutter xxxxxxxxxxxxxxxxxxxxx START: xxxxxxxxxxxx... (7 Replies)
Discussion started by: rxc23816
7 Replies

7. UNIX for Dummies Questions & Answers

Displaying the Second Line of the Grep Search Results

Hi I really hope someone can help with the below question. Lets say that I have a file called output.txt and I want to display all of the lines which contain the word ‘disconnect'. I know that this can easily be obtained by using the following command: grep -i disconnect output.txt However,... (6 Replies)
Discussion started by: Sunny Sid
6 Replies

8. Shell Programming and Scripting

Grep a string and print a string from the line below it

I know how to grep, copy and paste a string from a line. Now, what i want to do is to find a string and print a string from the line below it. To demonstrate: Name 1: ABC Age: 3 Sex: Male Name 2: DEF Age: 4 Sex: Male Output: 3 Male I know how to get "3". My biggest problem is to... (4 Replies)
Discussion started by: kingpeejay
4 Replies

9. Shell Programming and Scripting

Multiple Grep Results - Formatting

Hello, Perhaps someone here can help with this. I'd like to grep a plain text file for a word and output each line containing a word found to a seperate line instead of back to back. Examples: Basic command: cat file.txt > grep -i CAT > results.txt file.txt: The cat said meow The... (7 Replies)
Discussion started by: sysera
7 Replies

10. Shell Programming and Scripting

diffrent results between command line and scripted grep

When I type a command at the command line it supplies one result and the exact same command in a script egrep '^01|^02|^03|^04' file > fileout count = 29353 same count in the script yields a count of 23492 is there any reason this could be happening. (1 Reply)
Discussion started by: r1500
1 Replies
Login or Register to Ask a Question