Generate print statements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Generate print statements
# 1  
Old 05-21-2017
Generate print statements

I need to pickup values provided in braces from a file with many records

Quote:
cat file.txt
s1:1000(dept17),10(dept2),50(dept3)
s2:10(dept2)
s3:100(dept50),99(dept49)
I am able to get the count of number of dept's in each line .How do we write a logic to generate a print statement like below instead of using a while loop
Code:
if the count is 0 it should generate awk -F'[()]' '{print $2 }'  
count is 1 it should generate awk -F'[()]' '{print $2 "," print $4}'  
count is 2 it should generate awk -F'[()]' '{print $2 "," print $4 "," print $6} '
count is 3 it should generate awk -F'[()]' '{print $2 "," print $4 "," print $6 "," print $8} '

similarly if the count is 8 it should generate the print statements with (2,4,6,8,10,12,14,16,18)
# 2  
Old 05-21-2017
What operating system are you using?

What shell are you using?

Why are you opposed to using a while loop?

Is this a homework assignment?

Where is the count of departments coming from?

What would a line in file.txt look like when the count of departments is 0?

Why do you want a trailing comma printed after the list of departments extracted from your input?

Once you have added a line with zero departments in it to your sample file.txt file contents, please show us the output you are trying to produce from that sample input file.

What have you tried to solve this problem on your own?

There are no braces in your sample input file (although there are pairs of matching parentheses). Please more clearly explain what you are trying to do.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Quick and easy way to comment out multi lined print statements

Is there a quick and easy way to comment out multi lined print statements? something like this? printf("3408 strings_line_tokens %s \n", strings_line_tokens); (6 Replies)
Discussion started by: cokedude
6 Replies

2. Shell Programming and Scripting

Sftp : not able to print the echo statements after the sftp transfer

I had the below sftp script working perfectly but the problem is I am not able to send the echo statements . #!/bin/sh echo "Starting to sftp..." sftp admin@myip << END_SCRIPT cd /remotepath/ lcd /localpath/ mget myfiles*.csv bye END_SCRIPT echo "Sftp successfully." echo echo... (11 Replies)
Discussion started by: scriptscript
11 Replies

3. Shell Programming and Scripting

Help with for/if/else print statements

{for(i in b){if(b-1&&a!=b){print i";\t\t"b}else{print "NEW:"i";\t\t"b} } } what I need is to just print else{print "NEW:"i";\t\t"b and not print {print i";\t\t"b} how do i get awk to not print the first bit? is there an ignore statement although i still need awk to use this statement... (2 Replies)
Discussion started by: slashbash
2 Replies

4. Shell Programming and Scripting

Too many if statements..

Hello. I am new here and new to scripting. I used to have a very basic script that worked for simple backup/restore of files. I have expanded it and well... I have ended up with a complete mess. It still backs up and restores but there is so many issues that stem from the many if statements I... (3 Replies)
Discussion started by: gameinn
3 Replies

5. Homework & Coursework Questions

Using While and If statements

1. The problem statement, all variables and given/known data: Two problems I need solving please. I created a script where the user types in 7 numbers as standard input and each one is then stored in an array. Now I need to perform the following calculations on those numbers: 1) Use a while... (11 Replies)
Discussion started by: jjb1989
11 Replies

6. Shell Programming and Scripting

Using While and If statements

Hi guys, Two problems I need solving please. I created a script where the user types in 7 numbers as standard input and each one is then stored in an array. Now I need to perform the following calculations on those numbers: 1) Use a while loop to determine the largest number in the range. ... (2 Replies)
Discussion started by: jjb1989
2 Replies

7. Shell Programming and Scripting

vi and if statements

Hi I am very new to Unix programming and shell scripting. I am trying t figure out how to write a little script that will output the number of directories. I can find the number of directories using ls -l | grep "^d" | wc -l I can not figure out how to do it so when I type the name... (8 Replies)
Discussion started by: Reddoug
8 Replies

8. Shell Programming and Scripting

If statements....

Good morning all! I want to know if Im interpreting this if statement below right. if(((1) || (0)) && (1)){ do stuff; } This is saying: if true piped into false, then true, then do stuff. Right? What does the && stand for? thanks in advance! ben (3 Replies)
Discussion started by: bigben1220
3 Replies

9. Shell Programming and Scripting

HELP!! if statements

I am kind of new in Unix and i have to make a menu. I want to put an if statement in the menu. you should enter the filename and it goes to that file. How do i do this? (1 Reply)
Discussion started by: trob
1 Replies

10. Shell Programming and Scripting

Please help on IF statements.

I had different problem scenarios with IF statement. Can any expert please enlighten me on the difference with these scenarios. Thank you. 1st Scenario: testdate=`date +%Y%m` test=`cat /var/log/database0.$testdate*.log | grep "Errors found during processing" | tail -10` if then ... (4 Replies)
Discussion started by: filthymonk
4 Replies
Login or Register to Ask a Question