The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-11-2009
silkiechicken silkiechicken is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 21
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!
  #2 (permalink)  
Old 02-12-2009
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,439
Quote:
Originally Posted by silkiechicken View Post
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 ....
Don't complicate your life for nothing awk can solve your problem.

Code:
awk '{$2=length($2)}1' OFS="\t" file

  #3 (permalink)  
Old 02-12-2009
ce9888 ce9888 is offline
Registered User
  
 

Join Date: Aug 2008
Location: Montreal, Qc, CA
Posts: 98
Or simply

awk '{print $1,length($2)}' info.tab
  #4 (permalink)  
Old 02-12-2009
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,439
Quote:
Originally Posted by ce9888 View Post
awk '{print $1,length($2)}' info.tab
1. Check the OP requirement:
Quote:
Originally Posted by silkiechicken View Post
So I am working on command line and I have a file that is spaced by tabs like:
2. Please use [code] tags.
  #5 (permalink)  
Old 02-12-2009
ce9888 ce9888 is offline
Registered User
  
 

Join Date: Aug 2008
Location: Montreal, Qc, CA
Posts: 98
It does work with tabs


Code:
awk '{print $1,length($2)}' info.tab

  #6 (permalink)  
Old 02-12-2009
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,439
Did you check the output?

Code:
# cat file
one     countMe
two     countMEtoo
three   COUNTMEthree
# awk '{print $1,length($2)}' file
one 7
two 10
three 12
# awk '{$2=length($2)}1' OFS="\t" file
one     7
two     10
three   12

  #7 (permalink)  
Old 02-12-2009
ce9888 ce9888 is offline
Registered User
  
 

Join Date: Aug 2008
Location: Montreal, Qc, CA
Posts: 98
Quote:
Originally Posted by danmero View Post
Did you check the output?

Code:
# cat file
one     countMe
two     countMEtoo
three   COUNTMEthree
# awk '{print $1,length($2)}' file
one 7
two 10
three 12
# awk '{$2=length($2)}1' OFS="\t" file
one     7
two     10
three   12
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:28 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0