Replace specific column range in a non-delimited file with a string!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace specific column range in a non-delimited file with a string!
# 1  
Old 08-05-2013
Replace specific column range in a non-delimited file with a string!

Hi All, I will need an help with respect to replacing a range of columns on a non-delimited file using a particular string pattern.

Say file input is
Code:
MYNUMBERD000000-BAN CHUE INSNTS       **    N+
MYAREDSDD000000+BAN CHUE INSNTS       **    N+
MYDERFFFSD00000-GIR PENT - ACH            **    N+
MYDDDDDAY000000+GIR PENT - ACH            **    N+

I have to replace the part from "+" or "-" to "**" with a string say "TEST".
I am specifically unable to traverse to 16th column and then replace from column17 to column38 with the string using sed.

So output should be as below :
Code:
MYNUMBERD000000-TEST**    N+
MYAREDSDD000000+TEST**    N+
MYDERFFFSD00000-TEST**    N+
MYDDDDDAY000000+TEST**    N+

Thanks for any help on this.

Last edited by Scott; 08-05-2013 at 07:27 AM.. Reason: Code tags
# 2  
Old 08-05-2013
After 35 post here, you should now how to use code tags
Code:
awk '{gsub(/-.+\*\*/,"-TEST**");gsub(/\+.+\*\*/,"+TEST**")}1' file
MYNUMBERD000000-TEST**    N+
MYAREDSDD000000+TEST**    N+
MYDERFFFSD00000-TEST**    N+
MYDDDDDAY000000+TEST**    N+

# 3  
Old 08-05-2013
Thanks a ton Jotne for the reply, but its giving quite a few error :
Code:
awk '{gsub(/-.+\*\*/,"-TEST**");gsub(/\+.+\*\*/,"+TEST**")}1' file.txt
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1

I am using sun-os 5.1 for executing this script.
Sorry for missing the code tags.
# 4  
Old 08-05-2013
Try to use /usr/xpg4/bin/awk , /usr/xpg6/bin/awk, or nawk instead of awk
# 5  
Old 08-12-2013
Thank you Jotne, it was getting executed via nawk.
I have a question though, can't we use
Code:
sed

to replace on
Code:
fixed column numbers, say colomn 21-40 to be replaced with some string(TEST)

.

As in future instead of +, - anyother symbol may come in, but the column lengths are always fixed.

I tried to google out the sed option to replace based on column number but couldn't fine one.

Thank you
# 6  
Old 08-12-2013
Code:
sed 's/\([+-]\).*\(\*\*\)/\1TEST\2/' input_file

--ahamed

---------- Post updated at 06:09 AM ---------- Previous update was at 05:52 AM ----------

Using awk for specific columns

Code:
awk '{val=substr($0,21,19);gsub(val,"TEST",$0)}1' input_file

Note : 19 characters from position 21 which will cover from 21 to 40. So, you need to change it accordingly

HTH

--ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace delimiter for a particular column in a pipe delimited file

I have an input file as below Emp1|FirstName|MiddleName|LastName|Address|Pincode|PhoneNumber 1234|FirstName1|MiddleName2|LastName3| Add1 || ADD2|123|000000000 Output : 1234|FirstName1|MiddleName2|LastName3| Add1 ,, ADD2|123|000000000 OR 1234,FirstName1,MiddleName2,LastName3, Add1 ||... (2 Replies)
Discussion started by: styris
2 Replies

2. UNIX for Beginners Questions & Answers

Replace a column in tab delimited file with column in other tab delimited file,based on match

Hello Everyone.. I want to replace the retail col from FileI with cstp1 col from FileP if the strpno matches in both files FileP.txt ... (2 Replies)
Discussion started by: YogeshG
2 Replies

3. Shell Programming and Scripting

Replace pipe delimited column string to null

Hi All, I have a large dat file where each lines are pipe delimited values. I need to parse the file depending on the request. For example: sometimes I have told to remove all the values in the 7th column (this case remove values '3333' only from the first line and '3543' from the second line)... (4 Replies)
Discussion started by: express14
4 Replies

4. UNIX for Dummies Questions & Answers

Search for a specific String in a log file for a specific date range

Hi, I have log file which rolls out every second which is as this. HttpGenRequest - -<!--OXi dbPublish--> <created="2014-03-24 23:45:37" lastMsgId="" requestTime="0.0333"> <response request="getOutcomeDetails" code="114" message="Request found no matching data" debug="" provider="undefined"/>... (3 Replies)
Discussion started by: karthikprakash
3 Replies

5. Shell Programming and Scripting

Replace column that matches specific pattern, with column data from another file

Can anyone please help with this? I have 2 files as given below. If 2nd column of file1 has pattern foo1@a, find the matching 1st column in file2 & replace 2nd column of file1 with file2's value. file1 abc_1 foo1@a .... abc_1 soo2@a ... def_2 soo2@a .... def_2 foo1@a ........ (7 Replies)
Discussion started by: prashali
7 Replies

6. UNIX for Dummies Questions & Answers

how to print the column that contains a string in delimited file in unix

Hi Team, I have a requirement to print the column or find the column number of a particular string in delimited (;) file. Ex: aaa;bbb;ccc;rrr;mmm; gggg;rrr;mmmm;ssss;eee;aaa; ccc;gggg;tttt;bbbb;dddd;ggggg;rrr; my file contains 1000+ rows like above example with semicolon... (3 Replies)
Discussion started by: baskivs
3 Replies

7. UNIX for Dummies Questions & Answers

Adding tags to a specific column of a space delimited text file

I have a space delimited text file with two columns. I would like to add NA to the first column of the text file. Input: 19625 10.4791768259 19700 10.8146489183 19701 10.9084026759 19702 10.9861346978 19703 10.9304364984 Output: NA19625 10.4791768259 NA19700 10.8146489183... (1 Reply)
Discussion started by: evelibertine
1 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. UNIX for Dummies Questions & Answers

Search and replace string only in a particular column in a delimited file

I have file with multiple columns. Column values for a record may be same. Now i have to replace a column value(this can be same for the other columns) with new value. File.txt A,B,C,D,A,B,C,D,A,B,C,D A,B,C,D,A,B,C,D,A,B,C,D A,B,C,D,A,B,C,D,A,B,C,D A,B,C,D,A,B,C,D,A,B,C,D... (1 Reply)
Discussion started by: ksailesh
1 Replies

10. Shell Programming and Scripting

Replace string in a file within a range of line

Hi, I want to replace the srting '; with ABCD'; in a file from line 1 to line 65. Is there any single command to do it without using awk Thanks for quick reply https://www.unix.com/images/misc/progress.gif (3 Replies)
Discussion started by: tosattam
3 Replies
Login or Register to Ask a Question