If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > sed......the stream editing....or awk maybe

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-23-04, 12:24
estumator estumator is offline
Registered User
 
Join Date: Feb 2004
Posts: 6
sed......the stream editing....or awk maybe

encounter problem again -_-b
i want to insert some character into specific position in a line, for example
insert a char after the 4th char, or insert a char in front of the 2nd field.etc.........
how can these be done then......
thanks for ur attention.......
Reply With Quote
  #2 (permalink)  
Old 02-23-04, 12:53
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Code:
Line="Txt: for test"

#
# Insert string after the 4th char
#

echo "$Line" | \
sed 's/^\(.\{4\}\)/\1(sed after 4th char)/'

echo "$Line" | \
awk '{print substr($0,1,4) "(awk after 4th char)" substr($0,5)}'

#
# Insert string before field 2
#

echo "$Line" | \
awk '{$2 = "(awk before field2)" $2;print $0}'
and the result

Quote:
Txt:(sed after 4th char) for test
Txt:(awk after 4th char) for test
Txt: (awk before field2)for test
__________________
Jean-Pierre.
Reply With Quote
  #3 (permalink)  
Old 02-23-04, 13:25
estumator estumator is offline
Registered User
 
Join Date: Feb 2004
Posts: 6
Cool

thanks
Reply With Quote
  #4 (permalink)  
Old 02-24-04, 15:13
julytli01 julytli01 is offline
Registered User
 
Join Date: Feb 2004
Posts: 7
How to do this?

I have a bunch of codes (like 80), every code has several subroutines. The task I was posed is to add C preprocessing to each subroutine (add #ifdef before each subroutine). I think this can be done using awk script. Could anybody help me on this? Please send email to julytli01@yahoo.com, thanks a lot!
Quote:
Originally posted by aigles
Code:
Line="Txt: for test"

#
# Insert string after the 4th char
#

echo "$Line" | \
sed 's/^\(.\{4\}\)/\1(sed after 4th char)/'

echo "$Line" | \
awk '{print substr($0,1,4) "(awk after 4th char)" substr($0,5)}'

#
# Insert string before field 2
#

echo "$Line" | \
awk '{$2 = "(awk before field2)" $2;print $0}'
and the result
Reply With Quote
  #5 (permalink)  
Old 02-24-04, 15:19
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Do you want to add and '#ifdef' statement only ?
You do you identify a subroutine ?

Give us an example of input code and requested result.
__________________
Jean-Pierre.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On