cancel
Showing results for 
Search instead for 
Did you mean: 

help in scripting

SOLVED
Go to solution
Ramesh Lanka
Occasional Contributor

help in scripting

hi fundoos
i need some help on scripting
one of my user has the following requirement, can anybody help?
The text file below will be taken as an input to the shell script and do the following

1)
The length of the bill type field (2nd field)should be checked

If the length = 2 then the bill type should be padded with 2 zero???s to the left.
(E.g. : if bill type = 1, then it should be 0001)
If the length = 1 then the bill type should be padded with 3 zero???s to the left.
(E.g. : if bill type = 19, then it should be 0019)

2)
Pad the date field (5th field)with ???. 000??? (dot space and then 000)
(Eg:2003-04-01 09:29:24. 000)
The date field is the 5th column in the text file.


The text file will be always of the same format.
Attached the input file

Regds
10 REPLIES
Massimo Bianchi
Honored Contributor
Solution

Re: help in scripting

Hi,
for the second part, the answer is simple:

cat input| awk -F~ '{ print $1"~"$2"~"$3"~"$4"~"$5". 000"$1"~"$6"~"$7"~"$8"~" }'


When i have time i will work also on the firt part.

HTH,
Massimo



will do the job




Ian Dennison_1
Honored Contributor

Re: help in scripting

cat /tmp/test1 |awk '{var="0000"$2;print $1" "substr(var, length(var-4), 4)}'

Awk statement that prints the second variable as zero filled to 4 characters.

Share and Enjoy! Ian
Building a dumber user
Ian Dennison_1
Honored Contributor

Re: help in scripting

Sorry, missed a bit

cat /tmp/test1 |awk '{var="0000"$2;print $1" "substr(var, length(var)-3, 4)}'


Note the changes, var-3. Ian
Building a dumber user
V.Tamilvanan
Honored Contributor

Re: help in scripting

Hi,
Check this script.This should meet your both requirement.


cat input |awk -F "~" '{printf("%s~%04d~%s~%s~%s. 000~%s~%s~%s\n",$1, $2 , $3, $4, $5, $6, $7, $8 )} '


OR
cat input |awk -F "~" '{printf("%04d~%s. 000\n", $2, $5)} '



HTH
V. V. Ravi Kumar_1
Respected Contributor

Re: help in scripting

Hi,
You can do both the things like this

cat | awk 'length($2) == 1 {print $1,":","000"$2,":",$3,":",$4,":",$5". 000"} length($2) == 2 {print $1,":","00"$2,":",$3,":",$4,":",$5". 000"}'> /tmp/tempfile


Regards
Never Say No
john korterman
Honored Contributor

Re: help in scripting

Hi,
One approach. Try running the attached script like this:
# attached.sh

Slow, but simple to correct.

regards,
John K.
it would be nice if you always got a second chance
H.Merijn Brand (procura
Honored Contributor

Re: help in scripting

l1:/tmp 108 > perl -naF\~ -e'$,="~";$F[1]=sprintf"%04d",$F[1];$F[4].=". 000";print@F' xx.txt
1000132873~0001~01~1270~2003-04-01 09:29:24. 000~ NARASIMHA REDDY B V~HYDERABAD~HYD000
1000388231~0001~01~1736~2003-04-01 09:34:13. 000~ PRAJA SAKTI TELUGU DAILY~HYDERABAD~HYD000
1000427496~0001~01~2300~2003-04-01 09:37:10. 000~ NVS NAIDU~HYDERABAD~HYD000
1000421963~0001~01~1060~2003-04-01 09:38:17. 000~ VVR CHOWDARY~HYDERABAD~HYD000
1000299151~0001~01~1760~2003-04-01 09:38:59. 000~ . AJAY DESHPANDE~HYDERABAD~HYD000
1000214434~0002~01~3570~2003-04-01 09:40:44. 000~ P VENKATA RAMA REDDY~HYDERABAD~HYD000
1000416477~0001~01~1600~2003-04-01 09:43:41. 000~ VARMA BALAVENKATA MANTHENA~VISAKHAPATNAM~FTFVIZ
1000129441~0001~01~250~2003-04-01 09:52:44. 000~ Dr.R.C.MATHUR~HYDERABAD~HYD000
1000109493~0001~01~4974~2003-04-01 09:53:05. 000~ M.RAMAKRISHNA RAJU~VIJAYAWADA~FTFVIJ
1000431318~0001~01~1100~2003-04-01 09:58:08. 000~ LAKSHMI PIRIMITI~VISAKHAPATNAM~FTFVIZ
1000117363~0002~01~1012~2003-04-01 09:58:56. 000~ GUNDRA NAGARAJU~SECUNDERABAD~S11000
1000373683~0002~01~749~2003-04-01 10:02:38. 000~ KAJA VIJAY SEKHAR~SECUNDERABAD~S11000
1000364205~0001~01~2300~2003-04-01 10:04:15. 000~ JAGANNADHA RAO SAKISA~VISAKHAPATNAM~FTFVIZ
1000453189~0001~01~540~2003-04-01 10:05:36. 000~ LAKSHMANA RAO KANTIBOTHU~VISAKHAPATNAM~FTFVIZ
1000162369~0001~01~1300~2003-04-01 10:05:36. 000~ POTLURI RAJ KUMAR~VIJAYAWADA~FTFVIJ
1000125600~0001~01~1765~2003-04-01 10:06:25. 000~ DR.N.SUBBA RAO~VIJAYAWADA~FTFVIJ
1000129590~0004~01~825~2003-04-01 10:08:16. 000~ DR G LAKSHMI~HYDERABAD~HYD000
1000271829~0001~01~1016~2003-04-01 10:09:53. 000~ NV RAMANA REDDY~HYDERABAD~H13000
1000444683~0002~01~1817~2003-04-01 10:12:04. 000~ Y MURALI KRISHNA~HYDERABAD~H13000
1000154221~0001~01~1180~2003-04-01 10:12:06. 000~ G PULLA REDDY~HYDERABAD~HYD000
1000243766~0001~01~1100~2003-04-01 10:12:39. 000~ SAJJA CHANDRA MOHAN~VIJAYAWADA~FTFVIJ
1000434884~0001~01~3230~2003-04-01 10:14:02. 000~ J BABU~VIJAYAWADA~FTFVIJ
1000454701~0001~01~498~2003-04-01 10:14:50. 000~ K HARIHARAN~VIJAYAWADA~FTFVIJ
1000412340~0001~01~4520~2003-04-01 10:16:14. 000~ SN CONSTRUCTIONS~HYDERABAD~HYD000
1000189588~0001~01~975~2003-04-01 10:18:53. 000~ M TULASIDAS~VIJAYAWADA~FTFVIJ
1000277301~0001~01~1107~2003-04-01 10:18:54. 000~ SURESH KUMAR JAIN~VIJAYAWADA~V02000
1000184404~0002~01~920~2003-04-01 10:19:51. 000~ M VISWESWARAIAH -~HYDERABAD~H13000
1000229466~0001~01~900~2003-04-01 10:19:58. 000~ P MUNISHEKAR REDDY~HYDERABAD~HYD120
1000307857~0001~01~974~2003-04-01 10:21:04. 000~ T.D.REDDY GRAPE GARDEN~SECUNDERABAD~S11000
1000117843~0001~01~2576~2003-04-01 10:21:18. 000~ N.VISHNU V REDDY~HYDERABAD~H13000
1000424059~0019~01~1546.5~2003-04-01 10:22:32. 000~VENKATANARASIMHA~HYDERABAD~HYD000
1000229427~0001~01~900~2003-04-01 10:23:49. 000~ NEKKALAPUDI ANJAYYA~VIJAYAWADA~FTFVIJ
1000139194~0001~01~1430~2003-04-01 10:24:43. 000~ J.PAPARAO~VIJAYAWADA~FTFVIJ
1000352619~0001~01~1443~2003-04-01 10:26:45. 000~ N SRIHARI RAO~GUNTUR~GUN050
1000270169~0001~01~2940~2003-04-01 10:26:47. 000~ M SAMBI REDDY~VIJAYAWADA~FTFVIJ
l1:/tmp 109 >

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Ramesh Lanka
Occasional Contributor

Re: help in scripting

Hi all
thank u all for ur inputs,

i have written a script like this:

echo "Enter the input file name:\c"
read filein
awk -F~ '{var="0000"$2;print $1"~"substr(var,length(var)-3,4)"~"$3"~"$4"~"$5". 000~"$6"~"$7"~"$8}' $filein >$filein.out
echo "\nThe modified output file name is:$filein.out"

Tamil:
do u know me?

Regds
M.Kantharuban /HCL
Bill Hassell
Honored Contributor

Re: help in scripting

Actually, there is no need to use awk and make specific tests for the field length. Use the shell's built-in typeset feature which will provide the leading zero padding automatically:

typeset -Z4 FIELD2
cat your_file | while read FIELD1 FIELD2 FIELD4 FIELD5 RESTOFFIELDS
do
print "$FIELD1 $FIELD2 $FIELD3 $FIELD4 $FIELD5 .000 $RESTOFFIELDS"
done

The typeset feature performs a lot of formatting automatically without lengthy tests.


Bill Hassell, sysadmin
V.Tamilvanan
Honored Contributor

Re: help in scripting

Hi Kantharuban/ HCL,
I remember that I met you somewhere on HP-UX training at HYD. Can you drop a mail at vtamilvanan@yahoo.com as this a public forum.