Operating System - HP-UX
1832275 Members
1844 Online
110041 Solutions
New Discussion

Removing constant pattern and whitespaces.

 
SOLVED
Go to solution
Chern Jian Leaw
Regular Advisor

Removing constant pattern and whitespaces.

HI,

I would like to remove every instance of the following pattern:
.output
=============

from the file attached. This pattern is a subset of the pattern:
pglc0003.output
=============

As there are whitespaces separating each entries, how do I write a script to eliminate those whitespaces and at the same time remove the mentioned pattern?

Thanks.


12 REPLIES 12
Steve Steel
Honored Contributor

Re: Removing constant pattern and whitespaces.

Hi

cat file|while read line
do
echo |sed -e 's/.output//'|grep -v "======="
done|strings > newfile


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Karvendhan M
Frequent Advisor
Solution

Re: Removing constant pattern and whitespaces.

I hope You have more than one file to process.

This small script will help you.

...............................
#!/usr/contrib/bin/perl -w

while(){

s/^\s*$//;
s/\.output//;
print;

}

...............................


run this script as $ cat text_file | script
or
$ script < text file.


HTH,

Karvendhan M
Karvendhan M
Frequent Advisor

Re: Removing constant pattern and whitespaces.

Sorry ,


I missed the === .. pattern.

the script can be
----------------------------------

#!/usr/contrib/bin/perl -w
while(){
s/^\s*$//; # remove blank lines.
s/^\=+\s*$//; # Remove === ..
s/\.output//; # Remove .output.
s/^\s+//; # remove leading space in lines.
print;
}


------------------------

~ Karvendhan M.

Leif Halvarsson_2
Honored Contributor

Re: Removing constant pattern and whitespaces.

# to remove .output :
while read $a
basename $a .output
done tmpfile1

# to remove ================

cat tmpfile1 |tr -s "=" >tmpfile2

# to remove whitespaces

cat tmpfile2 |tr -s " " >tmpfile3




Leif Halvarsson_2
Honored Contributor

Re: Removing constant pattern and whitespaces.

Sorry

I forgot "do".

While read $a do
Chern Jian Leaw
Regular Advisor

Re: Removing constant pattern and whitespaces.

Steve,
I tried running your code exactly as you've shown me:

cat filename|while read line
do
echo |sed -e 's/.output//'|grep -v "====="
done|strings > newfile.txt

However, the file "newfile" does not contain any outputs.

Could you help me out?

Thanks.
Karvendhan M
Frequent Advisor

Re: Removing constant pattern and whitespaces.

hi

Steve's script should be....


echo $line |sed -e 's/ .....



HTH,

Karvendhan M
Chern Jian Leaw
Regular Advisor

Re: Removing constant pattern and whitespaces.

Steve,

the line sed -e 's/.output//'|grep -v "===="
should remove the .output pattern followin the patterns pglc.., right?

Unfortunately, it's printing :
pglc0002.output
....

for all of its output.

Could you please help me out?

Thanks.
Procnus
Frequent Advisor

Re: Removing constant pattern and whitespaces.

if you want the output to be :
pglc0000
pglc0001
etc.

I'd tend to use:
grep '[a-zA-Z]' | cut -f1 -d. > output.txt

I can think of other ways using sed etc. but this would be quicker

Cheers
Steven
Mark Ellzey
Valued Contributor

Re: Removing constant pattern and whitespaces.

Chern,

Try Steve's script again, just modify the > newfile.txt to >> newfile.txt

ME
Procnus
Frequent Advisor

Re: Removing constant pattern and whitespaces.

oops, forgot something...
if you want the output to be :
pglc0000
pglc0001
etc.

I'd tend to use:
grep '[a-zA-Z]' | cut -f1 -d. > output.txt

I can think of other ways using sed etc. but this would be quicker

Cheers
Steven
Dave Chamberlin
Trusted Contributor

Re: Removing constant pattern and whitespaces.

I am assumming you want to get output like:
pglc0003
pglc0004
pglc0005

etc.

Use:
cat yourfile | grep output | sed s/\.output//g > newfile