awk script to replace nth character with comma


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script to replace nth character with comma
# 1  
Old 07-03-2014
awk script to replace nth character with comma

I have a requirement as below.

In one of my column, I have data which may or may not be separted with coma always. Now I need to validate the length of these text within the coma (if available) and if the length is more than 30 characters, I need to insert a coma either at 30 characters if its a white space or at last occurence of whitespace within 30 characters.

For example if my column value is "FOR ASSY, FFFFHDAD CONVOORS - STRUCTURE ABC DGER GSHH"

FOR ASSY is fine as this is well within 30.
For next set FFFFHDAD CONVOORS - STRUCTURE ABC DGER GSHH where the characted length is 43, I have to check if there is whitespace at 30 characters if so replace it with coma else look at the previous whitespce from 30 characters and replace it with coma.
Code:
FOR ASSY, FFFFHDAD CONVOORS - STRUCTURE, ABC DGER GSHH

Essantially I should have coma before every 30 characters or less. If the coma already exists in the data retain as it is.

Can some one help me with awk scripts for the same

Sample Data

Code:
ID Description
1    FOR ASSY,JONBEYOR,KRODUJT TRANSFER, EXTENDED (TWIN JHAIN) 59 JRS
2   FOR NSSY,INFEED JONBEYOR & DIBERIER,UKSTRENO
3   FOR NSSY,SIDE IUIDES & DIBIDER KLNTES - KRODUJT TRNNSFER    JONBEYOR NSSY,SIDE IUIDES &  DIBIDER KLNTES - KRODUJT TRNNSFER JONBEYOR
4   FOR FEEDER & HOKKER OOUNTINI NSSY
5   FOR DIBERIER ONIN OKERNTOR STNTION ELEJTRIJNL NSSEOBLY
6   FOR K27721-2-O RTS JLOSER OEJHNNIJNL SKNRE KNRTS


Thanks
Moderator's Comments:
Mod Comment When spacing is important and multiple adjacent spaces are present in your data, CODE tags are crucial for readers to be able to understand the format of your input. The forum rules require use of CODE tags when showing sample input, sample output, and code segments to make it easier to read and understand what you're trying to do.

Last edited by Don Cragun; 07-03-2014 at 04:30 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 07-03-2014
If a column can have space and comma, what's the field separator?
# 3  
Old 07-03-2014
SriniShoo's question is yet to be answered and may necessitate an entirely different approach.
For a file with just your description in it, you can try
Code:
awk -F, '       {for (i=1; i<=NF; i++) if (length ($i) > 30) {
                         mx = split ($i, T, "")
                         j = 30; while (j>=1 && T[j]!=" ") j--; T[j]=","
                         $i = ""; for (j=1; j<=mx; j++) $i=$i T[j]
                        } 
                 print}  
        ' OFS="," file

# 4  
Old 07-05-2014
Thanks RudiC. It worked exactly the same way I wanted.

@Srini - Its a single column having larger data either separetd by comma or a space.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script using awk to replace space by comma

I have the content of this file that i would like to replace the space by comma. The file content values in this format FName LName Date & time ------------------------------------ Gilles John 14/12/17 12:30:45 I want this format Fname,LName,Date&time... (7 Replies)
Discussion started by: gillesi
7 Replies

2. Shell Programming and Scripting

awk search and replace nth column by using a variable.

I am passing a variable and replace nth value with the variable. I tried using many options in awk command but unable to ignore the special characters in the output and also unable to pass the actual value. Input : "1","2","3" Output : "1","1000","3" TempVal=`echo 1000` Cat... (2 Replies)
Discussion started by: onesuri
2 Replies

3. Shell Programming and Scripting

Replace nth to nth character?

Hi I got the following problem and I wonder if some could please help me out? I'd like to replace character 8 - 16 , 16 - 24 cat file ... (2 Replies)
Discussion started by: stinkefisch
2 Replies

4. Shell Programming and Scripting

Replace a value of Nth field of nth row

Using Awk, how can I achieve the following? I have set of record numbers, for which, I have to replace the nth field with some values, say spaces. Eg: Set of Records : 4,9,10,55,89,etc I have to change the 8th field of all the above set of records to spaces (10 spaces). Its a delimited... (1 Reply)
Discussion started by: deepakwins
1 Replies

5. Shell Programming and Scripting

awk to search for specific line and replace nth column

I need to be able to search for a string in the first column and if that string exists than replace the nth column with "-9.99". AW12000012012 2.38 1.51 3.01 1.66 0.90 0.91 1.22 0.82 0.57 1.67 2.31 3.63 0.00 AW12000012013 1.52 0.90 1.20 1.34 1.21 0.67 ... (14 Replies)
Discussion started by: ncwxpanther
14 Replies

6. Shell Programming and Scripting

[Solved] Find and replace till nth occurence of a special character

Hi, I have a requirement to search for a pattern in each line in a file and remove the in between words till the 3rd occurrence of double quote ("). Ex: CREATE TABLE "SCHEMANAME"."AMS_LTV_STATUS" (Note: "SCHEMANAME" may changes for different schemas. Its not a fixed value) I need to... (2 Replies)
Discussion started by: satyaatcgi
2 Replies

7. Emergency UNIX and Linux Support

Replace nth position character of all the lines in file

I want to replace 150th character of all the lines in a file using sed or awk... searched the forums but didn't find exact answer (9 Replies)
Discussion started by: greenworld123
9 Replies

8. Shell Programming and Scripting

Find and replace a column that has '' to NULL in a comma delimited using awk or sed

Hi this is my first time posting ever. I'm relatively new in using AWK/SED, I've been trying many a solution. I'm trying to replace the 59th column in a file where if I encounter '' then I would like to replace it with the word NULL. example 0 , '' , '' , 0 , 195.538462 change it to 0... (5 Replies)
Discussion started by: gumal901
5 Replies

9. Shell Programming and Scripting

How to replace all entries of comma in text file by space or other character

Hi , How to replace all entries of comma in text file by space or other character. cat temp.txt A,B,C,D I want this file to be like A B C D Please help!!! (4 Replies)
Discussion started by: prashant43
4 Replies

10. UNIX for Dummies Questions & Answers

Replace nth character in a file with a period

Hi all, If you look at the example below,I want to replace the 21st character (,) with a period (.). I have 1000 records in a file can someone help me how to do that. Thankyou all in advance. "2008-07-15... (3 Replies)
Discussion started by: blackhawk_123
3 Replies
Login or Register to Ask a Question