How to strip out common terms in string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to strip out common terms in string
# 8  
Old 08-04-2008
Hi.

I would use a modular approach with a pipeline:

1) transpose (perhaps with an awk script)

2) eliminate rows of all zeros (egrep could be useful)

3) transpose

Best wishes ... cheers, drl
# 9  
Old 08-04-2008
Hi.,

Which 0 in orange line do you mean.

The example of data is :-

0, 0, 0, 0, 1, 2, 0, 4, 5, 6, 7,foo
0, 0, 0, 0, 1, 4, 0, 5, 5, 5, 5,foo1
0, 0, 6, 0, 1, 6, 0, 6, 1, 2, 3,orange
etc...

I wanted to remove the 0 which occur on the same COLUMNS of foo,foo1 and orange in this case.

Desired output is:-

0,1,2,4,5,6,7,foo
0,1,4,5,5,5,5,foo1
6,1,6,6,1,2,3,orange
# 10  
Old 08-05-2008
Ahh, it makes perfect sense now.

Code:
awk -F'[ ,]+' -vOFS=, '
        { for (i=1; i<=NF; i++) { if ($i ~ /^[0-9]+$/) { tot[i]+=$i } else { tot[i]++ } } }
        END {
                while (getline < "ahjiefreak.dat") {
                        for (i=1; i<=NF; i++) {
                                if (tot[i]) { printf $i OFS }
                        }
                        print ""
                }
        }
' ahjiefreak.dat


Last edited by Annihilannic; 08-05-2008 at 12:17 AM.. Reason: Bug fix
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace substring by longest string in common field (awk)

Hi, Let's say I have a pipe-separated input like so: name_10|A|BCCC|cat_1 name_11|B|DE|cat_2 name_10|A|BC|cat_3 name_11|B|DEEEEEE|cat_4 Using awk, for records with common field 2, I am trying to replace all the shortest substrings by the longest string in field 3. In order to get the... (5 Replies)
Discussion started by: beca123456
5 Replies

2. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

3. Shell Programming and Scripting

Strip leading and numbers from a string.

Hello I have two vars loaded with $VAR1="ISOMETHING103" $VAR2="COTHERTHING04" I need to: 1) Strip the first char. Could be sed 's/^.//' 2) The number has it's rules. If it has "hundreds", it needs to be striped. If it is just two digits it shouldn't. So, for VAR1 output should be... (7 Replies)
Discussion started by: tristezo2k
7 Replies

4. Shell Programming and Scripting

Find common terms in two text file, xargs, grep

Hello, I'm interested in finding all occurrences of the terms in file1 in file2, which are both csv files. I can do this with a loop but I'm interested in knowing if I can also do it with the help of xargs and grep. What I have tried: cat file1 | xargs grep file2 The problem is that... (15 Replies)
Discussion started by: eon
15 Replies

5. Shell Programming and Scripting

Need help joining two files with a common string

Hi all, I have one file that is in the form: S0243K05_T7_S0243K05_|_BASS2243.C7_K05 groupVI. 88.76 S0137F20_SP6_S0137F20_|_BASS2137d.SPB2.2_C10 groupXXI 88.06 S0056F03_T7_S0056F03_|_BASS256c.C7_C02 groupXIX 85.99 S0056F03_T7_S0056F03_|_BASS256c.C7_C02 groupXIX 83.23... (3 Replies)
Discussion started by: repiv
3 Replies

6. UNIX for Dummies Questions & Answers

Combine multiple files with common string into one new file.

I need to compile a large amount of data with a common string from individual text files throughout many directories. An example data file is below. I want to search for the following string, "cc_sectors_1" and combine all the data from each file which contains this string, into one new... (2 Replies)
Discussion started by: GradStudent2010
2 Replies

7. Shell Programming and Scripting

Strip out the string

awk -F"\t" -vOFS="\t" '{print $1"\t-\t-","",$6,$7"\t-"$8"\t-\t-\t"$15}' file.tsv > output.tsv Using the above command how to remove the string www.abc.com from the $7 value. (7 Replies)
Discussion started by: sandy1028
7 Replies

8. Shell Programming and Scripting

Strip a string in sh

I have a list of servers that I need my script to ping however this list also has the env they belong too such as SIT, PRD, warehouse and so on. The break character for each section is : A value in my list would look like this... brutus.grhq.xxx.com:warehouse Where brutus.grhq.gfs.com is... (13 Replies)
Discussion started by: LRoberts
13 Replies

9. Shell Programming and Scripting

Need to strip a string

I have a file that looks like this: /home/fred/opt/bin /opt/usr/bin /usr/sbin/var/opt I need a way to chop of everything after the last occurance of the / sign including the /. So the file above will now look like this below. /home/fred/opt /opt/usr /usr/sbin/var I tried using... (6 Replies)
Discussion started by: x96riley3
6 Replies
Login or Register to Ask a Question