Finding nth occurrence in line and replacing it


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Finding nth occurrence in line and replacing it
# 1  
Old 01-31-2008
Finding nth occurrence in line and replacing it

Hi,

I have several files with data that have to be imported to a database. These files contain records with separator characters. Some records are corrupt (2 separators are missing) and I need to correct them prior to importing them into the db.
Example:

|field1|field2|field3|field4|field5|field6|field7|field8|field9|field10|field11|field12|||
|field1|field2|field3|field4|field5|field6|field7|field8|field9|field10|field11|field12|field13|fiel d14|field15
|field1|field2|field3|field4|field5|field6|field7|field8|field9|field10|field11|field12|field13

The records 1 and 2 are correct. They consist of 15 optional fields separated by 15 | characters.
Record 3 is corrupt since it's missing 2 separator characters ahead field13.
It should look like:
|field1|field2|field3|field4|field5|field6|field7|field8|field9|field10|field11|field12|||field13

How can I achieve this by using sed or awk or something else?
Any help is greatly appreciated!

TIA,
Stephan.
# 2  
Old 01-31-2008
ok - not going to win a prize for pretty code, but,

Code:
 awk -F"|" 'NF==16{print}NF==14{OFS="|";$16=$14;$14="";print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16}' infile

works. With the input you provided, the following is the o/p:

Code:
|field1|field2|field3|field4|field5|field6|field7|field8|field9|field10|field11|field12|||
|field1|field2|field3|field4|field5|field6|field7|field8|field9|field10|field11|field12|field13|fiel d14|field15
|field1|field2|field3|field4|field5|field6|field7|field8|field9|field10|field11|field12|||field13

HTH,
# 3  
Old 01-31-2008
Hey, thanks for your fast reply!

I don't care about pretty solutions - it works and that's important to me! I just tested on my real file and that's what I looked for!

Maybe one day I have the time to pimp the statement - but I won't win a prize as well... Smilie

Thanks again!
# 4  
Old 01-31-2008
If that's the requirement:

Code:
awk 'NF==16||$NF=FS FS $NF' OFS="|" FS="|" filename

Use nawk or /usr/xpg4/bin/awk on Solaris.
# 5  
Old 01-31-2008
now there you go - that's what a real guru comes up with, and I'm impressed - never even thought of that sneaky solution.

nice piece of code radoulov - I keep learning something new from your posts - you da man.
# 6  
Old 02-01-2008
Wow, that's a really nice solution. I never would have found that on my own - of course, that's the reason why I post to the Dummies forum... Smilie Thank you!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trim after nth occurrence with loop

Hello, Below command trims right after the nth occurrence of a string. When I try in while loop, it is not working. In Terminal IFS=/ ; read -ra val < Textfile ; echo "${val:0:3}" It gives only one line: sunday/monday/tuesday Textfile: sunday/monday/tuesday/wednesday/thursday... (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

Replacing nth field with nth_text for each line in a file

Hi All, I am very new to shell scripting and tried to search this in the forum but no luck. Requirment: I have an input file which is comma separated. I need to replace the value in 4th column with another value. This has to happen for all the lines in the file. Sample data: Input... (2 Replies)
Discussion started by: arunkumarsd
2 Replies

3. Shell Programming and Scripting

Extract the text between the nth occurrence of square brackets

Please can someone help with this? I have a file with lines as follows: word1 word2 word3 word4 word5 word6 word7 word8 word1 word2 word3 word4 word5 word6 word7 word8 word1 word2 word3 word4 word5 word6 word7 word8 word1 word2 word3 word4 word5 word6 word7 word8 When I use the... (7 Replies)
Discussion started by: Subhadeep_Sahu
7 Replies

4. Shell Programming and Scripting

Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04

Hi, I am getting crazy after days on looking at it: Bash in Ubuntu 12.04.1 I want to do this: pattern="system /path1/file1 file1" new_pattern=" data /path2/file2 file2" file to edit: data.db - I need to search in the file data.db for the nth occurrence of pattern - pattern must... (14 Replies)
Discussion started by: Phil3759
14 Replies

5. Shell Programming and Scripting

How to output all lines following Nth occurrence of string

Greetings experts. Searched the forums (perhaps not hard enough?) - Am searching for a method to capture all output from a log file following the nth occurrence of a known string. Background: Using bash, I want to monitor my Oracle DB alert log file. The script will count the total # of... (2 Replies)
Discussion started by: cjtravis
2 Replies

6. Shell Programming and Scripting

Replacing nth occurrence

There is already one thread with the same heading. But here the case is little different. i have a line which have a field separator '|' abc|def|ghi|jkl|mno|pqr|stu|vwx|yz I want to replace every 3rd occurance + next character with the same + newline character.. I mean i want to enter a... (6 Replies)
Discussion started by: ratheeshjulk
6 Replies

7. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

8. Shell Programming and Scripting

Finding and replacing whole line using sed

Hi all, assume that i am having the following line in a file called file1. triumph and disaster must be treated same. I want to replace this line with. follow excellence success will chase you. is it possible to do this using sed. if possible kindly post me the... (2 Replies)
Discussion started by: anishkumarv
2 Replies

9. UNIX for Dummies Questions & Answers

Finding nth line across multiple files

I have several files (around 50) that have the similar format. I need to extract the 5th line from every file and output that into a text file. So far, I have been able to figure out how to do it for a single file: $ awk 'NR==5' text1.txt > results.txt OR $ sed -n '5p' text1.txt > results.txt... (6 Replies)
Discussion started by: oriqin
6 Replies

10. Shell Programming and Scripting

Replacing a string in nth line

Hello All, How to replace a string in nth line of a file using sed or awk. For Ex: test.txt Line 1 : TEST1 TEST2 TEST3 Line 2 : TEST1 TEST2 TEST3 TEST4 Line 3 : TEST1 TEST2 TEST3 TEST5 Line 4 : TEST1 TEST2 TEST3 TEST6 Line 5 : TEST1 TEST2 TEST3 TEST7 i want to go to 4th line of a... (1 Reply)
Discussion started by: maxmave
1 Replies
Login or Register to Ask a Question