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
# 1  
Old 08-04-2008
How to strip out common terms in string

Hi,

I have this kinda of data:-

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 rows 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

In other words, those with zeros occuring on all rows, i wanted it to removed from the string. Anyone could advise.


THanks.
# 2  
Old 08-04-2008
Try sed
Code:
sed '/foo\|foo1\|orange/s/0,//g' file

# 3  
Old 08-04-2008
Hi,

Actually, the data I have previously is just an analogy.

In real case, i have this file of thousands of rows of numbers ending with a text.


Please advise.
# 4  
Old 08-04-2008
Your request is ambiguous - do you want all leading zeroes removed or is there some kind of "trim" factor - like remove 2 zeroes.
# 5  
Old 08-04-2008
At the same time, i noticed you had posted quite a lot, so you should have learnt something by now. Show your code.
# 6  
Old 08-04-2008
Hi,

I wanted to have common zeros occuring in rows of data to be eliminated at all. Dont have to be trim of any factor.

Currently, I am thinking of using awk with NF and NR.

Code:
awk -F FS={,}  '{

for(i=0; i<NF;i++){
   for(j=0;j<NR;j++){
       if(element[i][j]=="0")
        count++;
    }
   if(count=NR)
        //remove the rows
}     
       
'} myfile

The code is just a basic brainstorm idea which I had. Its not syntatically-correct.

Please advise. Thanks.
# 7  
Old 08-04-2008
In your example, why did you remove the 0 from the beginning of the foo and foo1 lines, but not the orange line? That's the confusing part...
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