Sponsored Content
Full Discussion: Replace text in columns
Top Forums Shell Programming and Scripting Replace text in columns Post 302977938 by RavinderSingh13 on Sunday 24th of July 2016 01:32:27 AM
Old 07-24-2016
Hello ctrld,

Could you please try following and let me know if this helps you.
Code:
awk -vs1="<p><span style=\"color: #ff0000;\">"  -vs2="</span></p>" -F"|" 'FNR==NR{A[$0]=$0;next} ($0 in A){num=split(A[$0], B,"|");for(i=1;i<=num;i++){Q=Q?Q OFS s1 B[i] s2:s1 B[i] s2};print Q;Q=""} !($0 in A){print}' OFS="|"   Input_file1   Input_file2

Output will be as follows.
Code:
<p><span style="color: #ff0000;">abc</span></p>|<p><span style="color: #ff0000;">abcd</span></p>|<p><span style="color: #ff0000;">10</span></p>|<p><span style="color: #ff0000;">1020</span></p>
11|xyz|1010|1020
abc|abcd|12|1020
10|xyz|1011|1020
abc|abcd|11|1020
<p><span style="color: #ff0000;">10</span></p>|<p><span style="color: #ff0000;">xyz</span></p>|<p><span style="color: #ff0000;">1010</span></p>|<p><span style="color: #ff0000;">1020</span></p>
<p><span style="color: #ff0000;">abc</span></p>|<p><span style="color: #ff0000;">abcd</span></p>|<p><span style="color: #ff0000;">10</span></p>|<p><span style="color: #ff0000;">1020</span></p>
<p><span style="color: #ff0000;">10</span></p>|<p><span style="color: #ff0000;">xyz</span></p>|<p><span style="color: #ff0000;">1010</span></p>|<p><span style="color: #ff0000;">1020</span></p>

EDIT: Adding a non-one liner form of soution on same now.
Code:
awk -vs1="<p><span style=\"color: #ff0000;\">"  -vs2="</span></p>" -F"|" 'FNR==NR{
					                                           A[$0]=$0;
										   next
                                                                                 } 
                                                                         ($0 in A){
										   num=split(A[$0], B,"|");
								                   for(i=1;i<=num;i++){
													Q=Q?Q OFS s1 B[i] s2:s1 B[i] s2
												      };
										   print Q;
										   Q=""
									          } 
									!($0 in A){
										   print
									          }
								        ' OFS="|"  Input_file1   Input_file2

Thanks,
R. Singh

Last edited by RavinderSingh13; 07-24-2016 at 02:37 AM.. Reason: Adding a non-one liner form of solution now.
This User Gave Thanks to RavinderSingh13 For This Post:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

search and replace a specific text in text file?

I have a text file with following content (3 lines) filename : output.txt first line:12/12/2008 second line:12/12/2008 third line:Y I would like to know how we can replace 'Y' with 'N' in the 3rd line keeping 1st and 2nd lines same as what it was before. I tried using cat output.txt... (4 Replies)
Discussion started by: santosham
4 Replies

2. UNIX for Dummies Questions & Answers

Replace columns from File1 with columns from File2

Hi all, I would like to replace some columns from file1 with columns from file2. Currently, I'm able to do it with the following command: awk 'NR==FNR{a=$1;b=$2;c=$3;next;} {$2=a;$4=b;$5=c;print}' file2 file1 > temp mv -f temp file1 First, i make the changes and save it as a temp... (1 Reply)
Discussion started by: seijihiko
1 Replies

3. UNIX for Dummies Questions & Answers

How to convert text to columns in tab delimited text file

Hello Gurus, I have a text file containing nearly 12,000 tab delimited characters with 4000 rows. If the file size is small, excel can convert the text into coloumns. However, the file that I have is very big. Can some body help me in solving this problem? The input file example, ... (6 Replies)
Discussion started by: Unilearn
6 Replies

4. Shell Programming and Scripting

Replace specific columns in one file with columns in another file

HELLO! This is my first post here! By the way, I think it is great that people do this. My question: I have two files, one is a .dilm and one is a .txt. It is my understanding that the .dilm file can be treated as a .txt file. I wrote another program where I was able to manipulate it as if it... (3 Replies)
Discussion started by: mehdib
3 Replies

5. Shell Programming and Scripting

Find and add/replace text in text files

Hi. I would like to have experts help on below action. I have text files in which page nubmers exists in form like PAGE : 1 PAGE : 2 PAGE : 3 and so on there is other text too. I would like to know is it possible to check the last occurance of Page... (6 Replies)
Discussion started by: lodhi1978
6 Replies

6. Shell Programming and Scripting

Replace columns of one file into another

Hi, I have two files that are different in size (column #'s differ). Each file has the exact same 3 starting columns. File 1 has 240 columns while File 2 has 45 columns. So if the first 3 columns are the same, I want to replace columns 83 to 163 from File 1 with columns 18 to 33 from File... (7 Replies)
Discussion started by: kylle345
7 Replies

7. UNIX for Dummies Questions & Answers

Removing columns from a text file that do not have any values in second and third columns

I have a text file that has three columns. But at the end of the text file, there are trailing lines that have missing second and third columns: 4 0.04972604 KLHL28 4 0.0497332 CSTB 4 0.04979822 AIF1 4 0.04983331 DECR2 4 0.04990344 KATNB1 4 4 4 4 How can I remove the trailing... (3 Replies)
Discussion started by: evelibertine
3 Replies

8. Shell Programming and Scripting

Replace text in column1 of a file matching columns of another file

Hi all, I have 2 files: species-names.txt Abaca-bunchy-top-virus ((((Abaca-bunchy-top-virus((Babuvirus((Unassigned((Nanoviridae((Unassigned)))) Abutilon-mosaic-virus ((((Abutilon-mosaic-virus((Begomovirus((Unassigned((Geminiviridae((Unassigned))))... (2 Replies)
Discussion started by: thienxho
2 Replies

9. Shell Programming and Scripting

Match on columns and replace other columns

Hi Friends, I have the following input file cat input chr1 100 200 0.1 0.2 na 1 na nd chr1 105 200 0.1 0.2 1 1 na 98 chr1 110 290 nf 1 na nd na 1 chr2 130 150 12 3 na 1 na 1 chr3 450 600 nf nf na 10 na nd chr4 300 330 1 1 10 11 23 34 My requirement is 1. If $6 is na make $7 nd and... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

10. Shell Programming and Scripting

How to concatenate 2-columns by 2 -columns for a text file?

Hello, I want to concatenate 2-columns by 2-columns separated by colon. How can I do so? For example, I have a text file containing 6 columns separated by tab. I want to concatenate column 1 and 2; column 3 and 4; column 5 and 6, respectively, and put a colon in between. input file: 1 0 0 1... (10 Replies)
Discussion started by: huiyee1
10 Replies
All times are GMT -4. The time now is 01:16 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy