Shell Script to replace specific columns on matched lines
Hi,
I have a data file that has many lines in it, that is space delimited. What I want to do is match lines in the file that have the word host in it and then i want to replace columns 3-6 with data that I have in another file. The other file only has the information that belongs on those specific lines. Any ideas? |
Quote:
There is a link at the bottom of this post to a bash scripting tutorial...that's a good place to start. If you have specific problems/questions, please post them (along with samples of the input/output data), and we will be glad to help. |
Well yeah I know write a script. I'm having trouble matching the lines and I'm having trouble replacing the columns. I am fairly new at this. I'm thinking awk or sed but I just don't know the right syntax to get it all working.
|
Quote:
Again, post samples of the data and what you've tried/written so far, and we'll be glad to assist. We need to see how both sets of input data look now, and what you want when you're done, and what you've written so far. |
Ok here is an example of the data in file1 which has the new file size/date time information that I need in file2
file1: 22696 Jan 31 11:04 2013 23035 Jan 31 10:28 2013 22694 Jan 31 11:04 2013 23033 Jan 31 10:28 2013 etc....etc... repeats many times with different file sizes and times the other file that i need to merge that data in to looks like this file2 rwxrwxrwx 0/0 0 Dec 17 15:25 2002 ./ rwxrwxrwx 0/0 0 Dec 17 15:37 2002 ./blah/ rwxrwxr-x 0/3 0 Dec 17 15:37 2002 ./blah/home/ rwxrwxr-x 8654/8655 0 Dec 17 15:53 2002 ./blah/home/foo/ rw-rw-rw- 0/3 889 Jul 29 14:14 2002 ./blah/home/foo/hostlist rwxrwxrwx 0/3 0 Dec 16 16:02 2002 ./comp0/ rwxrwxrwx 0/3 0 Dec 17 10:23 2002 ./comp0/etc/ rw-rw-rw- 0/3 477 Jul 29 14:14 2002 ./comp0/etc/tab r--r--r-- 0/3 7329 Jul 29 14:14 2002 ./comp0/etc/bootinfo rwxrwxrwx 0/3 0 Mar 26 11:07 2003 ./comp0/opt/ rwxrwxrwx 0/3 0 Mar 26 10:45 2003 ./comp0/opt/RARED/ rw-rw-r-- 0/0 133120 Dec 6 16:05 2002 ./comp0/opt/RARED/configs.tar rwxrwxrwx 0/0 0 Dec 16 17:01 2002 ./comp1/ rwxrwxrwx 0/0 0 Dec 16 17:01 2002 ./comp1/etc/ r--r--r-- 0/0 5797 Dec 10 10:24 2002 ./comp1/etc/bootinfo r--r--r-- 0/3 22483 Dec 16 15:25 2002 ./comp1/etc/hosts Again a snippet it continues much longer than this. I need to replace columns 3-6 with the data from above on the lines that match */etc/hosts. The data above lines up with the lines in the replacement file. Not sure if that helps understand what I'm doing. I really don't know awk or sed but thought they might be of use here somehow. |
Quote:
Are they discarded? Are they written to the output file, unchanged? What happens if there are more lines in File1 which contain the word host than there are lines in File2? Daniel B. Martin |
Quote:
|
I did a sanity check and the number of lines that need to be fixed is 62 and the number of lines that I have data for is also 62.
|
Quote:
It will be helpful if you identify your two input files with names, even if the names are arbitrary such as File1 and File2. It will be helpful if you give samples of your input files which contain both match and no-match cases. It will be helpful if you give a sample of the desired output file which corresponds to the sample input files. It gives us a standard of comparison -- if we write a code snippet, we can judge its correctness. Daniel B. Martin |
Quote:
rwxrwxrwx 0/0 0 Dec 17 15:25 2002 ./ rwxrwxrwx 0/0 0 Dec 17 15:37 2002 ./blah/ rwxrwxr-x 0/3 0 Dec 17 15:37 2002 ./blah/home/ rwxrwxr-x 8654/8655 0 Dec 17 15:53 2002 ./blah/home/foo/ rw-rw-rw- 0/3 889 Jul 29 14:14 2002 ./blah/home/foo/hostlist rwxrwxrwx 0/3 0 Dec 16 16:02 2002 ./comp0/ rwxrwxrwx 0/3 0 Dec 17 10:23 2002 ./comp0/etc/ rw-rw-rw- 0/3 477 Jul 29 14:14 2002 ./comp0/etc/tab r--r--r-- 0/3 7329 Jul 29 14:14 2002 ./comp0/etc/bootinfo rwxrwxrwx 0/3 0 Mar 26 11:07 2003 ./comp0/opt/ rwxrwxrwx 0/3 0 Mar 26 10:45 2003 ./comp0/opt/RARED/ rw-rw-r-- 0/0 133120 Dec 6 16:05 2002 ./comp0/opt/RARED/configs.tar rwxrwxrwx 0/0 0 Dec 16 17:01 2002 ./comp1/ rwxrwxrwx 0/0 0 Dec 16 17:01 2002 ./comp1/etc/ r--r--r-- 0/0 5797 Dec 10 10:24 2002 ./comp1/etc/bootinfo r--r--r-- 0/3 22696 Jan 31 11:04 2013 ./comp1/etc/hosts |
So far I have come up with these commands that will give me the file2 and also find the lines in file1 that need to be changed
ls -Rl | grep hosts | cut -d " " -f 5-8 > file2 sed -n '/hosts/p' file1 > linestobechanged |
Interesting problem replace each match with next item from list.
|
Quote:
It isn't in either input file. Daniel B. Martin |
It belongs in file1 I forgot to add that column. Sorry
|
I changed the input files to make a better test.
File1 ... Code:
22696 Jan 31 11:04 2013 Code:
rwxrwxrwx 0/0 0 Dec 17 15:25 2002 ./ Code:
awk 'BEGIN {while (getline < "'"$InFile1"'") a[++j]=$0}; Code:
rwxrwxrwx 0/0 0 Dec 17 15:25 2002 ./ |
Thanks I think that will work. Can you please change the 3rd line of file2 and the third line of your output to blah/home the other information I meant not to include.
|
Quote:
Code:
rwxrwxrwx 0/0 0 Dec 17 15:25 2002 ./ Code:
rwxrwxrwx 0/0 0 Dec 17 15:25 2002 ./ |
I meant on your other post from page1 just edit and change thanks and sorry again.
|
I "polished" the code to make it more concise.
Code:
awk 'BEGIN {while (getline < "'"$InFile1"'") a[++j]=$0}; |
All times are GMT -5. The time now is 07:26 AM. |