Separating words in a line

Hi
I have the following file

[root@teag8 ~]# cat red
it.d-state = 50988 41498   45      0       0       0
it.age_buffer= 1500
it.dir = 1

I need to grep lines that are present after "="
But when I do so, few lines has more than one value and some has one
How to parse the line
I have used the follwing code which parses first argument and "=" sign also

[root@teag8 ~]# while read line
> do
>     for word in $line
>     do
>         echo $word
>     done
> done < /root/red
it.d-state
=
50988
41498
45
0
0
0
it.age_buffer
=
1500
it.dir
=
1

Please help :confused:

Hello Priya,

Could you please try following and let me know if this helps.

awk -F".*= " '{print $NF}'  Input_file

Output will be as follows.

50988 41498   45      0       0       0
1500
1

Also on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .

Thanks,
R. Singh

Could you not just do:

awk -F "=" ' {print $2}' red

This will output:

 50988 41498   45      0       0       0
 1500
 1

Hello andy391791,

You could add a space in field separator else it will have spaces in the starting of the last field or 2nd field, so following should do the trick.

 awk -F"= " '{print $NF}'  Input_file
 

Thanks,
R. Singh

2 Likes

This worked when I provide entire file as input
But I need to store every grep value to a variable (i need to process each and every single line using loop)

while IFS=" =" read key line
do
  for word in $line
  do
    echo "$word"
  done
done < /root/red

Note that $line must be unquoted so the shell performs word splitting on it, but the shell also does wildcard matching against the current directory.
For safety do a

set -f # no wildcard expansion

before the loop.

Hello Priya,

Could you please try following and let me know if this helps.

while IFS='=' read -r var1 var2; do echo $var2; done < Input_file
OR
while IFS='=' read -r var1 var2; 
do 
    echo $var2; 
done < "Input_file"

Output will be as follows.

50988 41498 45 0 0 0
1500
1

If you want to get all variables then you could do following too.

while IFS='=' read -r var1 var2; do for i in $var2; do echo $i; done; done < "Input_file"
OR
while IFS='=' read -r var1 var2
do
    for i in $var2
    do
         echo $i
    done
done < "Input_file"

Output will be as follows.

50988
41498
45
0
0
0
1500
1
 

You could save these values into a variable and could use it then. Please let me know if this helps you.

Thanks,
R. Singh