Visit Our UNIX and Linux User Community


How to replace and remove few junk characters from a specific field?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to replace and remove few junk characters from a specific field?
# 1  
Old 09-30-2014
How to replace and remove few junk characters from a specific field?

I would like to remove all characters starting with "%" and ending with ")" in the 4th field - please help!!
Code:
1412007819.864  /device/services/heartbeatxx    204     0.547%!i(int=0)        0.434   0.112
1412007819.866  /device/services/heartbeatxx    204     0.547%!i(int=1)        0.423   0.123
1412007819.867  /device/services/heartbeatxx    204     0.547%!i(int=5)        0.450   0.096
1

note: field separator is "\t"

Last edited by vgersh99; 09-30-2014 at 04:55 PM.. Reason: code tags, please!
# 2  
Old 09-30-2014
Code:
awk -F'\t' 'match($4,"%.*[)]") {$4=substr($4,1,RSTART-1) substr($4,RSTART+RLENGTH+1)}1' OFS='\t' myFile

# 3  
Old 09-30-2014
Maybe,

Code:
awk -F"\t" 'sub(/%.*\)/, "", $4) 1'  OFS='\t' filename


Last edited by Aia; 09-30-2014 at 05:12 PM.. Reason: Correct the output field
# 4  
Old 09-30-2014
Sorry Aia and vgresh99, thanks for trying but both solution did not work - appreciate any other help.
# 5  
Old 09-30-2014
Quote:
Originally Posted by snemuk14
Sorry Aia and vgresh99, thanks for trying but both solution did not work - appreciate any other help.
Help us to help you. Could you explain how did it fail?

I'm guessing that if it did not work is because you are not using tabs as you assume, but rather spaces expanding to behave like tabs.

However, as I said before. Tell us how it did not work.
# 6  
Old 09-30-2014
On Solaris use nawk instead of awk......
# 7  
Old 09-30-2014
Ok. I stored the first line in a file named x to test and tried it on a linux machine as below:
Code:
$ awk -F"\t" 'sub(/%.*\)/, "", $4) 1'  OFS='\t' x

Output shows the same :
Code:
1412007819.864  /device/services/heartbeatxx    204     0.547%!i(int=0)        0.434   0.112

so, it did not eliminate the junk characters from "%" till ")"

Last edited by Don Cragun; 09-30-2014 at 09:04 PM.. Reason: Add CODE tags.
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #516
Difficulty: Easy
Programming languages only support a single data type in order to insure faster execution.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find records with specific characters in 2 nd field

Hi , I have a requirement to read a file ( 5 fields , ~ delimited) and find the records which contain anything other than Alphabets, Numbers , comma ,space and dot . ie a-z and A-Z and 0-9 and . and " " and , in 2nd field. Once I do that i would want the result to have field1|<flag> flag can... (2 Replies)
Discussion started by: ashwin3086
2 Replies

2. UNIX for Beginners Questions & Answers

Need to remove Junk characters

Hi All, I have a issue that we are getting Junk characters from source and i am not able to load that records to Database. Line breakers Junk Characters ( and different every time) Japanese Characters Every time I am using grep command and awk -F "\007" to find them and delete that... (1 Reply)
Discussion started by: spradeep86
1 Replies

3. Shell Programming and Scripting

Remove all junk characters from a text file

I am using flatfile, in that flat file we are getting the junk chars 1)I21001f<82>^Me<85>!h49 Service Charge 2) I21001f e...!h49 Service Charge please tell me how to remove all junk chars in unix scripts. (1 Reply)
Discussion started by: Talari
1 Replies

4. Shell Programming and Scripting

Remove first n characters from specific columns

I have a file like: s_20331 803 1 1 5 1:2=0.00000000 1:3=0.00000000 1:4=0.11111111 s_20331 814 1 1 5 1:2=0.00000000 1:3=0.12611607 1:4=0.00000000I would like to remove the four characters "x:x=" from all columns containing them (in my actual file, there are 15 total columns (i.e. columns... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

5. Shell Programming and Scripting

[Solved] Counting specific characters within each field

Hello, I have a file like following: ALB_13554 1 1 1 ALB_13554 1 2 1 ALB_18544 2 0 2 ALB_18544 1 0 1 This is a sample of my file, my real file has 441845 number of fields. What I want to do is to calculate the number of 1 and 2 in each column using AWK, so, the output file looks like... (5 Replies)
Discussion started by: Homa
5 Replies

6. UNIX for Dummies Questions & Answers

How to remove JUNK characters (FROM�)

Hi I have to remove the junk characters from my file. Please help.. File content : CURITY_CODE_GSD) FROM� DL_CB_SOD_EOD_VALUATION WHERE� ASOF (1 Reply)
Discussion started by: arukuku
1 Replies

7. Shell Programming and Scripting

Remove the special characters from field

Hi, In source data few of columns are having special charates(like *) due to this i am not able to display the data into flat file.it's displaying the some of junk data into the flat file. source dataExample: Address1="XDERFTG * HYJUYTG" how to remove the special charates in a string (2 Replies)
Discussion started by: koti_rama
2 Replies

8. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

9. Shell Programming and Scripting

Remove junk characters using Perl

Guys, can you help me in removing the junk character "^S" from the below line using perl Reference Data Not Recognised ^S Where a value is provided by the consuming system, which is not reco Thanks, M.Mohan (1 Reply)
Discussion started by: mohan_xunil
1 Replies

10. HP-UX

extract field of characters after a specific pattern - using UNIX shell script

Hello, Below is my input file's content ( in HP-UX platform ): ABCD120672-B21 1 ABCD142257-002 1 ABCD142257-003 1 ABCD142257-006 1 From the above, I just want to get the field of 13 characters that comes after 'ABCD' i.e '120672-B21'... . Could... (2 Replies)
Discussion started by: jansat
2 Replies

Featured Tech Videos