UNIX shell scripting logic

This is my source file and required output is mentioned as below.
Any input on how to proceed.

start
name=vamsi
phone=123456789
mailid=abc@gmail.com
adress:-india
end
start
name=vamsi1
phone=1234567890
mailid=abc1@gmail.com
adress:-usa
end

required output:

vamsi,123456789,abc@gmail.com,india
vamsi1,1234567890,abc1@gmail.com,usa

Hello vamsi.valiveti,

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

awk -F'[=|:-]' '/end/{print Q;Q=A=""}/start/{A=1;next} A{Q=Q?Q","$NF:$NF}'  Input_file
 

Output will be as follows.

vamsi,123456789,abc@gmail.com,india
vamsi1,1234567890,abc1@gmail.com,usa

EDIT: Thanks to Don for mentioning the correction in -F so following could be the change in code.

awk -F'=|:-' '/end/{print Q;Q=A=""}/start/{A=1;next} A{Q=Q?Q","$NF:$NF}'  Input_file

Thanks,
R. Singh

what you are doing in below code is not clear and what is Q?

/end/{print Q;Q=A=""}/start/{A=1;next}

Hello vamsi.valiveti,

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

awk -F'[=|:-]'      ##### Setting field separator as = OR :-
'/end/              ##### Searching for text end here.
{print Q;           ##### print the value of variable Q here.
Q=A=""}             ##### Nullifying values of variables Q and A here.
/start/             ##### Searching for text start here.
{A=1;               ##### Setting value of variable A as 1 here.
next}               ##### putting next here so that all next statements will be skipped.
A                   ##### Checking condition if value of variable A is not null.
{Q=Q?Q","$NF:$NF}'  ##### if above condition is TRUE then execute this, to make the value of variable named Q.
                          Q=Q checking condition if Q's value is NOT NULL then execute actions after ?
                          which is to concatenate the value of already existing variable Q with Q","$NF(current line's last field)
                          If above mentioned condition is NOT TRUE then execute actions after :
                          which is to keep value of variable Q as $NF only no need to concatenate it.
Input_file          ##### Mentioning the Input_file here.

Thanks,
R. Singh

what is the value of q here.we are not setting any value here?

Hello vamsi.valiveti,

Not sure if you have gone through my all explanation, Q's value is getting assigned in below steps so it has been explained there, let me know if you have any queries(Off course after going through all my explanation only).

Thanks,
R. Singh

Simplest solution you can try below:

cat temp.txt  | grep -v -i "^start$" | grep -v -i "^end$" | paste -d, - - - -

Useless use of cat, useless use of grep. Same effect with fewer wasted commands:

awk '!/^(start|end)$/' temp.txt | paste -d, - - - -

@Corona688 .... this forum is not to give exact and complex answer. Person who is asking question needs to learn as well. Hence i used cat and grep so that by executing person can learn.:D:D

Please don't teach the useless use of cat. :frowning:

:b::b::b:

awk 'NF > 1 {printf rs $2; rs=","} /^end/ {print ""; rs=""}' FS="[=:]-*" infile

Not really simple. Too many processes. You can replace the cat and the two greps by a single

grep -viE '^(start|end)$'  temp.txt

Please keep in mind that the desired output is

vamsi,123456789,abc@gmail.com,india
vamsi1,1234567890,abc1@gmail.com,usa

Hi Ravinder,
Note that:

awk -F'[=|:-]'

sets four field separator characters = , | , : , and - . Each of those characters are matched by the ERE bracket matching expression containing those four characters. If you want to use the two strings = and :- as field separators, leave off the brackets:

awk -F'=|:-'

which specifies an ERE containing two alternatives with the | separating the alternatives.

1 Like

Thanks for reminding. I do, but this would be achieved by the subsequent paste. In my comment, I just rewrote three commands of the pipe (cat/grep/grep) into a single one. Applying this to the whole solution - given the other comments on this topic - is trivial.