Adding String at specific postition of a delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding String at specific postition of a delimiter
# 1  
Old 05-27-2012
Adding String at specific postition of a delimiter

I have a file containing data lines:

HTML Code:
1|14|CONSTANT||11111111||00887722||30/04/2012|E|O|X||||20120528093654-30.04.2012|STA11ACT|ddts555S||||00001|rrttbbggcc|
1|15|CONSTANT||22222222||00887722||30/04/2012|E|O|X||||20120528093654-30.04.2012|rrtha772|llkis000||||00001|AAEtbbggcc|
I want to add a string say "11" after 3rd position (after the string "CONSTANT") of the delimiter in each line if it is blank. so that the file should look like this.

HTML Code:
1|14|CONSTANT|11|11111111||00887722||30/04/2012|E|O|X||||20120528093654-30.04.2012|STA11ACT|ddts555S||||00001|rrttbbggcc|
1|15|CONSTANT|11|22222222||00887722||30/04/2012|E|O|X||||20120528093654-30.04.2012|rrtha772|llkis000||||00001|AAEtbbggcc|
thanks.
# 2  
Old 05-28-2012
Hi

Code:
 awk -F"|" '$4=11' OFS="|"  file

Guru.
# 3  
Old 05-28-2012
Thanks Guru !! This works perfectly fine !!!
# 4  
Old 05-28-2012
Quote:
Originally Posted by pparthiv
I want to add a string say "11" after 3rd position (after the string "CONSTANT") of the delimiter in each line if it is blank.
Code:
awk -F"|" 'BEGIN {OFS="|"} {if ($4 ~ /^$/) $4=11;print}' <filename>

This is the required solution as you need to replace only if the field is blank...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace specific column delimiter

Hi All, I have a file with a pipe delimiter. I need to replace the delimiter with html tags. I managed to get all the delimiters replaced along with first and last but the requirement is that I need to change 7th delimiter with slight change. File1: ... (2 Replies)
Discussion started by: shash
2 Replies

2. Shell Programming and Scripting

Adding delimiter to a fixed pattern of date

I have a text file as below: jan16201413:17PM jan1620143:17PM jan1620143:17PM jan1620143:17PM jan1620143:17PM I want to add a delimeter which will be space to the date part as below: jan 16 2014 13:17 PM jan 16 2014 3:17 PM jan 16 2014 3:17 PM jan 16 2014 3:17 PM jan 16... (13 Replies)
Discussion started by: Sharma331
13 Replies

3. UNIX for Advanced & Expert Users

Inserting delimiter after a specific number of chars

Hello guys, I have a problem where I need to add a delimiter, that can be | for example, after each 28000 chars. The problem is that sometimes 1 row, which should contain 28000 chars is split in 2, so I want to put the delimiter after each 28000 so I will know the end of each row. Please... (2 Replies)
Discussion started by: Diogo R Jesus
2 Replies

4. Shell Programming and Scripting

Adding a delimiter to a variable length file

Hi, I'm new to unix, i have a variable length file like below, 01|Test|Test1|Sample| 02|AA|BB|CC|DD| 03|AAA|BBB|CCC|DDD|EEE|RRR|TTT|SSS|YYY| I need to make this as a fixed length file. Assume that i have 10 columns in the DAT file. for ex: the first 01 record is having 4cols -... (8 Replies)
Discussion started by: Mohankumar Venu
8 Replies

5. Shell Programming and Scripting

Selecting Specific Columns and Insert the delimiter TAB

Hi, I am writing a Perl Script for the below : I have a data file that consists of the header information which is 231 Lines and the footer information as 4 lines. The total number of line including the header and footer 1.2 Million with Pipe Delimited file. For example: Header Information:... (4 Replies)
Discussion started by: filter
4 Replies

6. Shell Programming and Scripting

How to remove delimiter from specific column?

I have 5 column in sample txt file where in i have to create report based upon 1,3 and 5 th column.. I have : in first and third coulmn. But I want to retain the colon of fifth coulmn and remove the colon of first column.. 5th column contains String message (for example,... (7 Replies)
Discussion started by: Shirisha
7 Replies

7. Shell Programming and Scripting

Adding a delimiter to a text file

Im writing a KSH script to read a simple text file and add a delimiter. Ive written the following script but it runs very slow. I initially used the cut command to substring the input record then switched to this version using awk to substring... both run too slow. Any ideas how to make this more... (2 Replies)
Discussion started by: lock
2 Replies

8. Shell Programming and Scripting

Parsing string using specific delimiter

Hi, I'm wondering what is the best way to parse out a long string that has a specific deliminator and outputting each token between the delim on a newline? i.e. input text1,text2,text3,tex4 i.e. output text1 text2 text3 text4 (8 Replies)
Discussion started by: primp
8 Replies

9. Shell Programming and Scripting

Adding delimiter to logged in users

Hi guys! Just was wanting to run a command that would allow me to seperate the currently logged in users. Basically from this format: user1 user2 user3 To: user1|user2|user3 (Note the lack of a pipe at the end, not sure if thats possible) Basically it needs to be in this... (11 Replies)
Discussion started by: crawf
11 Replies

10. Shell Programming and Scripting

adding delimiter to a fixed width file

Hi , I have a file : CSCH74000.00 CSCH74000.00 CSCH74100.00 CSCH74000.00 CSCH74100.00 CSCH74000.00 CSCH74000.00 CSCH74100.00 CSCH74100.00 CSCH74100.00 I have to put a delimiter( say comma) in between after 6th character: CSCH74,000.00 CSCH74,000.00 CSCH74,100.00 (2 Replies)
Discussion started by: sumeet
2 Replies
Login or Register to Ask a Question