UNIX shell scripting logic


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting UNIX shell scripting logic
# 1  
Old 01-31-2017
UNIX shell scripting logic

This is my source file and required output is mentioned as below.
Any input on how to proceed.
Code:
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:
Code:
vamsi,123456789,abc@gmail.com,india
vamsi1,1234567890,abc1@gmail.com,usa



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

Last edited by RudiC; 01-31-2017 at 06:27 AM.. Reason: Added CODE tags.
# 2  
Old 01-31-2017
Hello vamsi.valiveti,

Could you please try following and let me know if this helps you.
Code:
awk -F'[=|:-]' '/end/{print Q;Q=A=""}/start/{A=1;next} A{Q=Q?Q","$NF:$NF}'  Input_file

Output will be as follows.
Code:
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.
Code:
awk -F'=|:-' '/end/{print Q;Q=A=""}/start/{A=1;next} A{Q=Q?Q","$NF:$NF}'  Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 02-01-2017 at 05:57 AM.. Reason: Added a little correction in answer, THANKS to Don for pointing out the same.
# 3  
Old 01-31-2017
what you are doing in below code is not clear and what is Q?

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



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

Last edited by RudiC; 01-31-2017 at 06:29 AM.. Reason: Added CODE tags.
# 4  
Old 01-31-2017
Hello vamsi.valiveti,

Could you please go through following and let me know if this helps you.
Code:
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

Last edited by RavinderSingh13; 01-31-2017 at 05:50 AM..
# 5  
Old 01-31-2017
what is the value of q here.we are not setting any value here?

Quote:
Code:
{print Q;           ##### print the value of variable Q here.


Last edited by rbatte1; 01-31-2017 at 01:00 PM.. Reason: Added quote & code tags tags for clarity
# 6  
Old 01-31-2017
Quote:
Originally Posted by vamsi.valiveti
what is the value of q here.we are not setting any value here?
Quote:
Code:
{print Q; ##### print the value of variable Q 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

Last edited by rbatte1; 01-31-2017 at 01:00 PM.. Reason: Retro-fitted quoted code tags to match previous post, as edited by me.
# 7  
Old 01-31-2017
Simplest solution you can try below:
Code:
cat temp.txt  | grep -v -i "^start$" | grep -v -i "^end$" | paste -d, - - - -



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

Last edited by RudiC; 01-31-2017 at 02:29 PM.. Reason: Added CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Unix Shell Scripting

I'm sorry if this doesn't go here, but I'm in depserate need of help with my last unix homework. Anyways, I'm taking summer classes, and one of them is UNIX. I've understood everything thus far, but I'm having a killer time with how my instructor has worded the problems for shell scripting. I... (3 Replies)
Discussion started by: dw15
3 Replies

2. UNIX for Dummies Questions & Answers

Unix Shell Scripting( Calling from Unix to PLSQL)

Hello Experts, I have the following questions to be discussed here at this esteemed discussion forum. I have two Excel sheets which contain Unix Commands llike creating directory the structure/ftp/Copy/Zip etc to basically create an environment. I need help in understanding some of... (1 Reply)
Discussion started by: faizsaadq
1 Replies

3. Shell Programming and Scripting

Unix shell scripting help

Hi , I am beginner in Unix. I have a string /aa/bb/cc/dd. I want to extract the sub string /aa/bb/cc from this . How do i do that in bash? (1 Reply)
Discussion started by: vignesh53
1 Replies

4. Shell Programming and Scripting

unix shell scripting

hi experts, i m a btech student......my mini project is making a secure unix shell which is intented to stop mal practice in educational environment...........in other words need to create a new shell in which commands like cp do not work....can anyone suggest me how to begin with it....refrence... (3 Replies)
Discussion started by: vikas MENON.S
3 Replies

5. Shell Programming and Scripting

Need logic for shell scripting

I am comfortable with shell scripting. I need a better logic for below, I've a reference file, to take the parameter value. For example in the no of channel is 6 i have get the commands as written below. I need need a differenct logic to extract the commands. NO_OF_CHANNEL=6 case... (3 Replies)
Discussion started by: ilugopal
3 Replies

6. Shell Programming and Scripting

Unix Shell Scripting

I 'm new to unix shell scripting can some one guide me to any e-book or link from where i can learn unix shell scripting .. i want to learn create interactive scripts for my day to day solaris work. Any help would be appreciated (1 Reply)
Discussion started by: fugitive
1 Replies

7. Shell Programming and Scripting

Shell Scripting (Unix)

Hello Guys, Pls wot command can i use to veiw the configuration and usage of the Unix file systems T.T (3 Replies)
Discussion started by: tt1ect
3 Replies

8. Shell Programming and Scripting

Unix shell scripting

Hi, we are writing this fields dynamically retrieved from database and writing into the file. $bmpRec = $bmpRec.'|'.$cust_id; # sp4 $bmpRec = $bmpRec.'|'.$serv_id; # sp5 $bmpRec = $bmpRec.'|'.$site_id; # sp6 $bmpRec = $bmpRec.'|'.$loc_id; # sp7 ... (4 Replies)
Discussion started by: Maruthi Kunnuru
4 Replies

9. Shell Programming and Scripting

difference between AIX shell scripting and Unix shell scripting.

please give the difference between AIX shell scripting and Unix shell scripting. (2 Replies)
Discussion started by: haroonec
2 Replies

10. Shell Programming and Scripting

Parent/Child Scripting logic Question

I have Parent script that will execute a child script. The child script will take about 2 hours to run and I wanted to somehow build a logic for parent.sh to wait and perform ps -ef|grep cp|grep -v on the child.sh cp process and loop and check return code of child.sh $? as well. Once they are both... (3 Replies)
Discussion started by: jigarlakhani
3 Replies
Login or Register to Ask a Question