echo -n "##### STATIONS? #######"
read station
awk -v input="$station" '
        n = split(tolower(input), user)
        fmt = "%-4s\n"
    /stab_site clear/ {
        for (i=1; i<=n; i++)
            printf fmt, "stab_site" user[i]
' ./data > data_2

Let assume user's input: ankr ista for stations. After the execution of codes, in data file, after the "stab_site clear" line, user's input is written as follows;


I need to give one space between "stab_site" and "station_ids" in the codes. I also need to write the lines after one space from the beginning of the line as follow;

stab_site ankr
stab_site ista

let split

printf fmt, "stab_site" user[i]
  • "stab_site" user[i] will make stab_sitenakr as there is no space after site
  • printf fmt, "stab_site" user[i] is turn to
  • print "%-4s\n" , stab_sitenakr which print left aligned stab_sitenakr

maybe you are looking for

printf fmt, "stab_site " user[i]

or (as per OP's request)

printf " %s %s\n", "stab_site", user[i]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.