|
Search Forums:
|
|||||||
| Forums | Register | Forum Rules | Linux and Unix Links | Man Pages | Albums | FAQ | Users | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
thank you. It works.
|
|
#4
|
|||
|
|||
|
Code:
$ ruby -F"\t" -ane 'puts $F[0,4].join("_")+"\t"+ $F[4..-1].join("\t")' file |
| Sponsored Links | |
|
|
#5
|
|||
|
|||
|
thank you. Ruby is good too.
|
| Sponsored Links | |
|
|
#6
|
|||
|
|||
|
Code:
awk '{sub(".*"$4,$1"_"$2"_"$3"_"$4)}1' infileCode:
# 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 | ||
|
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|