![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
| Compare & replace contents within a file | kaustubh137 | Shell Programming and Scripting | 2 | 05-27-2008 07:56 AM |
| Replace contents of a file | shiroh_1982 | Shell Programming and Scripting | 2 | 05-25-2006 10:19 AM |
| Creating file contents using contents of another file | ReV | Shell Programming and Scripting | 21 | 02-24-2006 10:25 AM |
| How to a see the contents of my CD drive | BAM | UNIX for Dummies Questions & Answers | 4 | 08-23-2002 08:42 AM |
| How to a see the contents of my CD drive | BAM | UNIX Desktop for Dummies Questions & Answers | 1 | 08-20-2002 11:59 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Using awk and sed to replace contents
So I am working on command line and I have a file that is spaced by tabs like:
one countMe two countMEtoo three COUNTMEthree What I want to do is read in that file, and replace the second column contents with the length of the string in that column. one 7 two 10 three 12 What I wanted to do was use awk to assign a temp variable the length of the string found in the 2nd colum, and then using sed, replace the string in the second column with the length of the string. What I was thinking was: awk '{$temp = length($2)}' info.tab | sed 's/$2/$temp/g' info.tab > info.new Howver... my new file is just a complete copy of the first file? I am not sure how to pass variables around. If there are any suggestions on how to fix it, an inherent length counter in sed, or where to look that would be great. Thanks! |
|
||||
|
Quote:
awk can solve your problem.Code:
awk '{$2=length($2)}1' OFS="\t" file
|
|
||||
|
1. Check the OP requirement:
2. Please use [code] tags. |
|
||||
|
It was not specified in the OP if the output should have tabs or not. The output requiered by the poster doesn't show any kind of tabbing.
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|