how to convert a line to columns, separated by | (pipe)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to convert a line to columns, separated by | (pipe)
# 1  
Old 03-03-2010
Power how to convert a line to columns, separated by | (pipe)

Hi,

Plz help.

input line
Code:
1;20100403;400|2;20100403;4|3;20290903;400|4;20290903;0|5;20290903;0|9;20100304;0|10;20100304;0|11;20100402;0|18;20100304;0


expected output
Code:
1;20100403;400
2;20100403;4
3;20290903;400
4;20290903;0
5;20290903;0
9;20100304;0
10;20100304;0
11;20100402;0
18;20100304;0


Last edited by Franklin52; 03-03-2010 at 06:06 AM.. Reason: Please use code tags!
# 2  
Old 03-03-2010
Code:
sed -i 's/|/\n/g' file_name

using this we can achieve this.

Consider that the file_name file will contain the content as
Code:
1;20100403;400|2;20100403;4|3;20290903;400|4;20290903;0|5;20290903;0|9;20100304;0|10;20100304;0|11;2 0100402;0|18;20100304;0

Now the file will contain the lines as
Code:
1;20100403;400
2;20100403;4
3;20290903;400
4;20290903;0
5;20290903;0
9;20100304;0
10;20100304;0
11;2 0100402;0
18;20100304;0


Last edited by Franklin52; 03-03-2010 at 06:07 AM.. Reason: code tags, please...
# 3  
Old 03-03-2010
Or...

Code:
tr '|' '\n' < infile

# 4  
Old 03-03-2010
You try the following script.

Code:
OIFS=$IFS
IFS='|'
for i in `cat inputfile`
do
echo $i
done
IFS=$OIFS

I used OIFS variable in the script to store the old IFS value.Because,if we change it ,we should restore it after our changes.That's why I restored the value at the end of our script.
# 5  
Old 03-03-2010
Code:
tr '|' '\n' < filename

... is working.

Thank you all.

Last edited by radoulov; 03-03-2010 at 06:55 AM.. Reason: Use code tags, please!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert fixed value fields to comma separated values

Hi All, Hope you are doing Great!!!. Today i have came up with a problem to say exactly it was for performance improvement. I have written code in perl as a solution for this to cut in specific range, but it is taking time to run for files thousands of lines so i am expecting a sed... (9 Replies)
Discussion started by: mad man
9 Replies

2. Shell Programming and Scripting

Remove leading zeros separated by pipe

I have a below file and I wanted to remove the leading zeros in each field separated by pipe File: 01/09/2017|2017/09/06|2017/02/06|02/06/2017|02/06/2017 06:50:06 AM|2017/02/06|02/06/2017|02/07/2017 05:45:06 AM| 02/08/2017|2017/08/06|2017/09/06|02/05/2017|02/07/2017 05:40:06... (4 Replies)
Discussion started by: Joselouis
4 Replies

3. Shell Programming and Scripting

Convert column to quote and comma separated row

Hi, I have a list of tables in a file.txt C_CLAIM C_HLD C_PROVIDER I want the output to be 'C_CLAIM','C_HLD','C_PROVIDER' Currently I'm usin awk and getting output which is almost correct but still has minor defects awk -vORS="','" '{ print $1 }' file.txt The output of... (4 Replies)
Discussion started by: wahi80
4 Replies

4. UNIX for Dummies Questions & Answers

How convert space separated list to matched columns?

Hi I have been racking my (limited) brains to get this to work without success I have a file output which is a list of lists - ie a single column of data that is separated by space into sub lists below - I need to both split this so that each list is in a separate column (eg tab or semicolon... (8 Replies)
Discussion started by: Manchesterpaul
8 Replies

5. Shell Programming and Scripting

Convert comma separated file to fix length

Hi, I am converting a comma separated file to fixed field lenght and I am using that: COLUMNS="25 24 67 26 39 63 20 34 35 14 397" ( cat $indir/input_file.dat | \ $AWK -v columns="$COLUMNS" ' BEGIN { FS=","; OFS=""; split(columns, arr, " "); } { for(i=1; i<=NF;... (5 Replies)
Discussion started by: apenkov
5 Replies

6. Shell Programming and Scripting

Print pipe separated list as line by line in Korn Shell

Korn Shell in AIX 6.1 I want to print the below shown pipe (|) separated list line by line. line=es349889|nhb882309|ts00293|snh03524|bg578835|bg37900|rnh00297|py882201|sg175883 for i in line do echo "Hello $line " done I wanted to execute the above for loop. But i can't even set the... (3 Replies)
Discussion started by: polavan
3 Replies

7. Shell Programming and Scripting

Convert a tab separated file using bash

Dear all, I have a file in this format (like a matrix) - A B C .. X A 1 4 2 .. 2 B 2 6 4 .. 8 C 3 5 5 .. 4 . . . ... . X . . ... . and want to convert it into a file with this format: A A = 1 A B = 4 A C = 2 ... A X = 2 B A = 2 B B = 6 etc (2 Replies)
Discussion started by: TheTransporter
2 Replies

8. Shell Programming and Scripting

Compare two columns separated by a tab

witam potrzebuje polecenia porownujacego koumny na podstawie n-ostatnich znakow danej linnijki tj mam 2 koumny AiB zawierajace ciag dowolnych znakow (dlugosci w kazdej linijce mga byc rozne wiec uzycie substra odpada) A B ewewewabc nbgujnnabc... (3 Replies)
Discussion started by: Toudi
3 Replies

9. Shell Programming and Scripting

Converting comma separated to pipe delimited file

Hi, I came across a very good script to convert a comma seperated to pipe delimited file in this forum. the script serves most of the requirement but looks like it does not handle embedded double quotes and commas i.e if the input is like 1234, "value","first,second", "LDC5"monitor",... (15 Replies)
Discussion started by: anijan
15 Replies

10. Shell Programming and Scripting

Pipe delimitated line with 148 columns

I have a pipe delimitated line with 148 columns. 123|ABC|XYZ|999|8|N|PRA/PA|Search|2|E|WW|KHG|| T |Y|U|KA|9W|K45| If I need to change a value in different column as below, how do I do that please? In column 14 change value to 100 In column 30 change value to XY In column 77 change value to... (2 Replies)
Discussion started by: ppat7046
2 Replies
Login or Register to Ask a Question