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
# 8  
Old 02-12-2009
Quote:
Originally Posted by silkiechicken
Sorry, I am probably deficient in explaining my problem. I think the proper way of describing my tabbed file was a "tab-delimited" file.
No, you didn't enclose your data sample in [code] tags.
# 9  
Old 02-12-2009
Quote:
Originally Posted by danmero
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.
# 10  
Old 02-12-2009
Total n00b here. what are [code] tags? The first time I've worked with this stuff was last friday.

I think I got it to do what I wanted though:
awk '{print $1, "\t", length($2)}' file.tab | sort -k 2

My issue now is to get this to be saved and not just outputed.

If I add
awk '{print $1, "\t", length($2)}' proteins.tab | sort -k 2 > new.tab

It doesn't give me a file called new.tab with the sorted list it printed me before.

Thanks again for your patience.

-Diana
# 11  
Old 02-12-2009
What does it give you ?

code tags are used to highlight code or any other input/output data. This makes it easier to read posts. The icon for code tags is #

Last edited by ce9888; 02-12-2009 at 01:43 AM..
# 12  
Old 02-12-2009
I think it got it actually and that it works... my brain just didn't understand the output when I looked at the file size vs the actual output. The SSH shell window just concatenated the previous output with the current one making it look like the new file was concatenated with the input file.

What I didn't get why the new file size is still 700 bites after I replaced much of it with the length instead. If I replaced a 200 character string with just "200", shouldn't the new file be smaller than the original?
# 13  
Old 02-12-2009
It should be smaller. The file input file I used is 48 bytes and the new file is 28. It should be the same for you

Code:
-rw-r--r--  1 Ixany None   48 Feb 11 23:42 info.tab
-rw-r--r--  1 Ixany None   28 Feb 12 00:29 new.tab

# 14  
Old 02-12-2009
Code:
In the future I put my test code in here right

I think I figured out what I was trying to do! I was looking at the wrong file sizes and 700 is the smaller file. The original was 7336 bites. Thanks!!!


Oh... is there a way to override the shell not letting me write over existing files? It tells me:

Code:
test.tab: File exists.

I'd like to just edit a file, rather than take file, do functions on it, put it in a new file, delete old file, and rename new file.

Thanks again!
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