-1

I want to combine data from three files that have whitespace-separated columns.

first file:

CYCLE_NUMBER    output
---------------------
32                1
28                2

second file:

DATALOG_OUTPUT    Time    CELL_TYPE    MUX_SELECT    CYCLE_NUMBER
-
DATALOG_OUTPUT    9500        0           0              32
DATALOG_OUTPUT    19500       1           4              12
DATALOG_OUTPUT    29500       2           8              28

third file:

MUX_SEL    CUTNO
-
8        combo_1
0        combo_2
12       combo_3

I want the output as

DATALOG_OUTPUT    Time    CELL_TYPE    MUX_SELECT    CYCLE_NUMBER    CUTNO
-
DATALOG_OUTPUT    9500        0           0              32         combo_2
DATALOG_OUTPUT    29500       2           8              28         combo_1

I want to implement the following process to create the desired output file.  Repeatedly (for every cycle in the first file):

  1. Read a cycle number from the first file.
  2. grep (search for) that cycle number in the second file and extract/capture that row.  (In my attempt to solve this, shown below, I save the line in a temporary/intermediate file called output_1.)
  3. Extract the mux_select value from that row.
  4. grep for this mux_select value in the third file and extract/capture that row. (In my attempt to solve this, I save the line in a temporary/intermediate file called output_2.)
  5. paste the two lines, writing to the final output file.

I tried this by writing this script:

foreach cycle (`cat first_file | awk '{print $1}'`)
  set cycle_number = ` cat second_file | grep -w $cycle`
  echo $cycle_number >> output_1
  foreach mux (`cat output_1 | awk '{print $3}'`)
    set mux_select = ` cat third_file | grep -w $mux`
    echo $mux_select >> output_2
    paste output_1 output_2 >> output_file
  end
end
  • Have you tried anything? You’re more likely to get help if you try to solve this yourself, show us what you’ve tried, and explain where you got stuck. Hint: look at join. … … … … … … P.S. Do you really want both MUX_SELECT and MUXSEL in your output (when they’re the same thing)?  And why does the last line of your desired output say “8” and “combo_3”, when your third input file associates MUXSEL “8” with “combo_1”? – Scott Aug 10 '16 at 21:17
  • oh so sorry...it was my mistake..i edit the files again. and yes i tried by writing this script. – Ankita Agrawal Aug 11 '16 at 6:21
  • (1) I advise you to write shell scripts for the POSIX shell or bash, and not the C shell. (a) See Csh Programming Considered Harmful. (b) You’ll probably get better help on Stack Exchange for sh scripts than you will for csh scripts.  (2) You should always quote your shell variable references (e.g., "$cycle" and "$cycle_number", etc.) unless you have a good reason not to, and you’re sure you know what you’re doing.  … (Cont’d) – Scott Aug 14 '16 at 0:47
  • (Cont’d) …  (3) You’re using cat way more than you need to; see UUoC.  For example, cat first_file | awk '{print $1}' can be simplified to awk '{print $1}' first_file, and cat second_file | grep -w $cycle can be simplified to grep -w "$cycle" second_file (note the added quotes), etc.  … (Cont’d) – Scott Aug 14 '16 at 0:47
  • (Cont’d) …  (4) It’s often a good idea to put -- between a command’s option(s) and its argument(s) — especially when the argument(s) come from outside the script (i.e., they come from user data, and are not known until run time).  This protects against argument(s) that begin with - and therefore “look like” options.  For example, grep -w "$cycle" second_file should be grep -w -- "$cycle" second_file.  In particular, this would prevent the error message that you’re currently getting when $cycle gets the value --------------------- from the first file.  … (Cont’d) – Scott Aug 14 '16 at 0:48
1

Solution in TXR:

Code in join.txr:

CYCLE_NUMBER    output
---------------------
@(collect)
@cycno @(skip)
@  (next)
@  (skip)
DATALOG_OUTPUT @time @cellt @muxsel @cycno
@  (next)
@  (skip)
@muxsel @cutno
@(end)
@(output)
DATALOG_OUTPUT    Time    CELL_TYPE    MUX_SELECT    CYCLE_NUMBER    CUTNO
-
@  (repeat)
DATALOG_OUTPUT    @{time 11}  @{cellt 11} @{muxsel   13} @{cycno 11}@cutno
@  (end)
@(end)

Run:

$ txr join.txr file1 file2 file3
DATALOG_OUTPUT    Time    CELL_TYPE    MUX_SELECT    CYCLE_NUMBER    CUTNO
-
DATALOG_OUTPUT    9500         0           0             32         combo_2
DATALOG_OUTPUT    29500        2           8             28         combo_1

Query only time 9500:

$ txr -Dtime=9500 join.txr file1 file2 file3
DATALOG_OUTPUT    Time    CELL_TYPE    MUX_SELECT    CYCLE_NUMBER    CUTNO
-
DATALOG_OUTPUT    9500         0           0             32         combo_2

Query only cycle number 28:

$ txr -Dcycno=28 join.txr file1 file2 file3
DATALOG_OUTPUT    Time    CELL_TYPE    MUX_SELECT    CYCLE_NUMBER    CUTNO
-
DATALOG_OUTPUT    29500        2           8             28         combo_1

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.