Replace pipe <|> with comma <,> in a column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace pipe <|> with comma <,> in a column
# 8  
Old 08-12-2011
Code:
awk -F\| '/^[0-9]/{for(;++i<=NF;)x=x ((i==1)?$i:(($i==int($i))?"|":",") $i);$0=x;x=y}1' file
Col1|Col2|Col3|Col4|col5|col6|col7|col8
1|2|3,3.1,3.2|4|5|6,6.1,6.2|7|8,8.1


Last edited by danmero; 08-12-2011 at 08:02 AM..
# 9  
Old 08-12-2011
What system are you on? Try this:

Code:
nawk '{ 
  split($0,d,".");  #store the integer part
  out= (last==d[1]) ? l"," : l"|";   #compare current integer part with last one; append comma or pipe
  if(l) { printf "%s", out; }    #print it out
  l=$0;  last=d[1];      #remember for comparison
}END{printf "%s",l }' RS="|" input

# 10  
Old 08-14-2011
Thanks mirni for your reply, the script able to run now.

However, it doesnt work for my actual source file. The sample input file is merely example, i apologize for this.

Actual input file contained 25 columns thus i cant post it up all together thus i cited an example in my post.

this sample input file will be more appropriate

Col1|Col2|Col3|Col4|col5|col6|col7|col8
A|data|Test|script|AB|data data|data test data|another|test|data|test|testing|testtt|testng|te

output:
Col1|Col2|Col3|Col4|col5|col6|col7|col8
A|data|Test,script,AB|data data|data test data|another,test,data|test|testing,testtt,testng,te

for this sample file, i will need to take care of col3, col6, col8 for the existence of pipe, and convert it into comma.
Thanks in advance for your help.
# 11  
Old 08-14-2011
Quote:
... will be more appropriate
No. Think about the 8-th column and clarify your requirements or change your sample input/output.
# 12  
Old 08-15-2011
I don't understand your input and output either: How do you know that it's the 3rd,4th and 5th column that need to be merged, and not 4,5,6?
Please elaborate and ideally post a sample of the real input and desired output. You can attach a file to your post if space is a concern.
# 13  
Old 08-15-2011
try this ..
Code:
$ echo "1|2|3|3.1|3.2|4|5|6|6.1|6.2|7|8|8.1" | tr '|' '\n' | awk ' { if ( $0~/[368]/ ) { print $0":" } else { print $0 } } ' | xargs | sed 's,: ,\,,g;s, ,|,g;s,:$,,g;s,\,4,|4,g;s,\,7,|7,g''
1|2|3,3.1,3.2|4|5|6,6.1,6.2|7|8,8.1
$


Last edited by jayan_jay; 08-15-2011 at 08:10 AM..
# 14  
Old 08-15-2011
@ jayan_jay I thought your previous post has the longest shell line I've ever seen here. No. It was only 177 chars and this one has 187. I'm afraid even to think about the next post. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace delimiter for a particular column in a pipe delimited file

I have an input file as below Emp1|FirstName|MiddleName|LastName|Address|Pincode|PhoneNumber 1234|FirstName1|MiddleName2|LastName3| Add1 || ADD2|123|000000000 Output : 1234|FirstName1|MiddleName2|LastName3| Add1 ,, ADD2|123|000000000 OR 1234,FirstName1,MiddleName2,LastName3, Add1 ||... (2 Replies)
Discussion started by: styris
2 Replies

2. Shell Programming and Scripting

Bring values in the second column into single line (comma sep) for uniq value in the first column

I want to bring values in the second column into single line for uniq value in the first column. My input jvm01, Web 2.0 Feature Pack Library jvm01, IBM WebSphere JAX-RS jvm01, Custom01 Shared Library jvm02, Web 2.0 Feature Pack Library jvm02, IBM WebSphere JAX-RS jvm03, Web 2.0 Feature... (10 Replies)
Discussion started by: kchinnam
10 Replies

3. Shell Programming and Scripting

Replace spaces with underscores up to first comma but not after the comma

I have a comma delimited file of major codes and descriptions. I want to replace all occurrences of spaces with underscores up to the first comma (only in the first field), but not replace spaces following the comma. For instance I have the following snippet of the file: EK ED,Elementary and... (7 Replies)
Discussion started by: tdouty
7 Replies

4. Shell Programming and Scripting

Replace pipe delimited column string to null

Hi All, I have a large dat file where each lines are pipe delimited values. I need to parse the file depending on the request. For example: sometimes I have told to remove all the values in the 7th column (this case remove values '3333' only from the first line and '3543' from the second line)... (4 Replies)
Discussion started by: express14
4 Replies

5. Shell Programming and Scripting

Replace comma and blank with comma and number

I, I have a file and i need to replace comma and blank space with comma and 0. cat file.txt a,5 b,1 c, d, e,4 I need the output as cat file.txt a,5 b,1 c,0 d,0 (4 Replies)
Discussion started by: jaituteja
4 Replies

6. Shell Programming and Scripting

Sed, replace comma with pipe. but ignore qoutes

hi, I am trying to replace comma with pipe, but the issue is that i want to ignore the commas inside qoutes. for example: i have file with the string: 1,"2,3",4,"5","6,7" the result should be : 1|"2,3"|4|"5"|"6,7" i trying to use sed and awk (match function) for that, but i did not... (4 Replies)
Discussion started by: gabik
4 Replies

7. Shell Programming and Scripting

comma replaced with pipe

Source data: "123","aaa bbb CCC","12000" "134","HHH,bbc","13000" i have a delimited file. i want to replace with the pipe.The sed command is not working for replacing a delimeter. Command : sed s/\,/\|/g filename Output : When i run the command it is replacing the columns value... (7 Replies)
Discussion started by: number10
7 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

Trimming fields for comma or pipe seperated file

I have file like this FileA: abc , "helloworld" , america def,asia, japan ghi, africa, ipl Output Needed: abc,"helloworld",america def,asia,japan ghi,africa,ipl I would like to implement using awk. I want to trim each field for its leading and trailing spaces. (7 Replies)
Discussion started by: pinnacle
7 Replies

10. Shell Programming and Scripting

To Replace comma with Pipe inside double quotes

Hi, I have a requirement to replace the comma's inside the double quotes. The comma's inside the double quotes will get changed dynamically. Input Record: "Washington, DC,Prabhu,aju",New York Output Record: "Washington| DC|Prabhu|aju",New York I tried with the below command but it... (3 Replies)
Discussion started by: prabhutkl
3 Replies
Login or Register to Ask a Question