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.

SEARCH="\".*\""
echo "$SEARCH"
RESULTS=$(grep -a -o $SEARCH $FILE)

This works, but the grep provides me the "Words" in multiple lines.
when i write

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

RESULTS=${RESULTS//"\n"/}

but it doesn't work.

i also tried it with:

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

Word1;Word2;This;is;Word3

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

Regards
Stefan

  perl -ne '(/"([^"]+)":/) and print ($sep,$1) and $sep=";"; END{print("\n");} ' your-source-file.txt

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?

awk 'NR%2==0' RS='"' file
awk 'NR%2==0' RS='"' file | paste -sd ';' -

The paste trick also works with your grep

RESULTS=$(grep -a -o "$SEARCH" file | paste -sd ';' -)
perl -anlF\" -e 'push @p, $F[1]; END{print join ";", @p}' textfile

Or

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

Output:

Word1;Word2;This is Word3
while IFS=\" read _ m _; do result="$result;$m"; done < textfile
echo ${result#;}

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.

Hi Made in Germany, i looked at your response and im confused about the format of the statment

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

RS='"'

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

Many thanks

@ andy391791: Did you run that scriptlet on the sample file in post#1? It will print exactly the desired fields. And, if extended by ORS=";" , it will print the desired single line. The residual flaw is the trailing semicolon...

The RS= is an initial variable assignment before opening the file.
The main loop only has the condition NR%2==0 where the default action is {print} .

1 Like

Not so easy in /bin/sh, but quite straightforward in awk (horses for courses):

#!/usr/bin/awk -f

BEGIN {
    FS=":"
}

{
    join()
    next
}

END {
    printf "\n"
}

function join() {
    gsub("\"","",$1)
    printf "%s%s", semi, $1
    semi = ";"
}

That will put it all on one line; if you want lines joined in batches of three, that is a little more work. The first step to solving a problem is specifying it precisely!

Assuming that there is only one quoted string before the colon (as in the sample input), it is also easy with any POSIX-conforming shell:

#!/bin/ksh
while IFS='"' read junk data junk
do	printf '%s%s' "$semi" "$data"
	semi=';'
done < "file.txt"
echo

This was written using a Korn shell and tested with ksh and bash with both of them producing the requested results:

Word1;Word2;This is Word3

Running it with zsh also produced the same results even though zsh obviously does not conform to the standard's quoting requirements for a shell (as mentioned in post #6 in this thread).