Sponsored Content
Top Forums Shell Programming and Scripting Count specific character of a file in each line and delete this character in a specific position Post 303021746 by teokon90 on Thursday 16th of August 2018 01:27:04 PM
Old 08-16-2018
Until now this code works for me :

Code:
#!/bin/bash
PATH=/usr/xpg4/bin:/bin:/usr/bin

while read line
do

grep "$line" /tmp/BadTransactions/test_data_for_validation_script.txt

awk 'NR==FNR { K[$1]; next } ($2 in K)' /tmp/BadTransactions/TRANSACTIONS_DAILY_20180730.txt FS="|" /opt/NorkomC
onfigS2/inbox/TRANSACTIONS_DAILY_20180730.txt > /tmp/BadTransactions/TRANSACTIONS_DAILY_NEW_20180730.txt

sed '/\([^|]*[|]\)\{65\}/ s/|//61' /tmp/BadTransactions/TRANSACTIONS_DAILY_NEW_20180730.txt

done < /tmp/BadTransactions/TRANSACTIONS_DAILY_20180730.txt > /tmp/BadTransactions/TRANSACTIONS_DAILY_NEW_201807
30.txt

So until now if there are more than 64th pipes in each line , it delete the 61th pipe.

Now , i want to delete the 61th pipe in each line if the line has more than 64 pipes until the line reaches the 64 pipes in whole line

What i mean :

If a line has for example 67 pipes , it will delete the 61th pipe , then it will go again to the same line and now it will check that it has more than 64 pipes(which actually has 66 now ) and i t will delete the 61th pipe.

This will be continued until the pipes are more than 64.

Could you please suggest me any idea how to loop that ?

Thank you

------ Post updated at 07:27 PM ------

Until now this code works for me :

Code:
Code:
#!/bin/bash
PATH=/usr/xpg4/bin:/bin:/usr/bin

while read line
do

grep "$line" /tmp/BadTransactions/test_data_for_validation_script.txt

awk 'NR==FNR { K[$1]; next } ($2 in K)' /tmp/BadTransactions/TRANSACTIONS_DAILY_20180730.txt FS="|" /opt/NorkomC
onfigS2/inbox/TRANSACTIONS_DAILY_20180730.txt > /tmp/BadTransactions/TRANSACTIONS_DAILY_NEW_20180730.txt

sed '/\([^|]*[|]\)\{65\}/ s/|//61' /tmp/BadTransactions/TRANSACTIONS_DAILY_NEW_20180730.txt

done < /tmp/BadTransactions/TRANSACTIONS_DAILY_20180730.txt > /tmp/BadTransactions/TRANSACTIONS_DAILY_NEW_201807
30.txt

So until now if there are more than 64th pipes in each line , it delete the 61th pipe.

Now , i want to delete the 61th pipe in each line if the line has more than 64 pipes until the line reaches the 64 pipes in whole line

What i mean :

If a line has for example 67 pipes , it will delete the 61th pipe , then it will go again to the same line and now it will check that it has more than 64 pipes(which actually has 66 now ) and i t will delete the 61th pipe.

This will be continued until the pipes are more than 64.

Could you please suggest me any idea how to loop that ?

Thank you
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
pipe(2) 							System Calls Manual							   pipe(2)

NAME
pipe() - create an interprocess channel SYNOPSIS
DESCRIPTION
creates an I/O mechanism called a pipe and returns two file descriptors, fildes[0] and fildes[1]. fildes[0] is opened for reading and fildes[1] is opened for writing. A read-only file descriptor fildes[0] accesses the data written to fildes[1] on a first-in-first-out (FIFO) basis. For details of the I/O behavior of pipes see read(2) and write(2). By default, HP-UX pipes are not STREAMS-based. It is possible to generate the kernel so that all pipes created on a system are STREAMS- based. This can only be done for HP-UX releases 10.0 and later. STREAMS-based FIFOs (created by or are not supported on HP-UX. To generate a kernel that supports STREAMS-based pipes: o STREAMS/UX must be installed. o The module and the driver must be included in the file. (When STREAMS/UX is installed, and are automatically added to the system file.) o The tunable parameter (see streampipes(5)) must be set to 1 in the file. (This is not automatically done when STREAMS/UX is installed.) o The kernel must be generated and the system rebooted. Once this is done, all pipes created by will be STREAMS-based. For more information, see EXAMPLES
The following example uses to implement the command string RETURN VALUE
returns one of the following values: Successful completion. Failure. is set to indicate the error. ERRORS
sets to one of the following error values if the corresponding condition is true. or more file descriptors are currently open. The system file table is full. The file system lacks sufficient space to create the pipe. Could not allocate resources for both Stream heads (STREAMS-based pipes only). SEE ALSO
sh(1), read(2), write(2), popen(3S), privileges(5), streampipes(5), streamio(7). STANDARDS CONFORMANCE
pipe(2)
All times are GMT -4. The time now is 02:54 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy