Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9133393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:28:51+00:00 2026-06-17T08:28:51+00:00

I’m trying to get the follow code to read in variables from the user;

  • 0

I’m trying to get the follow code to read in variables from the user; files to search, a search string, wanted whitespace for output and the amount of fields to be output.

First issue is with the AWK command. If I enter a valid white space such as ” ” (A single space” or “\t” I am given the Unterminated string and syntax error, which only occurs if I request more than one field to be output (otherwise no whitespace is added on).

Secondly GREP seems to be a bit picky when using the search string. I’ve had to add double quotation marks to the start and finish of the variable in order for the entire string to be used.

#!/bin/bash
#****************************************************
#Name:          reportCreator.sh
#Purpose:       Create reports from log files
#Author:        
#Date Written:      11/01/2013
#Last Updated:      11/01/2013
#****************************************************

clear

#Determine what to search for
printf "Please enter input file name(s): "
read inputFile
printf "Please enter your search query: "
read searchQuery
printf "Please enter the whitespace character: "
IFS= read whitespace
printf "Please enter the amount of fields to be displayed: "
read fieldAmount

#Add quotation marks to whitespace and searchQuery
whitespace=\""$whitespace"\"
searchQuery=\""$searchQuery"\"

#Declare variables
declare -i counter=0
declare -a fields[$fieldAmount]
declare -a fieldInsert[$fieldAmount]

#While loop for entering fields
while [[ "$counter" -ne "$fieldAmount" ]]
do
        #Ask for field numbers
        printf "Please enter number for required field $((counter+1)): "
        read fields[$counter]
        ((counter++))
done

#Create function to add '$' before every field and the whitespace characters
function fieldFunction 
{
    for (( counter=0; counter <= ($fieldAmount-1); counter++ ))
    do
        fieldInsert[$fieldAmount]="$""${fields[$counter]}"
        if (( counter!=($fieldAmount-1) ))
        then
            printf "${fieldInsert[*]}$whitespace"
        else
            printf "${fieldInsert[*]}"
        fi  
    done
}
printf "%b\n"

tac $inputFile | grep "$searchQuery" | less #| awk '{print $(fieldFunction)}'

exit 0

Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:28:52+00:00Added an answer on June 17, 2026 at 8:28 am
    1. Grep doesn’t understand quotes, so delete the line that adds them to $searchQuery.
    2. Use double quotes instead of single quotes for awk, so $(fieldFunction) will expand.

    Fixing this (as well as uncommenting the awk, of course), it works:

    user@host 15:00 ~ $ cat script
    #!/bin/bash
    #****************************************************
    #Name:          reportCreator.sh
    #Purpose:       Create reports from log files
    #Author:        
    #Date Written:      11/01/2013
    #Last Updated:      11/01/2013
    #****************************************************
    
    clear
    
    #Determine what to search for
    printf "Please enter input file name(s): "
    read inputFile
    printf "Please enter your search query: "
    read searchQuery
    printf "Please enter the whitespace character: "
    IFS= read whitespace
    printf "Please enter the amount of fields to be displayed: "
    read fieldAmount
    
    #Add quotation marks to whitespace and searchQuery
    whitespace=\""$whitespace"\"
    
    #Declare variables
    declare -i counter=0
    declare -a fields[$fieldAmount]
    declare -a fieldInsert[$fieldAmount]
    
    #While loop for entering fields
    while [[ "$counter" -ne "$fieldAmount" ]]
    do
            #Ask for field numbers
            printf "Please enter number for required field $((counter+1)): "
            read fields[$counter]
            ((counter++))
    done
    
    #Create function to add '$' before every field and the whitespace characters
    function fieldFunction
    {
        for (( counter=0; counter <= ($fieldAmount-1); counter++ ))
        do
            fieldInsert[$fieldAmount]="$""${fields[$counter]}"
            if (( counter!=($fieldAmount-1) ))
            then
                printf "${fieldInsert[*]}$whitespace"
            else
                printf "${fieldInsert[*]}"
            fi
        done
    }
    printf "%b\n"
    
    tac $inputFile | grep "$searchQuery" | awk "{print $(fieldFunction)}"
    
    exit 0
    user@host 15:01 ~ $ cat file
    foo two three four
    foo two2 three2 four2
    bar two three four
    user@host 15:01 ~ $ bash script
    Please enter input file name(s): file
    Please enter your search query: foo
    Please enter the whitespace character:
    Please enter the amount of fields to be displayed: 2
    Please enter number for required field 1: 4
    Please enter number for required field 2: 2
    
    four2   two2
    four    two
    user@host 15:01 ~ $
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.