-1

I have a text file with 2 values in each line. I need to put these two values in two different variables i.e var and var2 and do further processing for each line. The delimiter is space between the two variables.


Comment converted into information in the question, using some of the formatting available in questions.

I have a text file, abc.txt:

apple jobs
microsoft gates

I want 'apple' in $var, 'jobs' in $var2 from the first line; same thing when iterated should give me 'microsoft' and 'gates' in the same $var and $var2. When I am using:

var=`awk -F' ' '{print $1}' abc.txt`

it's giving me 'apple', 'jobs' and 'microsoft' in var and 'gates' in var2.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 3
    What have you tried? Which command are you planning to use? Have you read the manual page for [`read`](http://www.gnu.org/software/bash/manual/bash.html#index-read)? Why not? What you're seeking to do is a very basic application of the `read` command — though if the processing requires `awk`, that too is a fine way to process data. Which is better depends on the 'further processing'. – Jonathan Leffler Sep 14 '15 at 13:13
  • Currently I am able to separate two values in a single line.Problem is with multiple lines.I am currently able to read each of these lines and print their count. – Snigdha Gayatri Sep 14 '15 at 13:21
  • You need to show what you've got, and explain what's up with it so we can help you. I've no idea what the problem is if you can separate the values. Are you after different variables for each line? Have you considered an array? – Jonathan Leffler Sep 14 '15 at 13:28
  • 1
    @SnigdhaGayatri please edit your question, with an example of the file, the expected outcome, and what you've already tired. Is it a single line in a text file? Is each pair on their own line? – AJefferiss Sep 14 '15 at 13:53
  • I have a text file,abc.txt : "apple jobs" in the firstline and "microsoft gates" in second line. I want apple in $var , jobs in $var2 from the first line, same thing when iterated shd give me msoft and gates in the same $var and $var2. When I am using var=`awk -F' ' '{print $1}' abc.txt its givin me apple,jobs and microsoft in var and gates in var2.. – Snigdha Gayatri Sep 14 '15 at 13:54
  • 1
    Please: update the question with the extra information so you can format it neatly. To indent code or data, type if in the edit box as you want it to appear, then select it, and use the **`{}`** button above the edit box to indent what's selected. – Jonathan Leffler Sep 14 '15 at 14:08
  • You should be getting `"apple microsoft"` in `var` from the command shown. Neither 'jobs' nor 'gates' should appear in it. You should not be using `awk` in this context. And generally, you should use `var=$(…)` in place of back-ticks (``var=`…` ``). – Jonathan Leffler Sep 14 '15 at 14:31
  • @SnigdhaGayatri you are almost certainly going about this completely wrong and instead of trying to read text into shell variables, you should be doing the whole thing in awk since shell is NOT a good tool for manipulating text while awk is. If you tell us what you plan to do with those variables and show the expected output from your whole script, we can help you. – Ed Morton Sep 14 '15 at 14:53
  • Thanks for the help, found the answer here http://unix.stackexchange.com/questions/113750/use-awk-to-split-line-into-array-and-use-that-arrays-values-in-calling-shell – Snigdha Gayatri Sep 16 '15 at 08:51

1 Answers1

3

Assuming you have the input file as

$ cat var2
a b
c d

If you want to assign two values in each row to two variables and operate them in a loop, you can do this

$ while read a b; do echo "two vars are: " $a $b; done < var2
two vars are:  a b
two vars are:  c d

Each iteration will reassign the values from the source file.

karakfa
  • 66,216
  • 7
  • 41
  • 56
  • `while read a b` will corrupt the data as it reads it. Always use `while IFS= read -r a b` unless you have a specific reason not to and fully understand all of the caveats (in this case you will want something other than `IFS=`). – Ed Morton Sep 14 '15 at 14:24
  • I have to disagree with that comment, @EdMorton. I'm sure you're going to disagree with my disagreement; that's a given. So I'm putting my objection on record, and we can leave the discussion there. If you want to discuss it, do so off record; my email is in my profile. – Jonathan Leffler Sep 14 '15 at 14:34
  • @JonathanLeffler what is it you disagree with? Try `read a < file` when file contains `a\tb` and you'll find `a` is populated with `atb` instead of `a\tb` as you'll get with `read -r a < file`. I don't want to get into a debate about it, `IFS= read -r var` IS the start of how to read a file correctly in shell (there's other things you should do with file descriptors too but they don't usually actually create problems). – Ed Morton Sep 14 '15 at 14:37