Separating words in a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Separating words in a line
# 1  
Old 05-27-2016
Separating words in a line

Hi
I have the following file
Code:
[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
Code:
[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 Smilie
# 2  
Old 05-27-2016
Hello Priya,

Could you please try following and let me know if this helps.
Code:
awk -F".*= " '{print $NF}'  Input_file

Output will be as follows.
Code:
50988 41498   45      0       0       0
1500
1

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

Thanks,
R. Singh
# 3  
Old 05-27-2016
Could you not just do:

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

This will output:

Code:
 50988 41498   45      0       0       0
 1500
 1


Last edited by RudiC; 05-27-2016 at 08:04 AM.. Reason: Added code tags.
# 4  
Old 05-27-2016
Quote:
Originally Posted by andy391791
Could you not just do:
Code:
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.
Code:
 awk -F"= " '{print $NF}'  Input_file

Thanks,
R. Singh
These 2 Users Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 05-27-2016
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)
# 6  
Old 05-27-2016
Code:
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
Code:
set -f # no wildcard expansion

before the loop.
# 7  
Old 05-27-2016
Quote:
Originally Posted by Priya Amaresh
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)
Hello Priya,

Could you please try following and let me know if this helps.
Code:
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.
Code:
50988 41498 45 0 0 0
1500
1

If you want to get all variables then you could do following too.
Code:
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.
Code:
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

Last edited by RavinderSingh13; 05-27-2016 at 07:26 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. Shell Programming and Scripting

Search words in multiple file line by line

Hi All I have to search servers name say like 1000+ "unique names" line by line in child.txt files in another file that is a master file where all server present say "master.txt",if child.txt's server name matches with master files then it print yes else no with server name. (4 Replies)
Discussion started by: netdbaind
4 Replies

3. Shell Programming and Scripting

Remove last few words from Line

Hi I would like to remove last few words from File Could anybody Help on it. ps -ef | grep mgr.prm | awk '{print $10}' /opt/app/dummyd/xyz/dirprm/mgr.prm /opt/app/dummy/xyz/dirprm/mgr.prm /opt/app/dummy/xyz/dirprm/mgr.prm I want output like /opt/app/dummyd/xyz... (4 Replies)
Discussion started by: tapia
4 Replies

4. Shell Programming and Scripting

get few words from same line

Hello all i tried awk , cut but i think something missing i have this line @@XYMONDCHK-V1|.acklist.|developer_instead|rendy_google_yagom|1323977582|1323979382|1323979382|1|admin|test case i want cut some words to be in new line like this... (6 Replies)
Discussion started by: mogabr
6 Replies

5. Shell Programming and Scripting

Copying x words from end of line to specific location in same line

Hello all i know it is pretty hard one but you will manage it all after noticing and calculating i find a rhythm for the file i want to edit to copy the last 12 characters in line but the problem is to add after first 25 characters in same line in other way too copy the last 12 characters... (10 Replies)
Discussion started by: princesasa
10 Replies

6. Shell Programming and Scripting

How to print the words in the same line with space or to the predefined line?

HI, cat test abc echo "def" >> test output is cat test abc def the needed output is cat test abc def and so on (5 Replies)
Discussion started by: jobycxa
5 Replies

7. Shell Programming and Scripting

separating comma delimited words

Hi, I have file with text ________________________________ GROUP:firstname1.lastname1,first_name2.last_name2,first_name3.last_name3 HEAD:firstname.lastname ________________________________ I need help to pick the names separately ie.. Need out put as var1 =firstname1.lastname1... (4 Replies)
Discussion started by: rider29
4 Replies

8. Shell Programming and Scripting

print only last two words of a line

can u help me out to print last two words of each sentence of a file. for example. contents of input file: i love songs my favourite songs sent songs all kind good buddy Ouput file should contain: love songs favourite songs sent all kind good buddy (5 Replies)
Discussion started by: pradeepreddy
5 Replies

9. Shell Programming and Scripting

how to get line number of different words if exists in same line

I have some txt files. I have to create another text file which contains the portion starting from the format "Date Sex Address" to the end of the file. While using grep -n on Date it also gives me the previous line containg Date. and also Date may be DATE in some files. My file is like this... (10 Replies)
Discussion started by: Amiya Rath
10 Replies

10. Shell Programming and Scripting

count no of words in a line

hi i have a string like str=abc def ghi jkl now i want to count the no of words in the string please help (7 Replies)
Discussion started by: satish@123
7 Replies
Login or Register to Ask a Question