The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Formatting the output from diff ragavhere Shell Programming and Scripting 9 05-07-2008 05:40 AM
Is there a way to limit DIFF output bobk544 Shell Programming and Scripting 2 06-08-2007 04:54 PM
parsing output from a diff command... kam Shell Programming and Scripting 5 05-30-2007 12:26 AM
diff output paulp UNIX for Dummies Questions & Answers 3 04-14-2005 03:27 AM
diff 2 files; output diff's to 3rd file blt123 Shell Programming and Scripting 2 05-28-2002 12:29 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-16-2007
scanner248 scanner248 is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 6
Diff output, unwanted characters

I've got a diff command running in a shell script that writes the ouput to a new file. In the new file there is a ">" at the beginning of each line. The output file is going to be used by another program and that character makes the file useless.

What I'm getting in the new file:
> 2007-09-27 20:23:33.47 EXEC 0B21800A 0A080231 8A000505 00000000 00000000

What it should look like: (absence of the > and a space)
2007-09-27 20:23:33.47 EXEC 0B21800A 0A080231 8A000505 00000000 00000000

Is there a way to avoid this going into the new file from the beginning or will I have to parse out the characters?

Thanks in advance,
  #2 (permalink)  
Old 10-16-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
I strip it out as follows:


Code:
	diff $STDERR $SUPPRESS_FILE | while read STRING
	do
		case "$STRING" in
		\<* )
			if test "$STRING" = "<"
			then
				echo 1>&2
			else
				echo "$STRING" | sed "s/< //" 1>&2
			fi
			;;
		* )
			;;
		esac
	done

  #3 (permalink)  
Old 10-16-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 712
Hi.

If you have GNU diff of a certain age, you can do something like:

Code:
#!/usr/bin/env sh

# @(#) s1       Demonstrate diff formatting.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility "version")"
version bash diff

echo

echo " Data files data1 data2:"
cat data1
echo
cat data2

echo
echo " Standard diff output:"
diff data1 data2

echo
echo " Suppress old and unchanged lines, output changed plain:"
diff --changed-group-format="%>" --unchanged-group-format="" data1 data2

exit 0

Producing:

Code:
% ./s1

(Versions displayed with local utility version)
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
diff (GNU diffutils) 2.8.1

 Data files data1 data2:
a
b
c
e

a
b
d
e

 Standard diff output:
3c3
< c
---
> d

 Suppress old and unchanged lines, output changed plain:
d

If you have a diff that does not recognize those options, you can filter the output:

Code:
#!/usr/bin/env sh

# @(#) s2       Demonstrate diff filtering.

set -o nounset
echo

debug=":"
debug="echo"

## Use local command version for the commands in this demonstration.

echo "(Versions displayed with local utility "version")"
version bash diff grep sed

echo

echo " Data files data1 data2:"
cat data1
echo
cat data2

echo
echo " Filtered diff output:"
diff data1 data2 |
grep "^>" |
sed 's/^> //'

exit 0

Which produces:

Code:
% ./s2

(Versions displayed with local utility version)
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
diff (GNU diffutils) 2.8.1
grep (GNU grep) 2.5.1
GNU sed version 4.1.2

 Data files data1 data2:
a
b
c
e

a
b
d
e

 Filtered diff output:
d

See man diff for details ... cheers, drl
  #4 (permalink)  
Old 10-17-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,958

Code:
diff file1 file2 | sed 's/^> //g' > filename

  #5 (permalink)  
Old 10-17-2007
porter porter is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2007
Posts: 2,965
With diff on IRIX, if the line is blank then only ">" is printed, not "> ".
  #6 (permalink)  
Old 10-17-2007
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 712
Hi, porter.

Thanks for that. So my 2nd sequence would be more general as:

Code:
diff data1 data2 |
grep "^>" |
sed 's/^> *//'

but that would cause trouble for lines that actually started with a space on IRIX and other systems, so that suggests that on IRIX, one would need a separate version, like:

Code:
diff data1 data2 |
grep "^>" |
sed 's/^>//'

Another reason why it's useful to post the flavor of one's OS (and to have someone who has access to a variety of systems) ... cheers, drl

Last edited by drl; 10-17-2007 at 05:29 PM..
Closed Thread

Bookmarks

Tags
linux, linux commands

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:40 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0