Depending upon character at particular position, string needs to be appended


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Depending upon character at particular position, string needs to be appended
# 8  
Old 11-30-2008
The code is not tested but it should give you the direction to get the desired output, play around with it:

Code:
awk 'BEGIN{a["J"]="Jaya";a["R"]="Ravi";a["S"]="Satish"}
substr($0,7,1)=="J" || substr($0,7,1)=="R" || substr($0,7,1)=="S" {    # For the letters J, R, S
  printf "%-256s\n", $1" "substr($2,2)" "a[substr($2,1,1)]
  next
}
{ s=substr($0,39,20); sub(/ *$/, "", s)            # s = trimmed string of pos 39-58 for other letters
  if(length(s)==9) {
    printf "%-256s\n", $0 " prakash"
  }
  else if(length(s)==16) {
    printf "%-256s\n", $0 " Dan"
  }
  else {
    printf "%-256s\n", $0 " Shelly"
  }
}' file


Last edited by Franklin52; 11-30-2008 at 10:46 AM.. Reason: typo "length"
# 9  
Old 12-01-2008
Thanks a ton franklin, I am able to tweak the above code as per my need.
# 10  
Old 12-02-2008
Franklin/Dennis, 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.


------------------
Code:
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"
  }
}


Last edited by Franklin52; 12-02-2008 at 04:18 PM.. Reason: adding code tags
# 11  
Old 12-02-2008
Use nawk, gawk or /usr/xpg4/bin/awk on Solaris. You can also adjust the code of the 2nd block and use an if statement:

Code:
BEGIN {a["N"]="N ABC";a["X"]="P ABC";a["A"]="D ABC"}
if(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]
}

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

2. Shell Programming and Scripting

To find nth position of character in string

Hi guyz i want to know nth position of character in string. For ex. var="UK,TK,HK,IND,AUS" now if we see 1st occurance of , is at 3 position, 2nd at 6,..4th at 13 position. 1st position we can find through INDEX, but what about 2nd,3rd and 4th or may be upto nth position. ? In oracle we had... (2 Replies)
Discussion started by: Jonty Immortal
2 Replies

3. Shell Programming and Scripting

URGENT!!!move character x of input string to some position

i have this prob I have some records in a file1 like this 1001 sajal singh tampa 1002 .... so on i have a pattern file which is like this 1,4 4,13 14,15 i have to read the first pair 1,4 and extract that from the first record so the pattern is 1001 now i have to scramble 1001... (1 Reply)
Discussion started by: spankincubus
1 Replies

4. Shell Programming and Scripting

Find character and Replace character for given position

Hi, i want find the character '-' in a file from position 284-298, if it occurs i need to replace it with 'O ' for the position in the file. How to do that using SED command. thanks in advance, Sara (9 Replies)
Discussion started by: Sara183
9 Replies

5. Shell Programming and Scripting

How to extract the certain position's character in a string

Suppose there are two files: A, format is like: line1 12 line2 33 line3 6 ... B, format is like: >header taaccctaaccctaaccctaacccaaccccaccccaaccccaaccccaac ccaaccctaaccctaaccctaacccaaccctaaccctaaccctaacccaa ccctcaccctcaccctcaccctcaccctcaccctcaccctcaccctaacc... (1 Reply)
Discussion started by: bioinflix
1 Replies

6. Shell Programming and Scripting

Replace character in certain position in a string

Hello everyone this is my first post of many to come :) I am writing a script and in this script at one point i need to replace a character in a particular position in a string for example: in the string "mystery" i would need to replace the 3rd position to an "r" so the string becomes... (3 Replies)
Discussion started by: snipaa
3 Replies

7. Shell Programming and Scripting

Junk Character appended at the end of a line

Hi All I have a rather unusual problem, which i have not faced till now. I have a script which exports some paths to a text file. The script runs fine but when i check the output file i can see some junk characters ^M appended at end of lines and random places. I am not able to figure... (4 Replies)
Discussion started by: raghu_shekar
4 Replies

8. Shell Programming and Scripting

how to find a position and print some string in the next and same position

I need a script for... how to find a position of column data and print some string in the next line and same position position should find based on *HEADER8* in text for ex: ord123 abs 123 987HEADER89 test234 ord124 abc 124 987HEADER88 test235 ... (1 Reply)
Discussion started by: naveenkcl
1 Replies

9. Shell Programming and Scripting

Replace string B depending on occurence of string A

Depending upon the occurence of string 'xyz', I want to remove -t from the input file. There is not a fixed length input file. Any suggestions Input file: this is xyz line -t of the data this is line 2 of -t of the data xyz this is line 3 of -t the file this is line xyz of the -t file... (1 Reply)
Discussion started by: hemangjani
1 Replies

10. Shell Programming and Scripting

How to add character in specific position of a string?

Hi All, I would like to use sed to add "-" between the following string: Value: 20060830 Result: 2006-08-30 Pls advice. Thx a lot Victor (5 Replies)
Discussion started by: victorlung
5 Replies
Login or Register to Ask a Question