multiple cuts syntax problem

Hi All,

I need some help with multiple cut and paste, at the moment I have a shell script that uses the following cuts ( this is just some)

cut -c1-92 WAITING > col1 .....etc etc etc

cut -c93-98 WAITING > col17 # blank_spaces
cut -c99-104 WAITING > col18 # Date
cut -c105 WAITING > col19 # Canceled
cut -c106-109 WAITING > col20 # ????
cut -c111-116 WAITING > col21 # Date
cp col12 col12a
sed -e s/0/"No ASC"/ -e s/2/ASC/ col12a > col1
2bb
awk '{print NF ? $0 : blankrow}' blankrow="No ASC" col12bb > col12a
paste col[1-3] col3a col4 col4a col5 col6a col[7-9] col9a > test1
paste col1[0-1] col12a col12 col1[3-9]> test2
paste col2[0-1] T_formula > test3
paste test* > joined
etc Etc!

but it is too long winded unforunately though this is the only way I know, does anyone know of a quicker way to do this as it takes a very long time to run ...after doing all the cutting it pastes everything back into a newfile called "joined", but it just seems like a hassle and I thought someone out there must know of a better way to achieve the same results(bearing in mind it does some awking and find and replacing into the bargain

thanks in advance

If you post the a part of the actual input file, it makes it easier to associate your script to the input.

Hi sorry about that,

sample below of file....

cat WAITING | wc -l
11924

head -5 WAITING | pg

IG405HC7  1342567SG002534181190725G52 1HP G52202     ADCR                           26090511310797       C292 011198
IG405HCB  4558796SG003527361070252G52 3AW G52378      LA CHECK CYSTOSCOPY           26090572040301       M459
IG405HCB  0724968SG002842631200638PA4 8QN C87700      CHECK CYSTOSCOPY              26090572160198       M459
IG405HCB  0019635SG009338692051223G46 7QN G49200      CHECK CYSTOSCOPY              26090512300797       M459
IG405HCB  0019635SG009383181240814G74 2AN L63315      LA CHECK CYSTOSCOPY           26090572190897       M459

actual script is below....

#! /bin/sh
echo " "
#echo "                         " $STRING;
cut -c1 WAITING > col1          # type
cut -c2-6 WAITING > col2        # location code
cut -c7-10 WAITING > col3       # spec code
cut -c11-17 WAITING > col4      # consultant
cut -c18-27 WAITING > col5      # crn
cut -c28 WAITING > col6        # Male/Female/Unknown
sed -e s/1/Male/ -e s/2/Female/ -e s/0/Unknown/ col6 > col6a
cut -c29-34 WAITING > col7        # date of birth
cut -c35-42 WAITING > col8        # Post code
awk '{print NF ? $0 : blankrow}' blankrow="blank_Pcode" col8 > Col8a
mv Col8a col8
cut -c43 WAITING > col9           # Healthboard
awk '{print NF ? $0 : blankrow}' blankrow="blank_Health" col9 > col9b
mv col9b col9
cut -c44-48 WAITING > col10       # practice code
cut -c54 WAITING > col12        # unknown
cut -c55-84 WAITING > col13     # free text
cut -c85-90 WAITING > col14     # date
cut -c91 WAITING > col15        # single
sed -e s/1/Inpatient/ -e s/7/Daycase/ col15 > col15a
cp col15a col15
cp col15 /u1/excel/Dups
cut -c92 WAITING > col16        # True or Repeat
sed -e s/1/True/ -e s/2/Repeat/ col16 > col16a
cp col16a col16
cut -c93-98 WAITING > col17     # blank_spaces
cut -c99-104 WAITING > col18    # Date
cut -c105 WAITING  > col19    # Canceled
cut -c106-109 WAITING > col20   # ????
cut -c111-116 WAITING > col21   # Date
cp col12 col12a
sed -e s/0/"No ASC"/ -e s/2/ASC/ -e s/4/ASC/ -e s/8/ASC/ -e s/A/ASC/ -e s/ASCSC/ASC/ col12a > col12bb
awk '{print NF ? $0 : blankrow}' blankrow="No ASC" col12bb  > col12a
paste col[1-3] col3a col4 col4a col5 col6a col[7-9] col9a > test1
paste col1[0-1] col12a col12 col1[3-9]> test2
paste col2[0-1] T_formula > test3
paste test*  > joined
cat ELLA | grep "blank_" > /u1/excel/errors.txt
cp joined /u1/excel/Dups
cat T_heading joined > ELLA
echo "\n\n\n\t\t\t ......F I N I S H E D >>>contents in file named (ELLA) "
sleep 3
rm test[1-3] joined col[1-5] col[7-9] col1[0-9]* col2[0-1]

Moderators, could you put his code within the code tags ? Would be much more pleasant for the eyes.

Instead of the external command cut, use the shell builtin to extract the info.

cut -c1 WAITING > col1          # type
cut -c2-6 WAITING > col2        # location code
cut -c7-10 WAITING > col3       # spec code

will change to 

while read line
do
col1=${line:0:1}
col2=${line:1:5}
col3=${line:6:3}
.
.
.
.
# after all processing is done
done < WAITING
cut -c28 WAITING > col6        # Male/Female/Unknown
sed -e s/1/Male/ -e s/2/Female/ -e s/0/Unknown/ col6 > col6a

will change to 

col6={$line:27:1}
if [[ "$col6" == "1" ]] ; then
col6a=Male
elif [[ "$col6" == "2" ]] ; then
col6a=Female
else col6a=Unknown
fi ;

Following the above patterns, you can replace the mv and cp command as
col15=${col15a}

From your awk statements it seems you are identifying blank lines. Change it to a if-else loop.

For paste,

echo "$col1 $col2... " > output.file

Now that you dont have any external files as well, you wouldnt need the rm

vino

Thanks for that Vino, you guys make it look so simple, that sure looks like a more cleaner professional approach, I'll give it a try asap ...many thanks again and for your prompt response, also if you don't mind me asking what type of programming/style is your coding most similar to? as I would like to study it when I get a little more time.

Read the man pages for sh.

Other links.

Linux Shell Scripting Tutorial - A Beginner's handbook and Advanced Bash-Scripting Guide.

vino

Hi,

Sorry to take so long to get back to you, I am finally getting round to implementing the code as you suggested, but right off the bat I get the following message

UX:sh (./Iptestger): ERROR: Bad substitution

code

#! /bin/sh
while read line
do
col1=${line:0:1}
echo $col1
done < WAITING

I dont see any error in that code. Unless WAITING is not a file. You can not put in a variable in that place like that.

Edit. The shell is sh. So it should work. ksh does not have the same construct.

vino

/bin/sh on most (all?) linux distributions is a link to bash.

If you're running on just about any other UNIX type system then sh will not have the functionality described. You need to use bash itself (instead of sh).

Thanks,

I tried the below code in bash, perhaps I don't have bash on my system as I get the following error now

# ls -lt Iptestger WAITING
-rwx------ 1 root other 2118 Oct 31 12:15 Iptestger
-rw-r--r-- 1 root other 1180 Oct 31 10:53 WAITING

# ./Iptestger
UX:sh: ERROR: ./Iptestger: Not found

#! /bin/bash
while read line
do
col1=${line:0:1}
done < WAITING

# head -2 WAITING
IG405HC7 1342567SG002534181190725G52 1HP G52202 ADCR 31100511310797 C292 011198
IG405HCB 4558796SG003527361070252G52 3AW G52378 LA CHECK CYSTOSCOPY 31100572040301 M459

You can check in /usr/local/bin to see if bash is there (you'll have to change the #!/bin/bash to #!/usr/local/bin/bash of course).

If bash is not present on your system then you'll need to get hold of a copy or do it some other way

I just have these

# ls -lt /usr/bin/*sh*
-rwxrwxrwx 1 root other 271 Nov 30 2001 /usr/bin/shtty
lrwxrwxrwx 1 root other 5 Oct 19 1999 /usr/bin/rsh -> remsh
lrwxrwxrwx 1 bin bin 5 Oct 19 1999 /usr/bin/rksh -> ./ksh
lrwxrwxrwx 1 bin bin 13 Oct 19 1999 /usr/bin/restsh -> ../../sbin/sh
lrwxrwxrwx 1 bin bin 13 Oct 19 1999 /usr/bin/jsh -> ../../sbin/sh
lrwxrwxrwx 1 bin bin 13 Oct 19 1999 /usr/bin/sh -> ../../sbin/sh
-rwsr-xr-x 1 root bin 25696 Jun 4 1999 /usr/bin/remsh
-rwxr-xr-x 1 bin bin 80040 Jun 4 1999 /usr/bin/rshd
-rwxr-xr-x 1 bin bin 175948 Jun 4 1999 /usr/bin/ksh
-rwxr-xr-x 1 bin bin 7369 Jun 4 1999 /usr/bin/admrshell
-rwsr-xr-x 1 root sys 38592 Feb 26 1998 /usr/bin/shl
-r-xr-xr-x 1 bin bin 223120 Apr 2 1997 /usr/bin/csh