Need help with unix command

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GT98
    FanBoy
    • Aug 1999
    • 4408

    #1

    Need help with unix command

    Ok I have the following three files:
    Code:
    File1.txt					File2.txt			File3.txt
    Smith Furniture Co.                            101			Active
    Wells Manufacturing                           202			Inactive
    Rose Department Store		           314			     Active
    Haywood Resort			               184			 Inactive
    Martha Stewart & Co.		             341		       Unknown
    And I need to use a UNIX command to display the following, out of those three txt files

    101 Smith Furniture Co. Active

    Any suggestions or hints?
    Last edited by GT98; 17 July 2006, 04:58.
    Why is it called tourist season, if we can't shoot at them?
  • Jessterw
    Style Master, Rtd.
    • Jul 2002
    • 4265

    #2
    What relation do the files have to the items you listed below?
    Does one file contain the name, one the number/id, and one the state?

    If the latter is correct then you're going to need more of a script to handle this sort of aggregate output.

    Depending on if you are wanting to output a specific set or all would determine what you need for output, but basically you will need to loop through the lines of each file and concatenate the values as you specified. *You would grab the values for the same line in each file, output, and then proceed to the next line in each file.
    Last edited by Jessterw; 16 July 2006, 15:27.
    “And, remember: there's no 'I' in 'irony'” ~ Merlin Mann

    Comment

    • Wombat
      Super MURCer
      • Aug 2000
      • 9498

      #3
      You really haven't given enough information here to be clear. Was that the name of the three files, plus example data?

      Code:
      grep -n "101 Smith Furniture Co. Active" *.txt
      Gigabyte P35-DS3L with a Q6600, 2GB Kingston HyperX (after *3* bad pairs of Crucial Ballistix 1066), Galaxy 8800GT 512MB, SB X-Fi, some drives, and a Dell 2005fpw. Running WinXP.

      Comment

      • GT98
        FanBoy
        • Aug 1999
        • 4408

        #4
        I fixed the formatting problem I had when I posted this...does this make it clearer?

        I basically need to take infomation from each text file and have it output as a single line following this example:

        101 Smith Furniture Co. Active
        Why is it called tourist season, if we can't shoot at them?

        Comment

        • Jon P. Inghram
          Super MURCer
          • Oct 1999
          • 3878

          #5
          I think gcc or whatever equivalent your Unix system has would probably be the best command for doing this.

          Comment

          • TnT
            Super MURCer
            • Sep 1999
            • 1644

            #6
            Do you need to display them all at once (ie merge the files) or do you need to do random access on a single row?
            Gigabyte GA-K8N Ultra 9, Opteron 170 Denmark 2x2Ghz, 2 GB Corsair XMS, Gigabyte 6600, Gentoo Linux
            Motion Computing M1400 -- Tablet PC, Ubuntu Linux

            "if I said you had a beautiful body would you take your pants off and dance around a bit?" --Zapp Brannigan

            Comment

            • Tjalfe
              Super MURCer
              • Nov 2002
              • 3053

              #7

              Code:
              perl -e '$col1=1; $col2=0;' -e '($f1,$f2)=@ARGV; open(F1,$f1); while (<F1>) {s/\r?\n//; @F=split /\t/, $_; $line1{$F[$col1]} .= "$_\n"}; warn "\nJoining $f1 column $col1 with $f2 column $col2\n$f1: $. lines\n"; open(F2,$f2); while (<F2>) {s/\r?\n//; @F=split /\t/, $_; $x = $line1{$F[$col2]}; if ($x) {$x =~ s/\n/\t$_\n/g; print $x; $merged++}} warn "$f2: $. lines\nMerged file: $merged lines\n";' ortho.tab human_func.tab > fly_func.tab
              We have enough youth - What we need is a fountain of smart!


              i7-920, 6GB DDR3-1600, HD4870X2, Dell 27" LCD

              Comment

              • GT98
                FanBoy
                • Aug 1999
                • 4408

                #8
                Originally posted by TnT
                Do you need to display them all at once (ie merge the files) or do you need to do random access on a single row?

                single line with the selected information as in the example...
                Why is it called tourist season, if we can't shoot at them?

                Comment

                • Wombat
                  Super MURCer
                  • Aug 2000
                  • 9498

                  #9
                  I'm still not totally clear on what PART of that stuff you're searching for, but I came up with something. I hope you can read shell script.

                  I assumed you're doing something like ./searchscript File1.txt File2.txt File3.txt Smith

                  The only tricky part about this one is that most people don't know how to use file descriptors. I open your files as descriptors 4, 5, and 6, and then read from them, slap that all together as a line, and grep for your search string in the merged line.

                  Code:
                  FILE1="$1"
                  FILE2="$2"
                  FILE3="$3"
                  SEARCH="$4"
                  
                  exec 4< $FILE1
                  exec 5< $FILE2
                  exec 6< $FILE3
                  
                  while read -u 4 line1
                  do
                  	read -u 5 line2
                  	read -u 6 line3
                  	total_line="$line1 $line2 $line3"
                          echo $total_line | grep $SEARCH
                  done
                  Gigabyte P35-DS3L with a Q6600, 2GB Kingston HyperX (after *3* bad pairs of Crucial Ballistix 1066), Galaxy 8800GT 512MB, SB X-Fi, some drives, and a Dell 2005fpw. Running WinXP.

                  Comment

                  • GT98
                    FanBoy
                    • Aug 1999
                    • 4408

                    #10
                    duh this was easy....the command I need is this
                    Code:
                    ls | paste File2.txt File1.txt File3.txt
                    Why is it called tourist season, if we can't shoot at them?

                    Comment

                    • Jessterw
                      Style Master, Rtd.
                      • Jul 2002
                      • 4265

                      #11
                      Ahhh... yes, now I see what you were wanting to do. In that case you might want to also try:

                      ls | paste -s -d'\t\t\n' File2.txt File1.text File3.txt

                      That'll give you a tab-delimited listing of all the lines in the files (in the format you desire).
                      “And, remember: there's no 'I' in 'irony'” ~ Merlin Mann

                      Comment

                      Working...