Unix/Linux Go Back    


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

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-08-2006   -   Original Discussion by rawatds
rawatds's Unix or Linux Image
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 Unix and Linux 05-08-2006   -   Original Discussion by rawatds
matrixmadhan's Unix or Linux Image
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 ? Linux Linux Linux

try this,

Quote:
awk -F"," '{ print "new,", $2,",",$3,",", $4,","}' samplefile
Sponsored Links
    #3  
Old Unix and Linux 05-09-2006   -   Original Discussion by rawatds
rawatds's Unix or Linux Image
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 Unix and Linux 05-09-2006   -   Original Discussion by rawatds
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 13 Times in 13 Posts


Code:
sed -e "s/^[^,]*\(,.*\)/new \1/g" input.txt > output.txt

Sponsored Links
    #5  
Old Unix and Linux 05-09-2006   -   Original Discussion by rawatds
patras's Unix or Linux Image
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 Unix and Linux 05-09-2006   -   Original Discussion by rawatds
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 13 Times in 13 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 Unix and Linux 05-09-2006   -   Original Discussion by rawatds
gauravgoel's Unix or Linux Image
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
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 10: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 03: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 01:42 AM



All times are GMT -4. The time now is 08:41 PM.