Help with a shell script to modify one line and copy the next 9 to same file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with a shell script to modify one line and copy the next 9 to same file
# 1  
Old 10-29-2009
Help with a shell script to modify one line and copy the next 9 to same file

Hi everyone,

the problem is quite simple, yet I can't find an easy solution using awk.

I need to search for a string in $3, then if I find this string, copy the line,modify $3, and copy the next 9 lines to the same file.

My problem is in the copying of the lines... Finding and modifying is alright (I read the string in a second file and substitute it by the corresponding string).

If you could give me an algorithm that I could write in awk, that would be great.

Thank you very much.

Teroc.
# 2  
Old 10-30-2009
Provide us with the Input file and the output your expecting, so it would be easy to work on.
# 3  
Old 10-30-2009
see here for example.
# 4  
Old 10-30-2009
I doesn't work GhostDog, well it probably does, but not in my case.
I did something like that:

Code:
NR==FNR {a[$2]=$1;next} #I read a first file to get the search pattern
{if ($3 in a)
    {$3=a[$3]
    print
    d=9
    next}
    d--&&d>=0
}

But it does only print the first line.

Here is an example of my files.
The first file where I read the values looks like this:
Code:
a1 b1
a2 b2
a3 b3

Then the second file looks like: (there is also other stuff in the file not related)
Code:
....
/begin MEASUREMENT b1
   "comment" 
   UWORD 
   ident 
   1 
   100
   0 
   65535 
   ECU_ADDRESS 0x4000582C
  /end MEASUREMENT 
....

I want to check if $3==b1, then replace it with a1 and copy the others lines at the end of the file, and do it for each bi of the first file.

thanks for your help.

---------- Post updated at 08:53 AM ---------- Previous update was at 08:42 AM ----------

Found a solution doing this:

Code:
NR==FNR {a[$2]=$1;next}
{if ($3 in a)
    {$3=a[$3]
    print
    d=9
    while (d>0)
    {getline; print; d--}
    }
}

# 5  
Old 10-30-2009
Quote:
Originally Posted by Teroc
I doesn't work GhostDog, well it probably does, but not in my case.
of course. Its just a similar example and it will NOT solve your problem exactly. The rest is up to you to understand, change and modify. Anyway, its good that you already found your solution. Lastly, provide examples of your input, and describe your desired output next time when you post a problem.
# 6  
Old 10-30-2009
I have another problem related to those files.

Sometimes, in file1, it is possible that one value in column 1 is associated to 2 or more values like this:
Code:
a1 b1 c1
a2 b2
a3 b3 c3 d4

Now, when I need to replace and copy as described in my other post, I need to do this for each value associated with a1.
The problem is, I tried with multidimensional arrays and other stuffs, but it doesn't write the full 9 lines for the 2nd value (c1 or c3).

Is there a way to jump back 10 lines up and retest the same line so I can process it again for the 2nd and 3rd value? I've searched since last post and can't find somethinbg working...

Thanks again and again !

Here is the code I use ...
Code:
NR==FNR {for(i=2;i<=NF;i+=1) a[$1]=$i" "a[$1];next}
NR!=FNR {print $0}

{if ($3 in a && $2=="MEASUREMENT")
	{temp=$3;
	split(a[$3],separate)
	for (x in separate)
	{$3=separate[x];
	buffer[j]=$0;
	$3=temp;
	d=9;
	while (d>0) #not printing for the second value of separate because of this while I guess, once it is done once, it wont do it again for the other values
	{getline; buffer[j]=buffer[j]"\n"$0; d--}  #I use a buffer so that I can write the thing where I want
	}j++
	}
}

And here is the ouput I have

Code:
...
/begin MEASUREMENT b1  #As intended
   "comment" 
   SWORD 
   ratio_0p1 
   1 
   100
   -2000.0 
   2000.0 
   ECU_ADDRESS 0x500040B4
  /end MEASUREMENT 
/end MEASUREMENT c1 #writes c1 here instead of the whole thing


Last edited by Teroc; 10-30-2009 at 08:56 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Make a copy of a file when someone tries to modify the file

I wish to take a backup of the file when someone tries to modify a file say /tmp/test.txt using vi, vim, cat, copy, mv commands. I'm aware of inotify which will notify me of any changes to the file however, I wish to make a backup before and not after the changes are made to the file. I... (1 Reply)
Discussion started by: mohtashims
1 Replies

2. UNIX for Dummies Questions & Answers

Copy and modify a file if the original has changed

Hi there, I have built up my own little "cloud" for my family as the amount of computers grows all day. By now we use 3 smartphones, 2 notebooks und 4 PCs, so this "home cloud" was made to store all personal data (photos, documents,...) and do a backup once in a while. It is running on a Ubuntu... (1 Reply)
Discussion started by: SebSnake
1 Replies

3. Shell Programming and Scripting

Shell script to modify file in several directories

Hi, I want a script shell to automate modifying httpd.conf file for several instances of apache, save httpd.file before changing it, after modifying it and then restart apache. - Replace ServerRoot "xxxx" by ServerRoot "yyyy" of all directories : "... (4 Replies)
Discussion started by: bras39
4 Replies

4. Shell Programming and Scripting

Modify text file using shell script

Hi, I have a text file which is following format - COL VAL ABC 1 ABC 2 ABC 3 ABC 4 ABC 5 My requirement is to search for a particular value (provided by user) in the file and comment the previous entries including that as well. E.g. If I search for number 3, then the output... (6 Replies)
Discussion started by: bhupinder08
6 Replies

5. Shell Programming and Scripting

Read data from .csv file through shell script & modify

I need to read data from a file called "test.csv" through shell script where the file contains values like name,price,descriptor etc. There are rows where descriptor (& in some rows name) are written as string & other characters like "car_+" OR "bike*" etc where it should contains strings like... (3 Replies)
Discussion started by: raj100
3 Replies

6. Shell Programming and Scripting

Shell script to copy file

Dear all, I have a database with thousands of files with the structure of name is: Filename_hour_year.abc Filename_hour_year_1.abc .............. So what I need is how to write a script that all file with contain the character "_1" will copy to "_2" For example: file name:... (7 Replies)
Discussion started by: hainguyen1402
7 Replies

7. Shell Programming and Scripting

Shell Script - Copy File at intervals

Hi, I want to copy some files from a Folder say, /usr/X at random intervals to another location. Basically, new files will be dumped at random intervals to location /usr/X and I have to copy those new files to some other location (after copying, I cannot delete those files from source... (2 Replies)
Discussion started by: angshuman_ag
2 Replies

8. Shell Programming and Scripting

Need help to modify perl script: Text file with line and more than 1 space

Dear Friends, I am beginner in Perl and trying to find the problem in a script. Kindly help me to modify the script. My script is not giving the output for the last field and followed text (LA: Language English). Input file & script as follows: Input file: Thu Mar 19 2:34:14 EDT 2009 STC... (3 Replies)
Discussion started by: srsahu75
3 Replies

9. Shell Programming and Scripting

How to copy and paste line in shell script

Hi I want to grep for a line and copy and paste that line. for Example ---- file abc.txt ---- host=atlx1 sid=atld1 mail=abc@abc.com host=atlx2 sid=atld2 mail=xyz@abc.com host=atlx3 sid=atld3 mail=def@abc.com host=atlx4 sid=atld4 mail=mno@abc.com --- end of file abc.txt ---- Now I... (16 Replies)
Discussion started by: upsrk
16 Replies

10. Shell Programming and Scripting

how to modify a file using shell script

Hi, i am using SuonOS and ksh. i need to add data into a file(s.txt) using a shell script. i have to pass 3 parameters and these 3 paramaters should add into the file at end of the file. File s.txt is look like, --------------------------------- column1|column2|column3 ... (1 Reply)
Discussion started by: syamkp
1 Replies
Login or Register to Ask a Question