Connecting Tech Pros Worldwide Help | Site Map

shell scripts relating removing leading characters

Newbie
 
Join Date: Feb 2008
Posts: 14
#1: Mar 25 '08
hello,
I have some problems regarding trimming the leading character

awk -F"|" '{print $1"|"$2"|"$3"|3|"$4}' /home/postgres/$File-9898.txt.edt > /home/postgres/edt1/$File-9898.txt.edt1
After getting the file $File-9898.txt.edt1, i am in need to limit the $4 parameter to 14 length so i want to trim everything if that extends more than 14.
so, how can i proceed,

with regards
creeds
ashitpro's Avatar
Expert
 
Join Date: Aug 2007
Posts: 389
#2: Mar 26 '08

re: shell scripts relating removing leading characters


Quote:

Originally Posted by creeds

hello,
I have some problems regarding trimming the leading character

awk -F"|" '{print $1"|"$2"|"$3"|3|"$4}' /home/postgres/$File-9898.txt.edt > /home/postgres/edt1/$File-9898.txt.edt1
After getting the file $File-9898.txt.edt1, i am in need to limit the $4 parameter to 14 length so i want to trim everything if that extends more than 14.
so, how can i proceed,

with regards
creeds


use printf instead of print with awk,and specify the format specifier for last field.
Somthing like below:

awk -F"|" '{printf "%s|%s|%s|3|%.14s\n" ,$1,$2,$3,$4}' /home/postgres/$File-9898.txt.edt > /home/postgres/edt1/$File-9898.txt.edt1

everything will be redirected to $File-9898.txt.edt1 file.
Pay attention for two things.
1: ".14" number, which is field width specifier.
2: \n at the end of string..I am not sure about this..check out yourself.
Newbie
 
Join Date: Feb 2008
Posts: 14
#3: Mar 27 '08

re: shell scripts relating removing leading characters


[quote=ashitpro]
thanks for the solution..
yeah i also cud dothat using substr(), so that i can count 14, but if i want to cout 14 from the last , what patterns i have to apply ...
how to modify the following lines which limits 4th file dto first 14th limit
awk -F"|" '{printf "%s|%.14s|%s|3|%s\n" ,$1,$2,$3,$4}'

with regards,
Creeds
ashitpro's Avatar
Expert
 
Join Date: Aug 2007
Posts: 389
#4: Mar 27 '08

re: shell scripts relating removing leading characters


[quote=creeds]
Quote:

Originally Posted by ashitpro

thanks for the solution..
yeah i also cud dothat using substr(), so that i can count 14, but if i want to cout 14 from the last , what patterns i have to apply ...
how to modify the following lines which limits 4th file dto first 14th limit
awk -F"|" '{printf "%s|%.14s|%s|3|%s\n" ,$1,$2,$3,$4}'

with regards,
Creeds

Thats what I said in my previous post..
Put the '.14' between '%' and 's'.
same as you've done for printing $2
So modified commandshould look like:

awk -F"|" '{printf "%s|%.14s|%s|3|%.14s\n" ,$1,$2,$3,$4}'

Now your last field will have only 14 characters..
Newbie
 
Join Date: Feb 2008
Posts: 14
#5: Mar 28 '08

re: shell scripts relating removing leading characters


[quote=ashitpro][quote=creeds]
sorry yar,
i got ur answer and the pattern in the very first reply so no problem with any field being limited to 13 or 14 wotsever...
its understandable,
but inthe last post i wanted to count 14 from the last,
i mean +009779841366128 i want to remove + 00 as i limit my parameter to 14 from last....so how to proceed,
just sort my problem out man,
with regards,
Suman
ashitpro's Avatar
Expert
 
Join Date: Aug 2007
Posts: 389
#6: Mar 28 '08

re: shell scripts relating removing leading characters


[quote=creeds][quote=ashitpro]
Quote:

Originally Posted by creeds

sorry yar,
i got ur answer and the pattern in the very first reply so no problem with any field being limited to 13 or 14 wotsever...
its understandable,
but inthe last post i wanted to count 14 from the last,
i mean +009779841366128 i want to remove + 00 as i limit my parameter to 14 from last....so how to proceed,
just sort my problem out man,
with regards,
Suman

check out this, modify according to you

awk -F"|" '{printf "%s|%.14s|%s|3|%s\n" ,$1,$2,$3,substr($4,length($4)-13,length($4))}'

Note:If you substract 13 it will print 13+1 characters in last field(ofcourse counting from last character)
Newbie
 
Join Date: Feb 2008
Posts: 14
#7: Mar 28 '08

re: shell scripts relating removing leading characters


[quote=ashitpro][quote=creeds][quote=ashitpro]

hey buddy
thanks that was clear n clever logic
regards,
creeeds
Reply