Count specific character of a file in each line and delete this character in a specific position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Count specific character of a file in each line and delete this character in a specific position
# 1  
Old 08-08-2018
Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment.

Scenario:

i have 2 files :

Code:
1) /tmp/TRANSACTIONS_DAILY_20180730.txt:

Code:
201807300000000004 
201807300000000005 
201807300000000006 
201807300000000007 
201807300000000008

Code:
2) /opt/TRANSACTIONS_DAILY_20180730.txt

Code:
20180730|201807300000000005||50001521111200|0106276-4|5SIJ00|WIRE||EUR|EUR|20180730|20180730|||||||0000000000030 0.00|00000000000300.00|Credit||||||||||SIJ|||500015|506|||||||||||||||||||||||||FI3158410220205399||||FI|SME5
20180730|201807300000000005||50001521111200|0106276-4|5SIJ00|WIRE||EUR|EUR|20180730|20180730|||||||00000000000300.00|00000000000300.00|Credit||||||||||SIJ|||500015|506|||||||||||||||||||||||||FI3158410220205399||||FI|SME5
20180730|201807300000000006||50001521111200|0106276-4|5SIJ00|WIRE||EUR|EUR|20180730|20180730|||||||00000000000050.00|00000000000050.00|Credit||||||||||SIJ|||500015|506|||||||||||||||||||||||||FI3650005020017008||||FI|SME5
20180730|201807300000000007||50001521111200|0106276-4|5SIJ00|WIRE||EUR|EUR|20180730|20180730|||||||00000000000015.00|00000000000015.00|Credit||||||||||SIJ|||500015|506|||||||||||||||||||||||||FI1958410220026068||||FI|SME5
20180730|201807300000000008||50001521111200|0106276-4|5SIJ00|WIRE||EUR|EUR|20180730|20180730|||||||00000000000300.00|00000000000300.00|Credit||||||||||SIJ|||500015|506|||||||||||||||||||||||||FI8358410220212320||||FI|SME5

i) I want to read each line of the first file and if this "string" line will exist to the second file will put whole transaction of the second file to a new file.

ii) This new file that is created we will count the " | " characters in each line and if they more than 64 in each line , the 61 " | " in the specific line will be deleted.

I have managed to do the i) part of the script but i need help for the ii) part.

MY code until now for i) part which works:

Code:
#!/bin/bash

while read line
do

grep "$line" /opt/TRANSACTIONS_DAILY_20180730.txt

done < /tmp/TRANSACTIONS_DAILY_20180730.txt > tmp/TRANSACTIONS_DAILY_NEW_20180730.txt

This specific Solaris enviroment doesnt support sed -r option neither grep -f or grep -o

Last edited by Neo; 08-08-2018 at 01:59 PM..
# 2  
Old 08-08-2018
Not quite sure about ii) requirement. You want to delete the whole line? You want to truncate the line to contain only 61 fields (I-separated)? Something else?
Could you provide another representative sample of say 10 fields requirement (instead of 61) and provide a desired output?
Thanks
# 3  
Old 08-08-2018
Basically i want from this new file that is created to delete the 61th pipe in each line " | " if in its line is more than 64th pipes " | " .

I dont want to delete the whole line neither to truncate.

Is it more understable now ? :-)
# 4  
Old 08-08-2018
I have to second vgersh99 in that your sample data should be refined. There should be lines that don't match either of your criteria. Right now, all file2 lines have a match and will be selected, and all have 64 separators. On top, with the sample given, removing the 61. separator or 60. or 62. wouldn't make a difference as they all are grouped together.
# 5  
Old 08-08-2018
try this - a bit rough, but...:
Code:
awk -F'|' 'FNR==NR{f1[$1+0];next}($2+0) in f1 {if (NF>64) {$60=$60 $61;$61="";sub("[|]{61}","")}print}' OFS='|' /tmp/TRANSACTIONS_DAILY_20180730.txt /opt/TRANSACTIONS_DAILY_20180730.txt

# 6  
Old 08-08-2018
Deleting an input field in awk might not be portable.
Even this rmcol() function is not portable.
The following should work with Posix-compatible awk and sed. On Solaris requires the /usr/xpg4/bin/ versions.
Code:
#!/bin/bash
PATH=/usr/xpg4/bin:/bin:/usr/bin
awk 'NR==FNR { K[$1]; next } ($2 in K)' file1 FS="|" file2 |
sed '/\([^|]*|\)\{65\}/ s/|//61'

It uses a pipe between awk and sed.
Of course you can have an intermediate file as you stated in post #1
Code:
awk 'NR==FNR { K[$1]; next } ($2 in K)' file1 FS="|" file2 > newfile
sed '/\([^|]*|\)\{65\}/ s/|//61' newfile


Last edited by MadeInGermany; 08-08-2018 at 05:44 PM..
This User Gave Thanks to MadeInGermany For This Post:
# 7  
Old 08-08-2018
Try also


Code:
awk -F\| '

function RMFLD(FNO, REP, PAT)   {for (i=1; i<FNO; i++) REP = REP $i FS
                                 PAT = REP $FNO
                                 if (index ($0, PAT) == 1) $0 = REP substr ($0, length (PAT) + 2) 
                                }

FNR == NR       {T[$1]
                 next
                }

                {for (t in T) if ($0 ~ t)       {if (NF > 65) RMFLD(61)
                                                 print
                                                }
                }
' file[12]

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Search for a pattern and replace a space at specific position with a Character in File

In file, we have millions of records each of 1000 in length. And at specific position say 800 there is a space, we need to replace it with Character X if the ID in that row starts with 123. So far i have used the below which is replacing space at that position to X but its not checking for... (3 Replies)
Discussion started by: Jagmeet Singh
3 Replies

2. Shell Programming and Scripting

Delete character on specific position

Hi, im still new in unix. i want to ask how to delete character on specific position in line, lets say i want to remove 5 character from position 1000, so characters from position 1000-1005 will be deleted. i found this sed command can delete 4 characters from position 10, but i dont know if... (7 Replies)
Discussion started by: bluesue
7 Replies

3. Shell Programming and Scripting

Delete line based on count of specific character

I'm looking for what I hope might be a one liner along these lines: sed '/a line with more than 3 pipes in it/d' I know how to get the pipe count in a string and store it in a variable, but I'm greedy enough to hope that it's possible via regex in the /.../d context. Am I asking too much? ... (5 Replies)
Discussion started by: tiggyboo
5 Replies

4. UNIX for Advanced & Expert Users

Count specific word or character per line

Hi, I need help regarding counting specific word or character per line and validate it against a specific number i.e 10. And if number of character equals the specific number then that line will be part of the output. Specific number = 6 Specific word or char = || Sample data:... (1 Reply)
Discussion started by: janzper
1 Replies

5. Shell Programming and Scripting

Using sed to replace specific character and specific position

I am trying to use sed to replace specific characters at a specific position in the file with a different value... can this be done? Example: File: A0199999123 A0199999124 A0199999125 Need to replace 99999 in positions 3-7 with 88888. Any help is appreciated. (5 Replies)
Discussion started by: programmer22
5 Replies

6. Shell Programming and Scripting

Insert character in a specific position of a file

Hi, I need to add Pipe (|) at 5th and 18th position of all records a file. How can I do this? I tried to add it at 5th position using the below code. It didnt work. Please help!!! awk '{substr($0,5,1) ~ /|/}{print}' $input_file > $temp_file (1 Reply)
Discussion started by: gpaulose
1 Replies

7. Shell Programming and Scripting

Print lines with specific character at nth position in a file

I need to print lines with character S at nth position in a file...can someone pl help me with appropriate awk command for this (1 Reply)
Discussion started by: manaswinig
1 Replies

8. Shell Programming and Scripting

Print lines with specific character at nth position in a file

I need to print lines with character S at nth position in a file...can someone pl help me with appropriate awk command for this (2 Replies)
Discussion started by: manaswinig
2 Replies

9. Shell Programming and Scripting

Count specific character(s) very large file

I'm trying to count the number of 2 specific characters in a very large file. I'd like to avoid using gsub because its taking too long. I was thinking something like: awk '-F' { t += NF - 1 } END {print t}' infile > outfile which isn't working Any ideas would be great. (3 Replies)
Discussion started by: dcfargo
3 Replies

10. HP-UX

count occurences of specific character in the file

For counting the occurences of specific character in the file I am issuing the command grep -o 'character' filename | wc -w It works in other shells but not in HP-UX as there is no option -o for grep. What do I do now? (9 Replies)
Discussion started by: superprogrammer
9 Replies
Login or Register to Ask a Question