Only one particular occurence needs to be substituted !!!


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Only one particular occurence needs to be substituted !!!
# 1  
Old 11-30-2008
Question Only one particular occurence needs to be substituted !!!

In a file, field separetor in line is irregular number of spaces, so I canot use field in sub function to get my charecter replaced with empty space. I would like to substitute only one perticular charecter with space at perticular posiotn, so I canot use perticular character as that may occur before the position to be repalced. I have written the code as below but passing (/substr($0,39,1)/ to sub function is wrong. Plz suggest any alternatives for below code.

awk '{ if(substr($0,39,1)=="N") { sub(/substr($0,39,1)/," ",$0); printf "%-517s", $0" ABCD ";} }' < inputfile >

Last edited by vaka; 12-01-2008 at 02:28 AM..
# 2  
Old 12-01-2008
Show an example for input and desired output please.
# 3  
Old 12-01-2008
Input:
NICEP H030325 350160564A N717391601 25081117201000199 11/13/200811/13/2008 496

Output:

NICEP H030325 350160564A 717391601 25081117201000199 11/13/200811/13/2008 496 ABCD
# 4  
Old 12-01-2008
Code:
root@isau02:~> echo "NICEP H030325 350160564A N717391601 25081117201000199 11/13/200811/13/2008 496 "| awk '$4 ~ /^[A-Z]/ {sub(/^./,"",$4); print}'
NICEP H030325 350160564A 717391601 25081117201000199 11/13/200811/13/2008 496

# 5  
Old 12-02-2008
thanks a lot, but we can not use $4 as field separator is not single space all the times..can we send any index(position) as an argument to sub function....
# 6  
Old 12-02-2008
If this works for your input file, you can use sub/substr as you did in your example if it is always at some fix position. If you show examples of any other posibilites of which field is the one you need to be changed, maybe post more examples of posibilities.
# 7  
Old 12-02-2008
It is working and I have modified as per your suggestion. I am almost done.
I am able to run the following code blocks independently. If I try to make them as one, I am getting error always at 2nd block. Can you please suggest what syntax error causing this?

ErrorLog:The error context is
substr($0,39,1)=="N" || substr($0,39,1)=="X" || substr($0,39,1)=="A" >>> { <<< # For the letters N, X, A
awk: 0602-502 The statement cannot be correctly parsed. The source line is 20.

3 AWK logical blocks:

------------------
BEGIN {
if (length($0) > 500) {
if (substr($0,39,1)=="X" || substr($0,39,1)=="A"){
space=" ";
sub( substr($0,39,1) , space)
printf "%-517s\n", $0
}
else {

printf "%-517s\n", $0

}
}
}
-------------

BEGIN {a["N"]="N ABC";a["X"]="P ABC";a["A"]="D ABC"}
substr($0,39,1)=="N" || substr($0,39,1)=="X" || substr($0,39,1)=="A" { # For the letters N, X, A
val=substr($0,39,1);
space=" ";
sub( substr($0,39,1) , space)
printf "%-517s\n", $0 " " a[val]
}

----------------------

BEGIN{ s=substr($0,39,20); sub(/ *$/, "", s) # s = trimmed string of pos 39-58 for other letters
if(length(s)==9) {
printf "%-517s\n", $0 " ABC EFG"
}
else if(length(s)==16) {
printf "%-517s\n", $0 " DEF GHI"
}
else {
printf "%-517s\n", $0 " JKL MNO"
}
}
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Python: Suppress lines not substituted

Hi all, I started playing around with Python (2.6.6) trying to parse a file. No matter what I tried so far I am not able to print only the lines/group/backreference that have been affected by the substitution. Instead I get also the other lines too File to parse: some more text ... (2 Replies)
Discussion started by: zaxxon
2 Replies

2. UNIX for Advanced & Expert Users

Get the first occurence between two patterns

I have an output file which gives me the timely status of a server. Sample file: March 11 2014 21:10, 1, 2, 3, 4, 5, 6, 7, 8, 9, x, y, z... 21:05, 1, 2, 3, 4, 5, 6, 7, 8, 9, x, y, z... 21:00, 1, 2, 3, 4,... (3 Replies)
Discussion started by: rpm120
3 Replies

3. Shell Programming and Scripting

Print between patterns - first occurence, second occurence etc

I have a file # cat asasas AAAAAA 11 22 33 44 BBBBB NILNILNIL AAAAAA 22 33 44 55 66 77 88 BBBBB NILNILNIL (2 Replies)
Discussion started by: anil510
2 Replies

4. Shell Programming and Scripting

Percentage of occurence

Dear all, I have data like below and i need to add coloumn before the COUNT field to see the Percentage out of all COUNT field value for respective raw. ============================================= COUNT CODE sConnType tConnType... (6 Replies)
Discussion started by: Iroshan
6 Replies

5. UNIX for Dummies Questions & Answers

How to print first occurence

Hi there, how can i print the first pattern occurrence in a .log file? I want to print the filename of the first 17262? I tried but all I can do is print all the lines with the number 17262? I tried using awk and sed but nothing!:wall: I just want filename! Here´s an example: 17259... (3 Replies)
Discussion started by: BMatter
3 Replies

6. Shell Programming and Scripting

Change first occurence

I'm not getting the syntax correct to change a line only on the first occurrence: I've tried to change only the first match and I've tried to change the from the second match forward sed 's/<B>PT#/<tr><td class=\"pt1\" width=\"40%\"><B>pt#/1' $file > tmpfile.html sed ... (0 Replies)
Discussion started by: dba_frog
0 Replies

7. Programming

Way to print a code with substituted macro..?

Is there any way to produce a code with all use dmacro to be substituted up to 'ready for compilation' condition? Some macro are build up and it is hard to replace all them up to final code by hand. I need to see the final line after all macro been applied by preprocessor. How that could be... (5 Replies)
Discussion started by: alex_5161
5 Replies

8. Shell Programming and Scripting

substituted variable assignment

I try to run this script, however, it gives an exception in line 3. How do I do an assignment to a substituted variable? #!/bin/bash name=fruit ext_$(eval echo ${name})=apple tmp=ext_$(eval echo ${name}) if ]; then echo "apple" elif ]; then echo "orange" fi echo ${!tmp} Error... (2 Replies)
Discussion started by: angelokh
2 Replies

9. UNIX for Dummies Questions & Answers

First occurence from grep

Hi , supoose i have a file in which a word is repeated so many times. I just want the firts occurence of that word through grep and it should not go to the next one means get the first occurence and stop there. Suggest me some solutions. Thanks Namish (10 Replies)
Discussion started by: namishtiwari
10 Replies

10. UNIX for Advanced & Expert Users

First Occurence

Hi, This is the format of the file that i have StartDate:10/01/06 EndDate :10/02/06 Cno Ccode 1 10 2 11 StartDate:10/03/06 EndDate :10/04/06 Cno Ccode 2 13 4 12 StartDate:10/01/06 EndDate :10/02/06 (5 Replies)
Discussion started by: kkm_job
5 Replies
Login or Register to Ask a Question