String generation from user input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String generation from user input
# 8  
Old 10-03-2012
Thanks a lot for that, workes great when I try it out. However, I have some troubles invoking it in my script.

I have the following:

Code:
read -p "  Blades?: " blades

blstr=seq 1 14|paste -s|cut -f "$blades"|tr '\t' ' '
hsgen="sh hostgen $blstr > hfil"
eval $hsgen

I guess I need an eval command or similar somewhere?

I'm a newbie on this. Learning, but not at the speed I had hoped...

Tobbe
# 9  
Old 10-03-2012
First, the variable blstr is not assigned the desired result unless you use "command substitution", i.e. $(seq ... ). Then, why do you use the hsgen variable at all instead of issuing the command itself? Third, $hsgen will execute by itself except for the redirection; if you want that, use eval $hsgen.
This User Gave Thanks to RudiC For This Post:
# 10  
Old 10-03-2012
Some systems (such as Solaris 10 and 11) have neither jot nor seq utilities. If you want to stay in awk rather than use jot, perl, or seq the following will work:
Code:
#!/bin/ksh
printf "%s\n" "1-3,6,7,9-11" "101-105,1000,10010-10013" | awk -F, '{
    no = 0 # number of values output for this input line
    # Loop through comma separated input fields.
    for(i = 1; i <= NF; i++) {
        n = split($i, range, "-")
        # Loop through the dash separated range.
        # Note that if there is no dash in $i, range[1] == range[n].
        for(j=range[1]; j<=range[n]; printf("%s%d", no++ ? " " : "", j++));
    }
    printf("\n")
}'

With the two lines of input I gave it, it produces the following output:
Code:
1 2 3 6 7 9 10 11
101 102 103 104 105 1000 10010 10011 10012 10013

# 11  
Old 10-03-2012
Rudic, thanks a million.

The reason for using the hsgen part like it is, is that I need redirection to a file.

Thanks again!

Tobbe

---------- Post updated at 06:32 AM ---------- Previous update was at 06:30 AM ----------

Don Cragun, I'm on Suse Enterprise, so seq works fine. Thanks for the effort though, awk is amazingly powerful.

Tobbe
# 12  
Old 10-03-2012
I think what RudiC was asking was: Why are you using:
Code:
hsgen="sh hostgen $blstr > hfil"
eval $hsgen

instead of: hostgen $blstr > hfil?
# 13  
Old 10-03-2012
bash version (space separated list, use IFS=, read ... for comma separated):
Code:
while read -p "  Blades?: " -a blades
do
  for b in "${blades[@]}"
  do
    for ((i=${b%%-*}; i<=${b##*-}; i++))
    do 
      printf "%s" "$i "
    done  
  done
  echo
done

# 14  
Old 10-03-2012
Quote:
Originally Posted by alister
Simplifying Ygor's inspired approach:
Code:
seq -s ' ' 14 | cut -d ' ' -f "$n"

Quote:
Originally Posted by Don Cragun
Some systems (such as Solaris 10 and 11) have neither jot nor seq utilities.
One possible approach that's POSIX-blessed and does not use sh nor awk (for fun):
Code:
bc <<EOBC | paste -sd ' ' | cut -d ' ' -f $n
    while (x < 14) ++x
EOBC

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert a user input string after matched string in file

i am having file like this #!/bin/bash read -p 'Username: ' uservar match='<color="red" />' text='this is only a test so please be patient <color="red" />' echo "$text" | sed "s/$match/&$uservar\g" so desireble output what i want is if user type MARIA this is only a test so please... (13 Replies)
Discussion started by: tomislav91
13 Replies

2. Shell Programming and Scripting

User input and run awk using the input

I am trying to allow a user to enter in text and then store that text in a variable $gene to run in an awk command in which those values are used to run some calculations. I am getting syntax errors however, when I try. Thank you :). The awk runs great if it is a pre-defined file that is used,... (7 Replies)
Discussion started by: cmccabe
7 Replies

3. Homework & Coursework Questions

Function to Check if string input from user is alphabetic only

Good Evening. I'm new to C. Can you please help me. I'm creating an error checking function, user will input a string, this will check if the input is all alphabet or all letters only. If there is a digit or other special char, it will print Error then ask input from user again. Here's my... (1 Reply)
Discussion started by: eracav
1 Replies

4. Shell Programming and Scripting

Script interacts with user , based on user input it operates

i have a script which takes input from user, if user gives either Y/y then it should continue, else it should quit by displaying user cancelled. #!/bin/sh echo " Enter your choice to continue y/Y OR n/N to quit " read A if then echo " user requested to continue " ##some commands... (7 Replies)
Discussion started by: only4satish
7 Replies

5. UNIX for Dummies Questions & Answers

Delete a line containing a string from user input

I have code that accepts input from a user, and when the user hits enter it is supposed to delete that whole line from the file. echo "Which record? " read record sed '/$record/d' file However, it does not delete it. Any help? (1 Reply)
Discussion started by: itech4814
1 Replies

6. Shell Programming and Scripting

How to get the user input recursively until the user provides valid input

Hi, echo "Enter file name of input file list along with absolute path : " read inputFileList if then for string in `cat inputFileList` do echo $string done else echo " file does not exist" fi From the above code, if the user enters a invalid file... (1 Reply)
Discussion started by: i.srini89
1 Replies

7. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

8. Shell Programming and Scripting

awk- report generation from input file

I have input file with below content: Person: Name: Firstname1 lastname1 Address: 111, Straat City : Hilversum Person: Name : Fistname2 lastname2 Address: 222, street Cit: Bussum Person: Name : Firstname2 lastname3 Address: 333, station straat City: Amsterdam I need... (6 Replies)
Discussion started by: McLan
6 Replies

9. UNIX for Dummies Questions & Answers

new user to the next generation of the new WORLD UNIX

am a windows Prof .... i deside to go with the UNIX .... but how can i start ......plz help me ........ (1 Reply)
Discussion started by: ekarak
1 Replies

10. Shell Programming and Scripting

String extraction from user input - sh

Hi, I have a shell script to build components of a product. The follow snippet will explain what I am doing. # !/bin/sh for choice in "$@" ; do case $choice in "o") echo "Calling $choice" ; o ;; "i") echo... (8 Replies)
Discussion started by: vino
8 Replies
Login or Register to Ask a Question