replacing one line with other


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replacing one line with other
# 1  
Old 12-10-2010
replacing one line with other

Hello

i need little help.

i have 2 files.

first that contains a lot of data and about 2000 lines that i need to replace with new ones. Lets call that line A.

i know exactly how that lines looks.

in second file i have about 2000 lines which contains the line A and line B in same line separated by ';'.

line B is the new value.

so i need a way how to go through first file and if i find value A that corresponds to value A from second file to replace it with value B from that same line.

if anyone has any idea please help.

e.g.

first file

ADSE 333 333 333 4.5.6 333.3

second file

ADSE 333 333 333 4.5.6 333.3 ;ADSE 222 333 222 4.5.6 345.1

result should be

ADSE 222 333 222 4.5.6 345.1

Last edited by zekich; 12-10-2010 at 03:24 AM..
This User Gave Thanks to zekich For This Post:
# 2  
Old 12-10-2010
Try:

Code:
grep -f file1 file2 | sed 's/^.* ;//g'

Explanation:

Code:
grep -f file1 file2             # this should display all lines of file2 which contains a part of file1
sed 's/^.* ;//g'             # this will remove characters from starting until it encounters ;(comma)

R0H0N
# 3  
Old 12-10-2010
yes but i need the line from file 1 to be exchnaged by value from file 2 after the ';' in the file 1

first file

ADSE 333 333 333 4.5.6 333.3

second file

ADSE 333 333 333 4.5.6 333.3 ;ADSE 222 333 222 4.5.6 345.1

result should be in file 1

ADSE 222 333 222 4.5.6 345.1
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing a line in a file

Hi all, I need to replace a line export TZ=xxxxxxxx with the line export TZ=$1 Now, "xxxxxxxx" in the above line is some unknown string and $1 is a parameter. I want the content of $1 to be replaced with "xxxxxxxx". Kindly help me how to do this in the shell scripting. (5 Replies)
Discussion started by: ddeeps2610
5 Replies

2. Shell Programming and Scripting

Replacing text on every third line

I have file like this "copy table_name from filea.txt on node replace delimiter '|';" "copy table_name from fileb.txt on node replace delimiter '|';" "copy table_name from filec.txt on node replace delimiter'|';" "copy table_name from filee.txt on node replace delimiter '|';" "copy... (1 Reply)
Discussion started by: nnani
1 Replies

3. Shell Programming and Scripting

Replacing line 'i' of file1 with line 'j' of file 2

Hi All, As mentioned in the title I have two text files and I would like to replace line number 5 of file #1 with line number 4 of file #2 e.g. file 1 wqwert 4.4464002 3 319 286 369 46.320002 56.150002 45.100002 1 1 1 0.723 (12 Replies)
Discussion started by: f_o_555
12 Replies

4. UNIX for Dummies Questions & Answers

Replacing line with another

Hi all, The problem i need helped with is the following: I need to read the first line in a text file and use that line to replace the third line in another text file. after an hour of searching for different solutions i try here and hope for the best:) (2 Replies)
Discussion started by: khorne
2 Replies

5. Shell Programming and Scripting

Replacing a string with new line

Hi, Can anyone help me know how to replace a string with the new line for ex: file1 val1 or val2 or val3 or I need to replace the "or" with new line. Thanks in advance (2 Replies)
Discussion started by: mr_manii
2 Replies

6. Shell Programming and Scripting

replacing line with variable

All I want to do is replace the 2nd line in a file with a variable, eg, var=xxx the file 'test' containing: aaa bbb ccc replace bbb with xxx aaa xxx ccc I had it working with sed on a redhat machine, but it doesn't work on a mac machine. (4 Replies)
Discussion started by: sideways
4 Replies

7. Shell Programming and Scripting

Replacing a pattern in a line

Hi, I need to replace an input IP address to an existing IP addresses. Source input line is as follows: -------------------- RequestURI := "sip: 123.1.1.1"; input IP = 124.10.3.4 My output should be Request URI := "sip: 124.10.3.4"; I have written the code as follows: $line =~... (1 Reply)
Discussion started by: gyana_cboy
1 Replies

8. UNIX for Dummies Questions & Answers

Sed replacing a whole line with a new line

I am needing to replace a whole line of code in a file with a new line of code. This is what I need to do. I need to replace. $db_url = 'mysql://test_dev:test12@localhost/test'; With $db_url = 'mysql://$dbuser:$dbpass@localhost/$dbase'; OK this is where it gets complicated. The... (4 Replies)
Discussion started by: filmguy
4 Replies

9. Shell Programming and Scripting

replacing certain characters with new line?

i have a text file which domains something like this 123213213213/32434342 324324324/12312321321 12321321,435435435 12321312 / 12313213 / 12435435345 4353213 , 123213213213 21321321312-12334324 234324324 - 235645645645 456456456 - 45456456456 - 45645645654243 how can i replace '/' and... (4 Replies)
Discussion started by: Bashar
4 Replies

10. Shell Programming and Scripting

Replacing the last field of a line.

Hi, I wrote a script which extracts data from 2 tables (joining the tables together) and outputs the fields to a csv file. the output may look something like scenario 1: a,b,c,d,1,2,3,4 or scenario 2: a,b,c,d,,,, now, in the second scenario, there are some empty fields at the end of... (3 Replies)
Discussion started by: Darek
3 Replies
Login or Register to Ask a Question