Conversion of '|' delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conversion of '|' delimiter
# 1  
Old 02-01-2010
Conversion of '|' delimiter

Hello All,

I want to convert the data saparated by '|' delimited in the flat file to the readable format.

The every odd number '|' (pipe) to be converted to '\tab' and every even numbered '|' to '\n' and transfer the data to another file.

example.

Code:
NAME|ABC|LASTNAME|PQR|AGE|20|SEX|MALE|
NAME|XYZ|LASTNAME|PDF|AGE|21|SEX|MALE|

output should be tab delimited as below -

Code:
NAME    ABC
LASTNAME   PQR
AGE      20
SEX      MALE
 
NAME    XYZ
LASTNAME   PDF
AGE      21
SEX      MALE    etc....

I tried the below script first to convert every second '|' to '\n' line but it's not working.

Code:
#!/usr/bin/ksh
FileName=INPUT_FILE.txt

if [[ -e $FileName ]]; then
while read line
  do
    awk -F |  '{for( i = 1;i <= NF;++i)
      if( i % 2 == 0 ) printf("%s\n",$i) 
    else
      printf("%s|",$i) ; }'
  done < $FileName
else
   echo "File $FileName  does not exists"
fi

Thank you in advance.

Last edited by Franklin52; 02-01-2010 at 11:16 AM.. Reason: Please use code tags!
# 2  
Old 02-01-2010
Let's do it with sed:
Code:
sed ':a;s/|/ /;s/|/\n/;ta' infile

# 3  
Old 02-01-2010
I am getting message as 'Label too long : :a;s/|/ /;s/|/\n/;ta'

The each input line will be more than 1000 characters and contains more than 75 even numbers of '|' delimiter.
# 4  
Old 02-01-2010
Code:
$ cat cmd
:a
s/|/ /
s/|/\
/
t a
$ sed -f cmd file
NAME ABC
LASTNAME PQR
AGE 20
SEX MALE

NAME XYZ
LASTNAME PDF
AGE 21
SEX MALE

# 5  
Old 02-01-2010
using perl

Code:
perl -wpl -e 'while (/\|/) { ++$i ; if ($i%2) {s/\|/\t/}  else {s/\|/\n/} ; }'infile.txt

SmilieSmilieSmilie
# 6  
Old 02-01-2010
Hi Ahmad,

It's workingSmilie. Could you please explain how it works ?
# 7  
Old 02-01-2010
Quote:
Originally Posted by ssachins
I am getting message as 'Label too long : :a;s/|/ /;s/|/\n/;ta'

The each input line will be more than 1000 characters and contains more than 75 even numbers of '|' delimiter.
Does this work (or Anbu's alternative)?
Code:
sed -e :a -e 's/|/ /' -e 's/|/\n/' -e ta infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl Code to change file delimiter (passed as argument) to bar delimiter

Hi, Extremely new to Perl scripting, but need a quick fix without using TEXT::CSV I need to read in a file, pass any delimiter as an argument, and convert it to bar delimited on the output. In addition, enclose fields within double quotes in case of any embedded delimiters. Any help would... (2 Replies)
Discussion started by: JPB1977
2 Replies

2. Shell Programming and Scripting

Shell script to put delimiter for a no delimiter variable length text file

Hi, I have a No Delimiter variable length text file with following schema - Column Name Data length Firstname 5 Lastname 5 age 3 phoneno1 10 phoneno2 10 phoneno3 10 sample data - ... (16 Replies)
Discussion started by: Gaurav Martha
16 Replies

3. Shell Programming and Scripting

Delimiter Conversion(Generic Code)

Hi All, I am looking for a generic code which can search for the existing delimiter(what ever it may be) in a file and convert it to Pipeline. The file may have Pipeline delimiter already in that case just leave it. Please find below some sample records. Sample records1: ... (2 Replies)
Discussion started by: Arun Mishra
2 Replies

4. Shell Programming and Scripting

how to get everything before the last delimiter?

hi all, i have a string with a number of "/"s as delimiter. and i want everything BEFORE the last delimiter i know to use basename to get everything after the last delimiter. thx a lot! (2 Replies)
Discussion started by: sunnydanniel
2 Replies

5. Shell Programming and Scripting

How to cut by delimiter, and delimiter can be anything except numbers?

Hi all, I have a number of strings like below: //mnt/autocor/43°13'(33")W/ and i'm trying to get the numbers in this string, for example 431333 please help thanks ahead (14 Replies)
Discussion started by: sunnydanniel
14 Replies

6. Shell Programming and Scripting

Help regarding the delimiter

Hi, I am trying to load data from a file to oracle DB. The file am using has a ";" as a delimiter. While I load the file, I want to check whether the file is having the correct delimiter or not. if not, the file should not be processed. Is there any way that i could handle this scenario using... (3 Replies)
Discussion started by: smileyreddy
3 Replies

7. UNIX for Dummies Questions & Answers

unconstant delimiter

I have file with unconstatnt delimiter for each field which are non-printable characters like tab and space file 6271 manchester (tab) 11/09/09 200 accepted 6272 manchester (tab) 11/09/09 200 accepted I want only first... (7 Replies)
Discussion started by: tsurendra
7 Replies

8. Shell Programming and Scripting

Substring based on delimiter, finding last delimiter

Hi, I have a string like ABC.123.XYZ-A1-B2-P1-C4. I want to delimit the string based on "-" and then get result as only two strings. One with string till last hyphen and other with value after last hyphen... For this case, it would be something like first string as "ABC.123.XYZ-A1-B2-P1" and... (6 Replies)
Discussion started by: gupt_ash
6 Replies

9. UNIX for Dummies Questions & Answers

Delimiter

I am having the following file. I need to insert a delimiter in this file. I used sed but its not working. AAABBB 9 JJJ AAABBC 9 TTTTT AAABBA 8 JJJ AAABBC 7 TTTTT AAABBC 6 TTTTT Now i want the output file as: AAA|BBB| |9| |JJJ| AAA|BBC| |9| | |TTTTT| (3 Replies)
Discussion started by: sivakumar.rj
3 Replies

10. Shell Programming and Scripting

from - to delimiter

hey guys can you please help me out, i'm having problem in cutting strings. I need a delimiter to cut string. sample a.txt "ID", "1234" , "iam bighippo", "help!" "ID", "1235" , "again0", "xxxxxxx1" "ID", "1236" , "again1", "xxxxxxx2" "ID", "1237" , "again2", "xxxxxxx3" how do... (6 Replies)
Discussion started by: bighippo
6 Replies
Login or Register to Ask a Question