Find the answer to your Linux question:
Results 1 to 5 of 5
I have 2 files containing data. Each file has text that corresponds line by line with the other. I want to use data from each line, sequentially, as inputs. For ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2011
    Location
    Michigan
    Posts
    8

    Looking for suggestions in dealing with lines from multiple files.


    I have 2 files containing data. Each file has text that corresponds line by line with the other.

    I want to use data from each line, sequentially, as inputs.

    For example:

    File1:
    A
    B
    C

    File2:
    1
    2
    3

    I need to use A and 1 for a particular job, then B and 2, C and 3 and so on until the end of the files.

    I've tried a few things but I end up getting the entire contents of both files as inputs, rather than just the specific lines.
    Would I be able to load the contents of each file into arrays and use each line one by one in a for loop? other suggestions?

    edit:
    I'd like the A and 1, B 2 etc to be in variable form for use.
    Last edited by conflict; 08-18-2014 at 07:43 AM.

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,411
    You can use the paste command:
    Code:
    cat << EOF > file1
    A
    B
    C
    EOF
    
    cat << EOF > file2
    1
    2
    3
    EOF
    
    
    paste file{1,2}
    A	1
    B	2
    C	3
    
    rm file{1,2}
    paste also lets you set a delimiter if needed. By default it is a TAB.
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Location
    Michigan
    Posts
    8
    Would it be possible to set A and 1 to a variable $A $1 ?

  4. $spacer_open
    $spacer_close
  5. #4
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,411
    Sure
    Code:
    paste file{1,2} | while read var1 var2; do echo -e "var1: ${var1}\t var2: ${var2}"; done
    var1: A	 var2: 1
    var1: B	 var2: 2
    var1: C	 var2: 3
    You could also parse the vars into an array with read -a

    Two notes:
    - The data integrity depends on the line numbers.
    Just one empty line in one of the input files and the order is wrong.
    This is a bit fragile imho
    - You might want to look into ruby or python.
    If you want to build complex data structures, verify input and intend to do some data mangling then bash becomes a headache quite fast.
    You must always face the curtain with a bow.

  6. #5
    Just Joined!
    Join Date
    Jan 2011
    Location
    Michigan
    Posts
    8
    Quote Originally Posted by Irithori View Post
    Sure
    Code:
    paste file{1,2} | while read var1 var2; do echo -e "var1: ${var1}\t var2: ${var2}"; done
    var1: A	 var2: 1
    var1: B	 var2: 2
    var1: C	 var2: 3
    You could also parse the vars into an array with read -a

    Two notes:
    - The data integrity depends on the line numbers.
    Just one empty line in one of the input files and the order is wrong.
    This is a bit fragile imho
    - You might want to look into ruby or python.
    If you want to build complex data structures, verify input and intend to do some data mangling then bash becomes a headache quite fast.
    Yeah this is definitely more of a quick and dirty script.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •