Solved

Bash shell script - Read file and add deliminator.

Posted on 2008-10-17
11
933 Views
Last Modified: 2013-12-26
I have a file (.dat) with fields that are not seperated by any deliminators, but by position and need to convert it to a csv file.  I need a bash script to read the .dat file, find the fields based on positions and write to a new csv file with the deliminator.

For example, my dat file contains
11222333344444
99888777766666
and need to convert it to:
11,222,3333,44444
99,888,7777,66666

Thanks in advance.
0
Comment
Question by:knc26
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 2

Expert Comment

by:tdiops
ID: 22742779

# change the numbers to represent the number of columns in each field
# add a new field delimiter, '\(.\{n\}\)' for more fields if necessary
 
mysplit() {
 sed 's/\(.\{2\}\)\(.\{3\}\)\(.\{4\}\)\(.\{5\}\)/\1,\2,\3,\4/'
}
 
sampledata() {
cat << %%%
11222333344444
99888777766666
%%%
}
 
sampledata | mysplit

Open in new window

0
 
LVL 40

Accepted Solution

by:
omarfarid earned 250 total points
ID: 22742867
try

cat file | while read line
do
   F1=`echo $line | cut -c 1-2`
   F2=`echo $line | cut -c 3-5`
   F3=`echo $line | cut -c 6-9`
   F4=`echo $line | cut -c 10-14`
   echo "$F1, $F2, $F3, $F4" >> newfile
done
0
 

Author Comment

by:knc26
ID: 22744600
I'm having a problem with the cut command.  When there is a field with more than one byte of space, cut command only includes one space.

For example
do
   F1=`echo $line | cut -c 1-2`
   F2=`echo $line | cut -c 3-5`
   F3=`echo $line | cut -c 6-9`
   F4=`echo $line | cut -c 10-14`
   echo "$F1, $F2, $F3, $F4" >> newfile
done

on: 112223  344444
gives me:
11,222,3 3,44444 (one blank) instead of 11,222,3  3,44444 (two blanks)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:Tintin
ID: 22744771
To use the cut solution, you need to ensure you have quotes around $line, ie:
#!/bin/sh
while read line
do
   F1=`echo "$line" | cut -c 1-2`
   F2=`echo "$line" | cut -c 3-5`
   F3=`echo "$line" | cut -c 6-9`
   F4=`echo "$line" | cut -c 10-14`
   echo "$F1,$F2,$F3,$F4" >> newfile
done <file

Open in new window

0
 
LVL 48

Expert Comment

by:Tintin
ID: 22744798
Please note that the while loop and cut solution is extremely slow.  The following sed solution is much shorter and many, many times quicker


sed "s/\(..\)\(...\)\(....\)\(.....\)/\1,\2,\3,\4/" file >newfile

Open in new window

0
 
LVL 2

Expert Comment

by:tdiops
ID: 22744853
Tintin: Check out the first response.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 22744874
tdiops, I did see your suggestion.  Note that the extended regexs aren't supported by all sed versions.  My solution will work with any sed version.
0
 

Author Comment

by:knc26
ID: 22744893
I think the cat command is removing the extra continous spaces.  Prior to performing the cut, I'm echoing each line and can see that multiple spaces within each record is being reduced to only one space.

Is this a behavior with the cat command?  I didn't see any cat option that would prevent this.  Is there another command that i can use instead of cat?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 22744938
knc26.  I've already given you the reason and a cut solution to deal with multiple spaces.  Please re-read my second last post.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 22744944
How big are the files you are processing?  Please take note of my other post regarding how slow a while loop and cut is.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 22745348
Here's a *very* graphic example as to how much quicker sed is.  Using just a small sample file (10000 lines), the while loop/cut solution takes 70 seconds to run compared to 0.02 of a second for the sed solution.  

That means the sed solution is 3500 times quicker!!!

$ cat script
#!/bin/sh
while read line
do
   F1=`echo "$line" | cut -c 1-2`
   F2=`echo "$line" | cut -c 3-5`
   F3=`echo "$line" | cut -c 6-9`
   F4=`echo "$line" | cut -c 10-14`
   echo "$F1,$F2,$F3,$F4" >> newfile
done <file


$ wc -l file
10000 file

$ time ./script
real    1m10.338s
user    0m8.740s
sys     0m41.060s

$ time sed "s/\(..\)\(...\)\(....\)\(.....\)/\1,\2,\3,\4/" file >newfile
real    0m0.022s
user    0m0.021s
sys     0m0.001s
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Linux Scripting 3 188
Awk & Pythagoras or SQL - applying csv file data to the problem Part 2 continuation 14 74
AWK: Pythagoras script 6 56
Shell Script- gzip 5 85
My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question