Computing.Net > Forums > Unix > Add tab at a line position

Add tab at a line position

Reply to Message Icon

Original Message
Name: Kathy
Date: August 10, 2002 at 00:31:00 Pacific
Subject: Add tab at a line position
Comment:


Hi,

Need help to write a Unix script to peform split of one record to 2 columns with a tab seperator. Pls advise.

eg:
Input record size:330 chars
ouput : 1 to 225 chars + tab + the rest.


Report Offensive Message For Removal

Response Number 1
Name: David Perry
Date: August 12, 2002 at 04:25:03 Pacific
Subject: Add tab at a line position
Reply: (edit)

Look at "awk"

#!/bin/sh
var="1234567890 abcdefg";
var1=`echo $var | awk '{ print $1 }'`
var2=`echo $var | awk '{ print $2 }'`
echo "$var1 | $var2"


Report Offensive Follow Up For Removal

Response Number 2
Name: Frank
Date: August 12, 2002 at 07:27:13 Pacific
Subject: Add tab at a line position
Reply: (edit)

Hi Kathy,

hope that I got the question right:
if [ $# -eq 2 ]
then
typeset -i bis=$2+1
cat $1 | while read rec
do
part1=`echo $rec|cut -b1-$2`
part2=`echo $rec|cut -b${bis}-`
echo "${part1}\t${part2}"
done
else
echo $0 filename position 1>&2
fi

This will give each record in a file seperatet by with a space at position $2.

If your record includes "Returns" it won't work.
Then you can easily work with the split -b255.


No RISK no fun
Frank


Report Offensive Follow Up For Removal

Response Number 3
Name: Jerry Lemieux
Date: August 15, 2002 at 15:52:38 Pacific
Subject: Add tab at a line position
Reply: (edit)

Given a single record of 330 characters in length, consisting of a single field (no field separator), you can split it into a single record with 2 fields, tab separated, as follows:

#!/bin/ksh

exec 3< junk.data.file

while read -u3 Line
do
PartOne=$(print $Line | cut -c1-255)
PartTwo=$(print $Line | cut -c256-330)

print "$PartOne\t$PartTwo"

done


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Add tab at a line position

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software