sed or awk Replacement/Addition

Hi,

I have a big text file with similar data as below and need the text as in output
using awk or sed. any help is greatly appreciated.

Input:

City=Chicago Elden street 
 >>> reservedBy = business 1
 >>> reservedBy = business 2
 >>> reservedBy = business 3
City=Dallas Elm street 
 >>> reservedBy = business 1
 >>> reservedBy = business 2
 >>> reservedBy = business 3

Output:

City=Chicago Elden street
Chicago  >>> reservedBy = business 1
Chicago  >>> reservedBy = business 2
Chicago  >>> reservedBy = business 3
City=Dallas Elden street
Dallas  >>> reservedBy = business 1
Dallas  >>> reservedBy = business 2
Dallas  >>> reservedBy = business 3

Hello tech_frk,

Could you please try following and let me know if this helps you.

awk -F"[ =]" '/^City/{val=$2;print;next} {print val "\t" $0}'  Input_file

Thanks,
R. Singh

What if the city has more than one word like "New York" or "Los Angeles"?

Hello RudiC,

Good point :b:
Then for these kind of scenarios following may help on same then.

awk -F"[ =]" '/^City/{val="";for(i=2;i<=NF-3;i++){val=val?val OFS $i:$i};print;next} {print val "\t" $0;}'  Input_file

Thanks,
R. Singh

OK, solved. What if a street has three or more elements as "East New York Ave"?

The sample in post#1 has trailing spaces that don't help either.

Or, more general, what separates the Cities name from the street name?

Because this is one problem where the "Hold space" capability of sed can be shown here is a solution (for one-word-cities) in sed:

sed '/^City=/ {
             h
             s/^City=//
             s/ .*/ /
             x
             p
             d
          }
     G; s/\(.*\)\n\(.*\)/\2 \1/' inputfile

I hope this helps.

bakunin