Replace text in columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace text in columns
# 1  
Old 07-24-2016
Replace text in columns

I have two files in below formats:

Code:
cat file1
abc|abcd|10|1020
10|xyz|1010|1020
abc|abcd|10|1020
10|xyz|1010|1020

cat file2
abc|abcd|10|1020
11|xyz|1010|1020
abc|abcd|12|1020
10|xyz|1011|1020
abc|abcd|11|1020
10|xyz|1010|1020
abc|abcd|10|1020
10|xyz|1010|1020

I am generating a html file using the second table. I need to change the font colour of those lines in second file which are in file1. Although i can match lines and replace with sed, i need to have a html tag wrapped with every column (delimiter is "|") . My expected output is ,
Code:
cat file2
<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>

I tried to replace by using IFS value as "|" but then it replaced everywhere in file.
# 2  
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:
# 3  
Old 07-24-2016
Here is a slightly different way to do it (which copies the results back into the 2nd input file if awk successfully processes the files). It should run slightly faster for files that contain duplicated lines in file1 (as in your example) and for files where lines found in file1 appear multiple times in file2 (as in your example):
Code:
#!/bin/ksh
trap 'rm -rf tmp.$$' EXIT	# Remove temp file when done.

awk '	# Start awk script.
BEGIN {	# Set input and output field separators.
	FS = OFS = "|"

	# Set HTML start and end tag strings.
	html_start = "<p><span style=\"color: #ff0000;\">"
	html_end = "</span></p>"
}
NR == FNR && !($0 in data) {
	# For lines in the 1st input file that have not already been processed,
	# set elements of the data[] array (indexed by the entire input line
	# contents) to the input line with HTML tags added to each field.
	data[$0] = html_start $1 html_end
	for(i = 2; i <= NF; i++)
		data[$0] = data[$0] OFS html_start $i html_end
}
NR == FNR {
	# Skip remaining step in this awk script for lines in this file.
	next
}
{	# For lines in the 2nd input file, print the line with HTML tags if
	# there is an entry in data[] for this line; otherwise, just print the
	# current input line.
	print ($0 in data) ? data[$0] : $0
}
# End awk script, specify files to be processed, and if successful, replace the
# 2nd input file with the outupt from the awk script.
' file1 file2 > tmp.$$ && cp tmp.$$ file2

This was written and tested using a Korn shell, but should work with any shell that is based on Bourne shell syntax. (It will not work with shells that use csh syntax.)

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.

Last edited by Don Cragun; 07-24-2016 at 03:01 AM.. Reason: Fix typo: s/(/)/
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 07-24-2016
Thanks Ravinder and Don, both solutions worked perfect. All are equally good. Thanks once again for your quick response.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question