Parse and print a colon separated list : Newbie question

Linux version: RHEL 8.4

I have a huge file like below.

$ cat applist
manhlx138p:hrms
manhlx142p:sales
manhlx143p:finance
manhlx145p:IT

I want to print like below:

App hrms runs on manhlx138p
App sales runs on manhlx142p
.
.

I tried the following - Didn't work. Any idea how I can achieve the above requirement ?

$ for app in `applist`
> do
>     echo "$app runs on server ....."
> done
-bash: applist: command not found
$

With awk it is just one line of code:

awk -F: '{print "App " $2 " runs on " $1}' applist
1 Like

As the error says applist cannot be executed, remove the ticks..,
Once you have solved the read issue, you find yourself with $app being the complet line, how will solve that?
I would use awk...

Since I don't know your knowledge of Unix, I will show it can be done just with unix commands, but this is dirty:

your file I called nn:

manhlx138p:hrms
manhlx142p:sales
manhlx143p:finance
manhlx145p:IT

And now my command line: but I use new line for clarity:

MacBook-Pro wks % cat nn|tr ":" "  "|while read X1 X2 
do
echo "  "$X2" runs on "$X1
done
  hrms runs on manhlx138p
  sales runs on manhlx142p
  finance runs on manhlx143p
  IT runs on manhlx145p

It would be great that seeing what you did wrong, come back with a nice script:

We try to avoid the use of cat in such cases as it has an impact on performance
Try to find a solution using awk, if you are going for a loop, then we would like to see no cat command in it

2 Likes

I think you wanted
for app in `cat applist`
but the for splits on whitespace (space, tab, newline).
Even
for app in $(tr ':' ' ' < applist)
is not easy to handle.
Much better is the suggested
awk -F: ...
But this is awk (read lines, split to fields separated by a colon)
You can do it in the shell using read in a while loop. It was suggested already, but using tr is not good; an improvement:

while IFS=":" read server app
do
  echo "$app runs on $server"
done < applist

The read reads a line from stdin and splits on $IFS that is set just for the read; the fields go to the following variables.
The loop's stdin is redirected from the applist file.

3 Likes

THANK YOU AGG2020, VBE, MadeInGermany !

Hi MadeInGermany,

I want to use your WHILE loop to ssh to few servers.
With the below code, I managed to log in (ssh) to remote server. But, line 6 and Line 7 don't seem to work.
Can't I assign $app variable's value to another variable, once I am logged in (ssh) into another Linux machine ?

-- Just one server for now (for testing)

$ cat applist
manhlx138p:hrms
while IFS=":" read server app
do
  echo "$app runs on $server"
  ssh -n "oracle@$server" "   ### This starting double quote should not be in a separate line
  hostname;
  APP_NAME=$app;   ## Line 6
  echo $APP_NAME   ## Line 7
   "
done < applist

Output of above command. Output for echo $APP_NAME (Line 7 of above WHILE loop) is missing

hrms runs on manhlx138p
manhlx138p
1 Like

The local shell does the $var substitutions.
So you can do just

  echo $app

Or you must do an extra level of quoting

  APP_NAME=$app  # local substitution
  echo \$APP_NAME  # remote substitution

The local shell just dequotes to $APP_NAME and the remote shell does the substitution.

2 Likes