16

I need to read the following input into separate columns as variables? input.txt

b73_chr10   w22_chr9
w22_chr7    w22_chr10
w22_chr8    w22_chr6

I have written the following command;but I guess it is not correct.

value1=$(echo $line| awk -F\ '{print $1}' input.txt)
value2=$(echo $line| awk -F\ '{print $2}' input.txt)
2
  • 3
    Why are you setting the field separator to a value (;) that doesn't present in your input file?
    – jasonwryan
    Feb 8, 2016 at 20:28
  • Okk..I will edit it.
    – shome
    Feb 8, 2016 at 20:46

1 Answer 1

28

You can use the read shell builtin:

while IFS=" " read -r value1 value2 remainder
do
    ...
done < "input.txt"

Extra fields, if any, will appear in 'remainder'. The shell's default IFS (inter-field-seperator) consisting of white space characters will be used to split each line into its component fields.

3
  • How to provide the file name in this syntax ? Like the filename(for e.g input.txt) in this syntax ?
    – shome
    Feb 8, 2016 at 21:00
  • I've added input redirection to provide the name of the file to be read.
    – JRFerguson
    Feb 8, 2016 at 21:08
  • I have another query..if you dont mind.I added this condition inside the loop: sed 's/$value1/$value2/g' circos.conf Is it correct to work ?
    – shome
    Feb 8, 2016 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.