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
# 1  
Old 11-29-2008
Question Depending upon character at particular position, string needs to be appended

Depending upon character at particular position in a line, string needs to be appended followed by empty spaces till 256th column position at the end of the each line in the file.

example:
123p4 J8769
123q4 R2345
123r4 S2345

What I Require is, it has to check at 7th column character and that is J for first line so it should become 123p4 8769 Jaya . J will be replaced my empty space and Jaya will be appended at 14 location followed by empty spaces till 256th column position.

the ideal result is as below:
123p4 8769 Jaya
123q4 2345 Ravi
123r4 2345 Satish

File is huge and all the lines in the file need to be applied above said logic.
Any script help is greatly appreciated.

Thanks,
Jaya

Last edited by vaka; 11-29-2008 at 04:25 AM..
# 2  
Old 11-29-2008
Try something like :

Code:
awk '{ if(substr($0,7,1)=="J") { sub(/J/," ",$0); printf "%-256s", $0" Jaya ";} }' < filename

# 3  
Old 11-29-2008
thanks Dennis, it is perfectly working for 1 st line of the input as your logic addresses one values that is J --- Jaya.
I need to have this code for all the three values. (J ---> Jaya, R ---> Ravi, S ---> Satish). Shall I need to write 3 times by changing variables and finally append 3 files?
# 4  
Old 11-29-2008
Try this:

Code:
awk 'BEGIN{a["J"]="Jaya";a["R"]="Ravi";a["S"]="Satish"}
{print $1, substr($2,2),a[substr($2,1,1)]}' file

Use nawk or /usr/xpg4/bin/awk on Solaris.

Regards
# 5  
Old 11-29-2008
Yes it is working but not giving empty spaces. After Jaya Ravi Satish, empty space need to be filled till 256 column position.
# 6  
Old 11-29-2008
Quote:
Originally Posted by vaka
Yes it is working but not giving empty spaces. After Jaya Ravi Satish, empty space need to be filled till 256 column position.
I yeah, forgot that, try this one:
Code:
awk 'BEGIN{a["J"]="Jaya";a["R"]="Ravi";a["S"]="Satish"}
{printf "%-256s\n", $1" "substr($2,2)" "a[substr($2,1,1)]}' file

Regards

Last edited by Franklin52; 11-29-2008 at 11:34 AM.. Reason: simplified code
# 7  
Old 11-30-2008
Thanks Franklin, it gives what I have requested. I need to add extra logic.
Any line which doesn't have J or R or S, at 7th position, I need to get the length lenght first and appliy logic based on lenght.

my present code which performs line by line in a loop: len=`cat firstline.txt | cut -c39-58 | tr -d ' ' | wc -m`

if len eq 9 append prakash at the end of the line followed by empty spaces as above code does..
else if lengh eq 16 append Dan at the end of the line followed by empty spaces till 256 as above code does...
else if lenth -ne 9 && length -ne 16 append Shelly at the end of the line followed by empty spaces till 256 as above code does...
-----------------
Right now I am doing line by line so there is huge performence hit.. so trying to apply the logic for all lines at once
----------------

Franklin plz help me out in giving the above logic in awk or sed....

Regards,
Jaya

Last edited by vaka; 11-30-2008 at 02:47 AM..
 
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