insert pipe in file to separate values


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users insert pipe in file to separate values
# 1  
Old 03-17-2006
insert pipe in file to separate values

hi all...
i need your help, because i donīt know what to do...
i have a flat file like this:

B065200512312004123111010000061451 000021853
B065200512312004123111020000621907 000417802
B065200512312004123111030000005214 000005861
B065200512312004123111040000120133 000088448

and i need insert the pipe character (" | ") in each record, to separate the values. I need this:

B|065|20051231|20041231|11010|000061451| |000021853|
B|065|20051231|20041231|11020|000621907| |000417802|-
B|065|20051231|20041231|11030|000005214|-|000005861|
B|065|20051231|20041231|11040|000120133| |000088448|-


how can i do that? is that possible?
please any idea, thanks...
# 2  
Old 03-17-2006
You could use Perl's unpack() to read fixed length field records.

e.g. put something like this in a file and have it run through Perl

<pre>
$ cat insert_pipes.pl
#!/usr/bin/perl

while (<DATA>) {
chomp;
@unpacked = unpack 'A1A3A8A8A5A9x1A*', $_;
print join('|', @unpacked[0..5], q{ }, $unpacked[-1], q{}), "\n";
}
__DATA__
B065200512312004123111010000061451 000021853
B065200512312004123111020000621907 000417802
B065200512312004123111030000005214 000005861
B065200512312004123111040000120133 000088448


$ ./insert_pipes.pl
B|065|20051231|20041231|11010|000061451| |000021853|
B|065|20051231|20041231|11020|000621907| |000417802|
B|065|20051231|20041231|11030|000005214| |000005861|
B|065|20051231|20041231|11040|000120133| |000088448|
</pre>

N.B. to moderators,
How do I enable markup recognition in this forum?
# 3  
Old 03-17-2006
use html code tags [ code ] [ / code ] without spaces
# 4  
Old 03-20-2006
is there a way using ksh?

is there a way using ksh to doing this?
because i don't know how to use perl... Smilie

thanks
# 5  
Old 03-20-2006
Certainly you can also do this with shell commands.
I am convinced someone will come up with a solution soon.
You could for instance do some character counting and use
the cut command with the -c flag and appropriate column numbers
that correspond to the positions where a pipe delimiter should follow.
(man cut).
If you are using Bash or similar Posix shell you could also use the substring
variable expansion (man bash).
e.g.
(not pretty elegant, you shouldn't use a here file but redirect input from your data file)
Code:
while read line; do
printf "%c|%s|%s|%s|%s|%s| |%s|\n" \
        ${line:0:1} ${line:1:3} ${line:4:8} ${line:12:8} \
        ${line:20:5} ${line:25:9} ${line:34:9}
done <<DATA
B065200512312004123111010000061451 000021853
B065200512312004123111020000621907 000417802
B065200512312004123111030000005214 000005861
B065200512312004123111040000120133 000088448
DATA

# 6  
Old 03-20-2006
if you have gawk - you can use gawk's 'FIELDWIDTH' to break up fields.

if you don't have gawk....... you can use 'nawk' iwth the function below to simulate gawk's functionality. Here's the link to USENET's comp.lang.awk original posting

nawk -f debian.awk myFile.txt

debian.awk:
Code:
function setFieldsByWidth(   i,n,FWS,start,copyd0) {
  # Licensed under GPL Peter S Tillier, 2003
  # NB corrupts $0
  copyd0 = $0                             # make copy of $0 to work on
  if (length(FIELDWIDTHS) == 0) {
    print "You need to set the width of the fields that you require" > "/dev/stderr"
    print "in the variable FIELDWIDTHS (NB: Upper case!)" > "/dev/stderr"
    exit(1)
  }

  if (!match(FIELDWIDTHS,/^[0-9 ]+$/)) {
    print "The variable FIELDWIDTHS must contain digits, separated" > "/dev/stderr"
    print "by spaces." > "/dev/stderr"
    exit(1)
  }

  n = split(FIELDWIDTHS,FWS)

  if (n == 1) {
    print "Warning: FIELDWIDTHS contains only one field width." > "/dev/stderr"
    print "Attempting to continue." > "/dev/stderr"
  }

  start = 1
  for (i=1; i <= n; i++) {
    $i = substr(copyd0,start,FWS[i])
    start = start + FWS[i]
  }
}

#I then call setFieldsByWidth() in my main awk code as follows:

BEGIN {
  #FIELDWIDTHS="7 6 5 4 3 2 1" # for example
  FIELDWIDTHS="1 3 8 8 5 9 1 9" # based on fieldwidth formating taken from sample - vlad
  OFS="|"
}
!/^[  ]*$/ {
  saveDollarZero = $0 # if you want it later
  setFieldsByWidth()
  # now we can manipulate $0, NF and $1 .. $NF as we wish
  print $0 OFS
  next
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print line is values between two fields in separate file

I am trying to use awk to find all the $3 values in file2 that are between $2 and $3 in file1. If a value in $3 of file2 is between the file1 fields then it is printed along with the $6 value in file1. Both file1 and file2 are tab-delimited as well as the desired output. If there is nothing to... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Insert values into a file 0 as per the date wise

Hi The file contains 12 months of date and less than 12 months of data I want to display if date filed less than 12 months of data I want to insert a value amount 1 to amount4 0 and dates as well. 12345|Date|cntry|amount1|amount2|amount3|amoun4... (2 Replies)
Discussion started by: jagu
2 Replies

3. Shell Programming and Scripting

Insert empty columns inside a pipe delimited file

Hi All , I have pipe delimiter file with 11 columns . I need to insert 4 empty columns after column 10 . and After 11 column I need to insert a column which is having the same value for all the rows . My file 1|2|3|4|5|6|7|8|9|10|11 New file ... (11 Replies)
Discussion started by: Hypesslearner
11 Replies

4. Shell Programming and Scripting

Find for line with not null values at nth place in pipe delimited file

Hi, I am trying to find the lines in a pipe delimited file where 11th column has not null values. Any help is appreciated. Need help asap please. thanks in advance. (3 Replies)
Discussion started by: manikms
3 Replies

5. Shell Programming and Scripting

insert dummy values in a file

Hey guys, i have a file a.txt and i need to insert a dummy data into it as per the below pattern.. bash: cat a.txt 1234,34 3434,45 4545,56 3434,56 Now i need the file in the below pattern 1234,34,a0001,C_01 3434,45,a0002,C_02 4545,56,a0003,C_03 3434,56,a0004,C_04 here the count of... (3 Replies)
Discussion started by: jaituteja
3 Replies

6. Shell Programming and Scripting

how to Insert values in multiple lines(records) within a pipe delimited text file in specific cols

this is Korn shell unix. The scenario is I have a pipe delimited text file which needs to be customized. say for example,I have a pipe delimited text file with 15 columns(| delimited) and 200 rows. currently the 11th and 12th column has null values for all the records(there are other null columns... (4 Replies)
Discussion started by: vasan2815
4 Replies

7. Shell Programming and Scripting

How to insert a sequence number column inside a pipe delimited csv file using shell scripting?

Hi All, I need a shell script which could insert a sequence number column inside a dat file(pipe delimited). I have the dat file similar to the one as shown below.. |A|B|C||D|E |F|G|H||I|J |K|L|M||N|O |P|Q|R||S|T As shown above, the column 4 is currently blank and i need to insert sequence... (5 Replies)
Discussion started by: nithins007
5 Replies

8. Shell Programming and Scripting

Korn shell program to parse CSV text file and insert values into Oracle database

Enclosed is comma separated text file. I need to write a korn shell program that will parse the text file and insert the values into Oracle database. I need to write the korn shell program on Red Hat Enterprise Linux server. Oracle database is 10g. (15 Replies)
Discussion started by: shellguy
15 Replies

9. Shell Programming and Scripting

hw to insert array values sequentially in a file

Hi All :), I am very new to unix. I am requiring ur help in developing shell script for below problem. I have to replace the second field of file with values of array sequentially where first field is ValidateKeepVar <File> UT-ExtractField 1 | &LogEntry &Keep(DatatoValidate)... (3 Replies)
Discussion started by: rohiiit.sharma
3 Replies

10. UNIX for Advanced & Expert Users

insert pipe in the finish of the line

hi, i have a flat file, with lines (records), and fields, and each field is separated by pipe ( | ) : 1|078|012006|3,9 2|078|012006|8692275|4|2|GON3507090 2|078|012006|7655734|9|0|GON3507090 2|078|012006|7572405|5|4|GCR5N07090 what i need is to insert a pipe in the end of the line:... (3 Replies)
Discussion started by: DebianJ
3 Replies
Login or Register to Ask a Question