Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Search Forums:



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-03-2011
Registered User
 

Join Date: Sep 2008
Posts: 71
Thanks: 6
Thanked 1 Time in 1 Post
find and replace

I have a tab-delimited inFile:

Code:
cat inFile
A    B    C    D    E    F
1    2    3    4    5    6
a    b    c    d    e    f

I would like to replace the first 3 tabs in each row with underscore to get outFile:

Code:
A_B_C_D    E    F
1_2_3_4    5    6
a_b_c_d    e    f

how can I modify the following code to limit the replacement to only the first 3 tabs in each row?

Code:
awk '{ gsub(/\t/,"_"); print }' inFile > outFile

cat outFile
A_B_C_D_E_F
1_2_3_4_5_6
a_b_c_d_e_f

Sponsored Links
    #2  
Old 04-03-2011
Moderator
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 12,192
Thanks: 233
Thanked 1,707 Times in 1,635 Posts
How about just printing them?


Code:
awk '{ printf("%s_%s_%s_%s", $1, $2, $3, $4);
for(n=5; n<=NF; n++) printf("\t%s", $n);
printf("\n"); }'

Sponsored Links
    #3  
Old 04-03-2011
Registered User
 

Join Date: Sep 2008
Posts: 71
Thanks: 6
Thanked 1 Time in 1 Post
thank you. It works.
    #4  
Old 04-03-2011
Resident Ruby Wrangler
 

Join Date: Dec 2009
Posts: 575
Thanks: 2
Thanked 79 Times in 74 Posts

Code:
$ ruby -F"\t" -ane 'puts $F[0,4].join("_")+"\t"+ $F[4..-1].join("\t")' file

Sponsored Links
    #5  
Old 04-04-2011
Registered User
 

Join Date: Sep 2008
Posts: 71
Thanks: 6
Thanked 1 Time in 1 Post
thank you. Ruby is good too.
Sponsored Links
    #6  
Old 04-04-2011
ctsgnb ctsgnb is offline Forum Advisor  
Registered User
 

Join Date: Oct 2010
Location: France
Posts: 2,529
Thanks: 64
Thanked 521 Times in 500 Posts

Code:
awk '{sub(".*"$4,$1"_"$2"_"$3"_"$4)}1' infile


Code:
# cat tst
A    B    C    D    E    F
1    2    3    4    5    6
a    b    c    d    e    f
# nawk '{sub(".*"$4,$1"_"$2"_"$3"_"$4)}1' tst
A_B_C_D    E    F
1_2_3_4    5    6
a_b_c_d    e    f
#

Sponsored Links
Closed 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
Find and replace sandy1028 Shell Programming and Scripting 1 07-15-2010 08:46 AM
Help with find and replace in XML aixjadoo Shell Programming and Scripting 5 12-13-2009 07:31 PM
Find and Replace in ksh. gauravsunil Shell Programming and Scripting 1 12-05-2008 06:55 AM
find and replace valhutch UNIX for Dummies Questions & Answers 4 07-29-2006 05:20 PM
Find & Replace gagansharma Shell Programming and Scripting 3 11-27-2001 03:17 PM



All times are GMT -4. The time now is 03:34 AM.