×
UNIX.COM Login
Username:
Password:  
Show Password






👤


Shell Programming and Scripting

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

replacing one filed at run time in UNix

👤 Login to reply
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-08-2006
rawatds rawatds is offline
Registered User
 
Join Date: Dec 2004
Last Activity: 27 September 2008, 9:48 PM EDT
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
replacing one filed at run time in UNix

Hi All,
My requirement is :
My input file is :

2 , 56, 23, 43,
1 , 32 , 5, 6,
10 , 2, 3, 4,

Now i want the output as
new, 56, 23, 43,
new,32 , 5, 6,
new , 2, 3, 4,

Please help me

Rawat
Sponsored Links
    #2  
Old 05-08-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
 
Join Date: Mar 2005
Last Activity: 10 January 2017, 8:10 AM EST
Location: classification algos
Posts: 3,215
Thanks: 19
Thanked 31 Times in 28 Posts
what have you tried so far ?

try this,

Quote:
awk -F"," '{ print "new,", $2,",",$3,",", $4,","}' samplefile
Sponsored Links
    #3  
Old 05-09-2006
rawatds rawatds is offline
Registered User
 
Join Date: Dec 2004
Last Activity: 27 September 2008, 9:48 PM EDT
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Hi
Thanks for the prompt reply.
Actuaaly i have a file with around 115 fileds in each row.
So with your method it will work but will be clumsy to write such a big code
.
Any simpler code !!

Thanks a lot
Rawat
    #4  
Old 05-09-2006
vino's Unix or Linux Image
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 8 February 2016, 5:12 PM EST
Location: Sydney, Down Under
Posts: 2,848
Thanks: 0
Thanked 14 Times in 14 Posts
Code:
sed -e "s/^[^,]*\(,.*\)/new \1/g" input.txt > output.txt

Sponsored Links
    #5  
Old 05-09-2006
patras patras is offline
Banned
 
Join Date: May 2006
Last Activity: 2 October 2006, 4:13 AM EDT
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Vino,

Can you pls explain your command as I'm trying to learn sed too and it'll be helpful for me.

thanks
Patras
Sponsored Links
    #6  
Old 05-09-2006
vino's Unix or Linux Image
vino vino is offline Forum Advisor  
Supporter (in vino veritas)
 
Join Date: Feb 2005
Last Activity: 8 February 2016, 5:12 PM EST
Location: Sydney, Down Under
Posts: 2,848
Thanks: 0
Thanked 14 Times in 14 Posts
Code:
sed -e "s/^[^,]*\(,.*\)/new \1/g" input.txt > output.txt

[^,]* means a character-set containing all those characters which are not , (comma)
\(,.*\) is capture a string which matches the pattern , (comma) followed by anything.

Then replace the character-set with *new* followed by the pattern.

Write everything into output.txt
Sponsored Links
    #7  
Old 05-09-2006
gauravgoel gauravgoel is offline
Registered User
 
Join Date: Dec 2005
Last Activity: 11 September 2015, 1:02 AM EDT
Location: India
Posts: 218
Thanks: 0
Thanked 1 Time in 1 Post
with awk for any no. of fields

Quote:
awk -F"," '$1="new"{print}' samplefile
Gaurav
Sponsored Links
👤 Login to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to get time duration between two human readable time stamp in Unix? ford99 Shell Programming and Scripting 3 07-19-2011 09:52 AM
How To Provide Time Sync Using Nts-150 Time Server On Unix Network? pesty UNIX for Advanced & Expert Users 2 03-22-2007 02:20 AM
Help needed - Replacing all date & time occurrences in a file with a string using Sed Hema_M Shell Programming and Scripting 4 11-17-2005 12:42 AM



All times are GMT -4. The time now is 09:10 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.