Copying Information from One File to Another File in Shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copying Information from One File to Another File in Shell
# 1  
Old 07-13-2011
Copying Information from One File to Another File in Shell

Hello, I'm new to scripting and I need help moving text from one file to another file. Here are examples what the files look like.

File 1:
Code:
Ac-223     2.10m A             1      0      0     0 Fr-219    358 9.9000E-01             0        0.0             0        0.0             0        0.0 6.6720 0.02536 0.01903   7 118   0  297  29 223.019136 3.739E-183.739E-18
Ac-224     2.78h ECA         482      0      0     0 Ra-224    820 9.0900E-01 Fr-220    359 9.1000E-02             0        0.0             0        0.0 0.5662 0.04901 0.23248  15 211   0  398  29 224.021722 2.304E-172.304E-17
Ac-225     10.0d A          1165      0      0     0 Fr-221    360 1.0000E+00             0        0.0             0        0.0             0        0.0 5.8919 0.02476 0.01708   7 157   0  526  44 225.023229 6.777E-186.777E-18
Ac-226    29.37h B-ECA      1944      1      0     0 Th-226   1108 8.3000E-01 Ra-226    822 1.7000E-01 Fr-222    361 6.0000E-05             0        0.0 0.0003 0.29143 0.13271  14 140   5   99   1 226.026097 1.048E-171.048E-17
Ac-227   21.772y B-A        2205    114      0     0 Th-227   1109 9.8620E-01 Fr-223    362 1.3800E-02             0        0.0             0        0.0 0.0692 0.01502 0.00105  15 185   4  421  19 227.027752 1.009E-181.009E-18
Ac-228     6.15h B-         2869    188      0     0 Th-228   1110 1.0000E+00             0        0.0             0        0.0             0        0.0    0.0 0.44945 0.86714   7 329  54 1465   0 228.031021 3.991E-173.991E-17
Ac-230      122s B-         4725    308      0     0 Th-230   1112 1.0000E+00             0        0.0             0        0.0             0        0.0    0.0 0.92285 0.54396   7 218  42  720   0 230.036289 2.278E-172.278E-17

File 2:
Code:
Ac-223        2.10m       480
 2 8.38497E+00 6.06280E-05  X
 2 2.14756E-05 1.04461E-04  X
 2 1.00091E-03 4.27494E-04  X
 2 7.83209E-07 6.59000E-04  X
 2 3.57877E-02 2.82466E-03  X
 2 1.76193E-06 2.93700E-03  X
 2 3.20904E-04 3.59600E-03  X
 2 2.32345E-03 1.04149E-02  X
 2 2.73926E-05 1.07215E-02  X
 2 2.47953E-05 1.13895E-02  X
 2 3.99189E-03 1.18951E-02  X
 2 3.51076E-02 1.20348E-02  X
               [.....]
Ac-224        2.78h       682
 2 1.26364E+00 6.05941E-05  X
 2 1.52234E+01 7.12229E-05  X
 2 3.23790E-06 1.04068E-04  X
 2 4.64062E-05 1.11439E-04  X
 2 1.43848E-04 4.30159E-04  X
 2 1.76238E-03 4.31588E-04  X
 2 4.53773E-07 6.59000E-04  X
 2 2.11568E-06 6.72000E-04  X
 2 5.58834E-03 2.81326E-03  X
 2 7.80547E-02 2.88379E-03  X
 2 1.99037E-07 2.93700E-03  X
 2 9.40375E-06 3.10400E-03  X
 2 1.85924E-04 3.59600E-03  X

I need to copy information from the second file into the first file. At the end of each line that lists the nuclide (ex: Ac-223 and Ac-224) is a number (ex: 480 or 682). I need to copy that number from file 2 and insert it into file 1 after the 4th column (1, 482, 1165, etc) in it's own separate column. I need to do that for each line in file 1.

Additionally, I have a third file that I need to extract information from and place in file 1. Here is an example of file 3:

Code:
Ac-226        112
0.00000 1.486E+00
0.00010 1.486E+00
0.00014 1.486E+00
0.00015 1.486E+00
0.00016 1.486E+00
0.00018 1.486E+00
0.00020 1.486E+00
          [.....]
Ac-227         73
0.00000 7.974E+01
0.00010 7.925E+01
0.00011 7.921E+01
0.00012 7.916E+01
0.00013 7.911E+01

I need to copy the number at the end of the line with the nuclide in it and paste it after the 5th column in the index file (0, 0, 0, 1). But this is only pasted in if there is 'B-' in the third column, otherwise I need it to list 0.

So, here's an example of what I would want it to look like, I have bolded the added information:

Code:
Ac-223     2.10m A             1    480      0      0      0     0 Fr-219    358 9.9000E-01             0        0.0             0        0.0             0        0.0 6.6720 0.02536 0.01903   7 118   0  297  29 223.019136 3.739E-183.739E-18
Ac-224     2.78h ECA         482    682      0      0      0     0 Ra-224    820 9.0900E-01 Fr-220    359 9.1000E-02             0        0.0             0        0.0 0.5662 0.04901 0.23248  15 211   0  398  29 224.021722 2.304E-172.304E-17
Ac-225     10.0d A          1165    778      0      0      0     0 Fr-221    360 1.0000E+00             0        0.0             0        0.0             0        0.0 5.8919 0.02476 0.01708   7 157   0  526  44 225.023229 6.777E-186.777E-18
Ac-226    29.37h B-ECA      1944    260      1    112      0     0 Th-226   1108 8.3000E-01 Ra-226    822 1.7000E-01 Fr-222    361 6.0000E-05             0        0.0 0.0003 0.29143 0.13271  14 140   5   99   1 226.026097 1.048E-171.048E-17

I hope this all makes sense! Thank you so much!
# 2  
Old 07-13-2011
See if this works for you:
Code:
#!/usr/bin/ksh
mPTag=""
egrep '^Ac-' mFile2 > mFile2.tag
egrep '^Ac-' mFile3 > mFile3.tag
sort mFile1 mFile2.tag mFile3.tag | while read mTag mFld1 mFld2 mFld3 mFld4 mRest; do
  if [[ "${mTag}" != "${mPTag}" ]]; then
    if [[ "${mPTag}" != "" ]]; then
      mOutLine=${mPTag}' '${mPFld1}' '${mPFld2}' '${mPFld3}' '${mValue2}' '${mPFld4}' '${mValue1}' '${mPRest}
      echo ${mOutLine}
    fi
    mValue1=0
    mValue2=0
  fi
  if [[ "${mFld2}" = "" ]]; then
    mValue1=${mFld1}
  else
    if [[ "${mFld3}" = "" ]]; then
      mValue2=${mFld2}
    else
      mPFld1=${mFld1}
      mPFld2=${mFld2}
      mPFld3=${mFld3}
      mPFld4=${mFld4}
      mPRest=${mRest}
    fi
  fi
  mPTag=${mTag}
done
if [[ "${mPTag}" != "" ]]; then
  mOutLine=${mPTag}' '${mPFld1}' '${mPFld2}' '${mPFld3}' '${mValue2}' '${mPFld4}' '${mValue1}' '${mPRest}
  echo ${mOutLine}
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script for copying text file to Excel Sheet

Hi, I want to write a program to copy a log file to Excel sheet. Excel sheet has four columns MethodName , Code , Description, Details and Time. I want to pick these info from text file and put it in excel sheet. here is how the text file looks - 04.17.2014 08:06:12,697... (1 Reply)
Discussion started by: hershey
1 Replies

2. Shell Programming and Scripting

Copying a string from a file using shell script

Hello everyone I am completely new to shell scripting in linux. I wan to write a script to search for a certain string from a .txt file and copy the string which apears just after tat searched string. Eg: in a file- try.txt , we have a line saying: "roses are red, so what do i do" I... (4 Replies)
Discussion started by: Kishore920
4 Replies

3. Shell Programming and Scripting

Shell :copying the content from one file to another

I have a log containing the below lines. file1.log ----------- module: module1 module10 module2 module002 module9 moduleRT100.2.1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... (1 Reply)
Discussion started by: giridhar276
1 Replies

4. Shell Programming and Scripting

Shell : copying from one file to another file

vat.log -------- null null statistics null null null Storage Response Time disk space1 disk space2 I have a log as mentioned above in which I need to copy only the below content to another file using shell scripting. In between null statistics and final null it also includes... (1 Reply)
Discussion started by: giridhar276
1 Replies

5. Shell Programming and Scripting

copying file information using awk & grep

Hi, TASK 1: I have been using this code to print the information of files kept at "/castor/cern.ch/user/s/sudha/forPooja" in some text file name FILE.txt. rfdir /castor/cern.ch/user/s/sudha/forPooja | grep data | awk '{print "rfio:///castor/cern.ch/user/s/sudha/forPooja/"$9}' > FILE.txt ... (6 Replies)
Discussion started by: nrjrasaxena
6 Replies

6. Shell Programming and Scripting

Create shell script to extract unique information from one file to a new file.

Hi to all, I got this content/pattern from file http.log.20110808.gz mail1 httpd: Account Notice: close igchung@abc.com 2011/8/7 7:37:36 0:00:03 0 0 1 mail1 httpd: Account Information: login sastria9@abc.com proxy sid=gFp4DLm5HnU mail1 httpd: Account Notice: close sastria9@abc.com... (16 Replies)
Discussion started by: Mr_47
16 Replies

7. Shell Programming and Scripting

shell script to sort information in one file

Hi to all, anyway to create shell script to sort informations from one file and create new file with the sorted values? from file 30days.out -bash-3.00# more 30days.out user/str4@kl.com/INBOX user/tg1@johor.com/INBOX user/tg2@kedah.com/INBOX user/tg3@titangroup.com/INBOX... (3 Replies)
Discussion started by: Mr_47
3 Replies

8. UNIX for Dummies Questions & Answers

Help with searching for a file in a directory and copying the contents of that file in a new file

Hi guys, I am a newbie here :wall: I need a script that can search for a file in a directory and copy the contents of that file in a new file. Please help me. :confused: Thanks in advance~ (6 Replies)
Discussion started by: zel2zel
6 Replies

9. Shell Programming and Scripting

sed command for copying the contents of other file replacing it another file on specifc pattern

We have 2 file XML files - FILE1.XML and FILE2.xml - we need copy the contents of FILE1.XML and replace in FILE2.xml pattern "<assignedAttributeList></assignedAttributeList>" FILE1.XML 1. <itemList> 2. <item type="Manufactured"> 3. <resourceCode>431048</resourceCode> 4. ... (0 Replies)
Discussion started by: balrajg
0 Replies

10. Filesystems, Disks and Memory

Strange difference in file size when copying LARGE file..

Hi, Im trying to take a database backup. one of the files is 26 GB. I am using cp -pr to create a backup copy of the database. after the copying is complete, if i do du -hrs on the folders i saw a difference of 2GB. The weird fact is that the BACKUP folder was 2 GB more than the original one! ... (1 Reply)
Discussion started by: 0ktalmagik
1 Replies
Login or Register to Ask a Question