Parse a line which has different word length


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parse a line which has different word length
# 1  
Old 09-08-2009
Parse a line which has different word length

Hi All,
Please let me know a command to parse the below line and find the words,
I have a line like this
40609 39930
In this above line the two words are separted by space.The length of this two words may differ.
I want to put 40609 in var_one and 39930 in var_two.
Eg.
Input line is
40609 39930
echo $var_one
echo $var_two
(some command which should give the below ouput)
40609
39930

Thanks in advance,
Giri.
# 2  
Old 09-08-2009

If you are reading the line from a file:

Code:
read var_one var_two < "$file"

If it's already in a variable:

Code:
var="40609 39930"
read var_one var_two <<.
$var
.

Or:

Code:
var_one=${var% *}
var_two=${var#* }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

2. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

3. Shell Programming and Scripting

Average word length

If i use wc -m filename it will generate the number of characters and wc -w filename will generate number of words if i used this info by dividing number of characters/number of words it will give me misleading result as number of character will include spaces and punctuation as... (3 Replies)
Discussion started by: khaled79
3 Replies

4. Shell Programming and Scripting

Check for length which exceeds specified length in a line

Hi, I have a issue, I need to loop through a comma delimited file and check for the length which exceeds specified length , if Yes truncate the string. But my problem is , I do not have to check for all the fields and the field lenght is not same for all the fields. For ex: Say my line... (9 Replies)
Discussion started by: rashmisb
9 Replies

5. Shell Programming and Scripting

Parse variable length status,timestamp CSV

I have the output of a process which on status change of the object being processed appends status,timestamp to the record in the text file... so i end up with output something like: task123,TERMINAL,glob,5,INITIAL,2012-02-27 16:48:07,PREPARING,2012-02-27 16:49:06,SCHEDULED,2012-02-27... (2 Replies)
Discussion started by: KarmaPoliceT2
2 Replies

6. Shell Programming and Scripting

Parse a String for a Specific Word

Hello, I'm almost there with scripting, and I've looked at a few examples that could help me out here. But I'm still at a lost where to start. I'm looking to parse each line in the log file below and save the output like below. Log File AABBCGCAT022|242|3 AABBCGCAT023|243|4... (6 Replies)
Discussion started by: ravzter
6 Replies

7. Shell Programming and Scripting

Print word 1 in line 1 and word 2 in line 2 if it matches a pattern

i have a file in this pattern MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1... (7 Replies)
Discussion started by: bangaram
7 Replies

8. Shell Programming and Scripting

Shell script to parse a line and insert a word

Hi All, I have a file like this, data1,data2,,,data5,data6. i want to write a shell script to replace data3 with "/example/string". which means my data file should look like this . data1,data2,example/string],,data5,data6. Could you guys help me to get a sed command or any other command... (8 Replies)
Discussion started by: girish.raos
8 Replies

9. Shell Programming and Scripting

Perl Parse Word Cksum help

Hi all, I'm attempting to parse through a .bin file word by word and perform a cksum on each word using perl. I'm new to perl so I dont exactly know how to get started. Any help would be greatly appreciated. Thanks! (1 Reply)
Discussion started by: TeamUSA
1 Replies

10. Shell Programming and Scripting

How to parse the given word

Hi , i need to parse the dir /opt/net/Pro/inv/do/disc_001812 to get only dsic001812 . how to do the same using shell script. (2 Replies)
Discussion started by: MuthuAlagappan
2 Replies
Login or Register to Ask a Question