The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
ps command - time field nhatch UNIX for Dummies Questions & Answers 1 05-12-2008 11:15 AM
insert EOD if value in Field 1 change jmaskar Shell Programming and Scripting 2 02-05-2008 12:09 PM
How to change field seperator onthetopo Shell Programming and Scripting 2 05-11-2007 12:11 PM
PS Command Field akuma Linux 2 11-08-2005 09:49 AM
exec command and field descriptors.. moxxx68 UNIX for Dummies Questions & Answers 5 12-04-2004 05:58 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-07-2006
Julius Julius is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 4
sed command to change 2nd field

Hi

I am a beginner to sed command, here I have a question about using sed to add a few characters into a token of a string.

For example, I have a file,
sqw:qqq:123124:uiqe
dfd:ccc:12390:dfjis
cde:aaa:21311:dfjsid

and, I want the output to be,
sqw:qqq:123124:uiqe
dfd:*WW*ccc:12390:dfjis
cde:aaa:21311:dfjsid
then result is redirected to a new file.

Looking for dfd as pattern, then add *WW* to its second field by using sed.

I have tried to run below statement, which is working on the terminal but not in the script.
cat file | awk -F: '{print $2}' | sed 's:^:\*WW\*:'

I have read a couple of sed threads at this forum, but I am still not able to solve above question by myself. Pls help me out. Thanks.
  #2 (permalink)  
Old 10-07-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
sed 's/^dfd:/&*WW*/' myFile.txt
  #3 (permalink)  
Old 10-07-2006
Julius Julius is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 4
oh, excellent. Thanks a lot
  #4 (permalink)  
Old 10-08-2006
Julius Julius is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 4
Quote:
Originally Posted by Julius
Hi

sqw:qqq:123124:uiqe
dfd:ccc:12390:dfjis
cde:aaa:21311:dfjsid

and, I want the output to be,
sqw:qqq:123124:uiqe
dfd:*WW*ccc:12390:dfjis
cde:aaa:21311:dfjsid
Hi again, I have one more question here. How can I use sed look the Kth field in the line and then replace it. eg. I want to look up 'cde' then replace the line
cde:aaa:21311:dfjsid
to
cde:aaa:00000:dfjsid
  #5 (permalink)  
Old 10-08-2006
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
in this case the only field you are manipulating is nueric so:

Code:
sed '/^cde/s/[0-9][0-9]*/00000/'
should work.

Having aid that, awk would be a much better tool in this situation:

Code:
awk 'BEGIN { FS=OFS=":" } /^cde/ { $3 = "00000" ; print }'
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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




All times are GMT -4. The time now is 11:39 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0