Print Unknown Number of User Inputs in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print Unknown Number of User Inputs in awk
# 1  
Old 10-10-2015
Print Unknown Number of User Inputs in awk

Hello,

I am new to awk and I am trying to figure out how to print an output based on user input.

For example:

Code:
ubuntu:~/scripts$ steps="step1, step2, step3"

ubuntu:~/scripts$ echo $steps
step1, step2, step3

I am playing around and I got this pattern that I want:

Code:
ubuntu:~/scripts$ echo $steps | awk -F, '{print "Step Taken:" "\n" "- "$1 "\n" "-"$2 "\n" "-" $3}'

Step Taken:
- step1
- step2
- step3

The question is this:

What if the users input 4 or more steps (always separated by comma which I will use as a field separator just like the example above)?

How can I print an output that will include step4 or more like this?

Code:
Step Taken:
- step1
- step2
- step3
- step4
- step5

So on and so forth...

Thanks!
# 2  
Old 10-10-2015
Hello tattoostreet,

You could try following for same.
Code:
steps="step1, step2, step3, step4, step5, step7, step8, step9, step10"
echo $steps  | awk -F", " '{for(i=1;i<=NF;i++){print $i}}'

Output will be as follows.
Code:
step1
step2
step3
step4
step5
step7
step8
step9
step10

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 10-10-2015
This is what I need!! Thank you so much!
# 4  
Old 10-10-2015
Try also
Code:
steps=" step1, step2, step3, step4, step5, step7, step8, step9, step10"
echo "$steps" | tr ' ,' '-\n'
-step1
-step2
-step3
-step4
-step5
-step7
-step8
-step9
-step10

( this needs a leading space before step1)

---------- Post updated at 19:54 ---------- Previous update was at 19:51 ----------

or
Code:
echo "${steps//, /    
- }"
 step1
- step2
- step3
- step4
- step5
- step7
- step8
- step9
- step10

(Here, even a leading space doesn't help)
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Noob question: How to check the total number of inputs entered by user?

Say I have this line: read -p "Enter 3 numbers: " num1 num2 num3; I want to write a while loop that repeatedly asks for input if the number of inputs entered is not equal to 3. I don't know the correct command to find the number of inputs entered. Help, please? (4 Replies)
Discussion started by: jejemonx
4 Replies

2. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

3. Shell Programming and Scripting

awk code to ignore the first occurence unknown number of rows in a data column

Hello experts, Shown below is the 2 column sample data(there are many data columns in actual input file), Key, Data A, 1 A, 2 A, 2 A, 3 A, 1 A, 1 A, 1 I need the below output. Key, Data A, 2 A, 2 A, 3 A, 1 A, 1 A, 1 (2 Replies)
Discussion started by: ks_reddy
2 Replies

4. Shell Programming and Scripting

AWK print number of records, divide this number

I would like to print the number of records of 2 files, and divide the two numbers awk '{print NR}' file1 > output1 awk '{print NR}' file2 > output2 paste output1 output2 > output awl '{print $1/$2}' output > output_2 is there a faster way? (8 Replies)
Discussion started by: programmerc
8 Replies

5. Shell Programming and Scripting

print max number of 2 columns - awk

Is it possible to print max number of 2 columns - awk note: print max if the integer is positive and print min if the integer is negative input a 1 2 b 3 4 c 5 1 d -3 -5 d -5 -3 output a 2 b 4 c 5 d -5 d -5 (4 Replies)
Discussion started by: quincyjones
4 Replies

6. UNIX for Dummies Questions & Answers

How do you print the number of processes that each user is currently running in Unix?

Ok, so I know there's a way to do this, but I've been trying to find out all afternoon with no luck. I think it should print out something like this: 1 bin 2 daemon 6 duo Where the numbers on the left are the number of processes being run by the user whose name is listed on the right. Is... (4 Replies)
Discussion started by: Duo11
4 Replies

7. Shell Programming and Scripting

Please help. How to print number with 'awk'?

Dear all, I have to transform a bunch of files into a new format. how to do it with bash script, especially with 'awk' command. the original data O,0,0.2839896442,-1.1628392411,-1.5976515479 Cr,0,-0.0502858628,-0.1725974837,-0.380627632 C,0,2.4506658279,-0.476964918,0.2299298249 ... (3 Replies)
Discussion started by: liuzhencc
3 Replies

8. Shell Programming and Scripting

How do I print out lines with the same number in front using awk?

Hi, I need help in printing out the dates with the largest value in front of it using awk. 436 28/Feb/2008 436 27/Feb/2008 436 20/Feb/2008 422 13/Feb/2008 420 23/Feb/2008 409 21/Feb/2008 402 26/Feb/2008 381 22/Feb/2008 374 24/Feb/2008 360... (7 Replies)
Discussion started by: SIFA
7 Replies

9. Shell Programming and Scripting

read n number of inputs

Hello, I think its a sinple query but somehow i m stucked up here... I am trying to enter n number of inputs from the user and write them in an input file ie row wise... I tried standard commands like $echo "enter the inputs for the file" $read var1 var2 var3 var4 test1 test2... (14 Replies)
Discussion started by: er_aparna
14 Replies

10. Shell Programming and Scripting

How to print number of lines with awk ?

Can some body tell me how to print number of line from a particular file, with sed. ? Input file format AAAA BBBB CCCC SDFFF DDDD DDDD Command to print line 2 and 3 ? BBBB CCCC And also please tell me how to assign column sum to variable. I user the following command it... (1 Reply)
Discussion started by: maheshsri
1 Replies
Login or Register to Ask a Question