awk to change specific string to new value if found in text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to change specific string to new value if found in text file
# 1  
Old 03-06-2017
awk to change specific string to new value if found in text file

I am trying to use awk to change a specific string in a field, if it is found, to another value. In the tab-delimited file the text in bold in $3 contains the string 23, which is always right before a ., if it is present.

I am trying to change that string to X, keeping the formatting and the name of the file the same. The awk below seems to change the formatting and may not be the best way. Thank you Smilie.

file
Code:
Input Variant	Errors	Chromosomal Variant	Coding Variant(s)
NM_004992.3:c.274G>T		NC_000023.10:g.153297761C>A	XM_005274683.1:c.-6G>T	XM_005274682.1:c.-6G>T	XM_005274681.1:c.274G>T	LRG_764t2:c.274G>T	NM_004992.3:c.274G>T	LRG_764t1:c.310G>T	NM_001110792.1:c.310G>T

awk
Code:
awk -F'\t' '{ $3 = ($3 == "23" ? X : $3) } 1' OFS="\t" file

desired output
Code:
Input Variant	Errors	Chromosomal Variant	Coding Variant(s)
NM_004992.3:c.274G>T		NC_0000X.10:g.153297761C>A	XM_005274683.1:c.-6G>T	XM_005274682.1:c.-6G>T	XM_005274681.1:c.274G>T	LRG_764t2:c.274G>T	NM_004992.3:c.274G>T	LRG_764t1:c.310G>T	NM_001110792.1:c.310G>T


Last edited by cmccabe; 03-06-2017 at 09:40 AM.. Reason: fixed format
# 2  
Old 03-06-2017
== is an equality comparison, it doesn't search substrings.

Try
Code:
awk -F"\t" '{ sub(/23/, "X", $3); } 1' infile > outfile

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-06-2017
To keep from matching 23 when it does not appear immediately before a period, you might want to change that to:
Code:
awk -F"\t" '{ sub(/23[.]/, "X.", $3); } 1' infile > outfile

This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 03-07-2017
Thank you both very much Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to change a specific column and in a specific row

I am trying to change the number in bold to 2400 01,000300032,193631306,190619,0640,1,80,,2/ 02,193631306,000300032,1,190618,0640,CAD,2/ I'm not sure if sed or awk is the answer. I was going to use sed and do a character count up to that point, but that column directly before 0640 might... (8 Replies)
Discussion started by: juggernautjoee
8 Replies

2. Shell Programming and Scripting

awk script to detect specific string in a log file and count it

Hello, can someone guide me on this? I don't know what is the best approach, (awk script, shell script) I am using RedHat Linux version 6.5. There is a third party application deployed on that server. This app by default generates 5 log files and each file is 20MB. These log rollover... (5 Replies)
Discussion started by: ktisbest
5 Replies

3. Shell Programming and Scripting

awk to place specific contents filename within text file

I am trying to use awk to place the contens of a filename in $1 and $2 followed by the data in the text file. Basically, put the filename within the text file. There are over 1000 files in the directory and as of now each file is saved with a unique name but it is not within the file. Thank you... (10 Replies)
Discussion started by: cmccabe
10 Replies

4. Shell Programming and Scripting

How to read all data after a specific string from a text file ?

Hi, I have a file(input.txt) and trying to format as output.txt. See the attached file format. Note: This is a windows file (DOS format) and the commands are also going to execute on windows. Basically I am trying to capture all the data in between Local Group Memberships and Global Group... (10 Replies)
Discussion started by: Monoj2014
10 Replies

5. Windows & DOS: Issues & Discussions

Removing anything from text file except specific string

So, I have a text file that looks like this: 0,0: (168,168,176) #A8A8B0 srgb(168,168,176) 1,0: (168,168,176) #A8A8B0 srgb(168,168,176) 2,0: (166,166,174) #A6A6AE srgb(166,166,174) 3,0: (166,166,174) #A6A6AE srgb(166,166,174) 4,0: (168,168,176) #A8A8B0 srgb(168,168,176) 5,0:... (0 Replies)
Discussion started by: pasc
0 Replies

6. Shell Programming and Scripting

To add a new line with specific text after the pattern is found using sed

hi guys, im trying to add the following line in my xml file <dbrollbacksegs <oa_var="s_db_rollback_segs">NOROLLBACK</dbrollbacksegs> when ever i find the following line <dbsharedpool oa_var="s_dbsharedpool_size">300000000</dbsharedpool> I have succedded till adding a new line... (1 Reply)
Discussion started by: smarlaku
1 Replies

7. UNIX for Dummies Questions & Answers

How can I search and change an specific string in a file

Dear All, New to Linux/Unix OS, my Linux version is 2010 x86_64 x86_64 x86_64 GNU/Linux As titled, I wonder if you can help to provide a solution to find and change an specific string in a file The file include a lots of data in following configuration but might be various in... (3 Replies)
Discussion started by: axel
3 Replies

8. UNIX for Dummies Questions & Answers

Deleting lines that contain a specific string from a space delimited text file?

Hi, I have a space delimited text file that looks like the following: 250 rs10000056 0.04 0.0888 4 189321617 250 rs10000062 0.05 0.0435 4 5254744 250 rs10000064 0.02 0.2403 4 127809621 250 rs10000068 0.01 NA 250 rs1000007 0.00 0.9531 2 237752054 250 rs10000081 0.03 0.1400 4 17348363... (5 Replies)
Discussion started by: evelibertine
5 Replies

9. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

10. UNIX for Dummies Questions & Answers

Output text from 1st paragraph in file w/ a specific string through last paragraph of file w/ string

Hi, I'm trying to output all text from the first paragraph in a file that contains a specific string through the last paragraph in that file that contains that string. Previously, I was outputting just each paragraph with that search string with: cat in_file | nawk '{RS=""; FS="\n";... (2 Replies)
Discussion started by: carpenn
2 Replies
Login or Register to Ask a Question