Question

E.g.:

cat file1

a="Ajim 123 Bagwan"
b="Johnny 120 Boy"
c="Bad 777 Girl"
<SHOUT>
a="Aj 30 Bag"
b="John 10 Boycott"
c="Badlands 20 Malick"
<SHOUT>
.........

My output should look like this:

"Ajim 123 Bagwan" | "Johnny 120 Boy" | "Bad 777 Girl"
<SHOUT>
"Aj 30 Bag" | "John 10 Boycott" | "Badlands 20 Malick"
<SHOUT>

i.e. the set of 3 lines(or just values will do too) that are separated by a pattern "<SHOUT>" should get concatenated with some separator in them. What is the best way to do this?

The way I've been trying this is by search replacing(using %s/A/B in vi) the pattern (spanning 2 lines) "\Nb=" and "\Nc=" by a pipe(|). I've done this way before but I am just not getting it. \N being the Newline character (Control-M)

Edit:

I was able to figure out a way to do this using simple search replace. I did the following from vi.

:%s/"\nb="/|/g
:%s/"\nc="/|/g

Where \n is newline character.

These two commands replaced every occurrence of "\nb=" and "\nc=" with a pipe(|) character. Then I just replaced the beginning a=" and the ending " with a null using similar command.

:%s/a="//g
:%s/"//g

Thanks everyone for your answers.

Was it helpful?

Solution

while read line; do
   if [ "$line" = "<SHOUT>" ]; then
       echo
       echo $line
       prefix=""
   else
       echo -n "$prefix${line#*=}"
       prefix=" | "
   fi
done < file1 

OTHER TIPS

Write a shell script (test.sh):

read acc
while read line; do
    if [ "$line" == "<SHOUT>" ]
    then
        echo "$acc"
        echo "<SHOUT>"
        read acc
    else
        acc="$acc | $line"
    fi
done
echo "$acc"

Run this script in command line as: sh test.sh < file1

Using awk you can do:

awk 'NF{gsub(/.=/, ""); gsub(/\n/, " | "); printf "%s\n%s", $0, RS}' RS='<SHOUT>\n' file
"Ajim 123 Bagwan" | "Johnny 120 Boy" | "Bad 777 Girl" | 
<SHOUT>
"Aj 30 Bag" | "John 10 Boycott" | "Badlands 20 Malick" | 
<SHOUT>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top