Connecting Tech Pros Worldwide Help | Site Map

shell scripts relating removing leading characters

  #1  
Old March 25th, 2008, 11:36 AM
Newbie
 
Join Date: Feb 2008
Posts: 14
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
  #2  
Old March 26th, 2008, 12:17 PM
ashitpro's Avatar
Expert
 
Join Date: Aug 2007
Posts: 388

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.
  #3  
Old March 27th, 2008, 07:50 AM
Newbie
 
Join Date: Feb 2008
Posts: 14

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
  #4  
Old March 27th, 2008, 12:02 PM
ashitpro's Avatar
Expert
 
Join Date: Aug 2007
Posts: 388

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..
  #5  
Old March 28th, 2008, 05:52 AM
Newbie
 
Join Date: Feb 2008
Posts: 14

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
  #6  
Old March 28th, 2008, 08:04 AM
ashitpro's Avatar
Expert
 
Join Date: Aug 2007
Posts: 388

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)
  #7  
Old March 28th, 2008, 11:14 AM
Newbie
 
Join Date: Feb 2008
Posts: 14

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