Using awk and sed to replace contents


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk and sed to replace contents
# 1  
Old 02-11-2009
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  
Old 02-12-2009
Quote:
Originally Posted by silkiechicken
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 Smilie awk can solve your problem.
Code:
awk '{$2=length($2)}1' OFS="\t" file

# 3  
Old 02-12-2009
Or simply

awk '{print $1,length($2)}' info.tab
# 4  
Old 02-12-2009
Quote:
Originally Posted by ce9888
awk '{print $1,length($2)}' info.tab
1. Check the OP requirement:
Quote:
Originally Posted by silkiechicken
So I am working on command line and I have a file that is spaced by tabs like:
2. Please use [code] tags.
# 5  
Old 02-12-2009
It does work with tabs

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

# 6  
Old 02-12-2009
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  
Old 02-12-2009
Sorry, I am probably deficient in explaining my problem. I think the proper way of describing my tabbed file was a "tab-delimited" file. I will work with the suggestions to see if I can't figure out the problem. Thanks a ton!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace using sed or awk

Hi, Need a help to replace a word if a pattern is found between the delimiters preferably using SED or AWK. below is the sample file that iam dealing with, need to match pattern 'application' if found replace the whole word between the delimiters and also print the lines that don't match.... (1 Reply)
Discussion started by: tech_frk
1 Replies

2. Emergency UNIX and Linux Support

sed replace file contents by reading from another file

Hello, My input file1 is like this by tab-delimited chr1 mm10_knownGene stop_codon 3216022 3216024 0.000000 - . gene_id "uc007aeu.1"; transcript_id "uc007aeu.1"; chr1 mm10_knownGene CDS 3216025 3216968 0.000000 - 2 gene_id "uc007aeu.1"; transcript_id "uc007aeu.1"; ... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

3. Debian

Using awk and sed to replace text

Good Day Every one I have a problem finding and replacing text in some large files that will take a long time to manually edit. Example text file looks like this #Example Large Text File unix linux dos squid bind dance bike car plane What im trying to do is to edit all the... (4 Replies)
Discussion started by: linuxjunkie
4 Replies

4. Shell Programming and Scripting

sed - Replace string with file contents

Hello, I have two files: file1 and file2 file1 has the following info: --- host: "localhost" port: 3000 reporter_type: "zookeeper" zk_hosts: - "localhost:2181" file2 contains an IP address (1.1.1.1) What I want to do is replace localhost with 1.1.1.1, so that the... (4 Replies)
Discussion started by: Jay Kah
4 Replies

5. Shell Programming and Scripting

Replace second match+awk/sed

I have a text file that looks like this: ----------------------------------------- sta WP00 time 10/23/2013 20:10:17 sensor trillium_240_2 0 583 add close sensor trillium_240_2 10/23/2013 20:10:17 sensor trillium_120 0 279 add close sensor trillium_120 10/23/2013 20:10:35... (11 Replies)
Discussion started by: klane
11 Replies

6. Shell Programming and Scripting

sed to replace specific positions on line with file contents

Hi, I am trying to use an awk command to replace specific character positions on a line beginning with 80 with contents of another file. The line beginning with 80 in file1 is as follows: I want to replace the 000000000178800 (positions 34 - 49) on this file with the contents of... (2 Replies)
Discussion started by: nwalsh88
2 Replies

7. Shell Programming and Scripting

HELP with awk or sed. Need to replace all IP address by 2

Hi, In a file, I have several time <IP>232.0.1.164</IP> ... <IP>232.0.1.135</IP> I need to replace all the random IP addresses , by 239.0.0.1 and 239.0.0.2 , alternatively. I try this grep "<IP>" tsp.xml | awk '{if(NR % 2)print $0}' | cut -d"<" -f2 | cut -d">" -f2 ... (3 Replies)
Discussion started by: FredMo
3 Replies

8. UNIX for Dummies Questions & Answers

replace 0.00 with awk/sed

Hi I have a problem when i use awk or sed to replace characters in file. For example when I want to replace line like this : 00000O120100512 1.70 1.59 0.00 +7.280 I want to get a new line : 0000000O120100512 1.70 1.59 13.56 +7.280 In ksh : awk... (1 Reply)
Discussion started by: Artur
1 Replies

9. Shell Programming and Scripting

Sed help to replace and then awk

Sed help echo "(200 rows affected)" | sed -e '/\(//p' | sed -e '/\)//p' | awk '{print $1}' I want output as "200" Please help me correct (2 Replies)
Discussion started by: pinnacle
2 Replies

10. Shell Programming and Scripting

Replace tr/sed with awk

There is a service that runs that we call multi-streaming that calls a shell script multiple times simultaneously. In this shell script is the following line: tr '\r' '\n' < $POLLFILE.OUT | sed '/0000000000000016000A/d' > $POLLFILE When I run this manually it produces the desired results, but... (6 Replies)
Discussion started by: philplasma
6 Replies
Login or Register to Ask a Question