Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old March 25th, 2008, 11:36 AM
Newbie
 
Join Date: Feb 2008
Posts: 13
Default shell scripts relating removing leading characters

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
Reply
  #2  
Old March 26th, 2008, 12:17 PM
ashitpro's Avatar
Expert
 
Join Date: Aug 2007
Posts: 297
Default

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

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

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

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

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

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

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

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles