Visit Our UNIX and Linux User Community


Diff output, unwanted characters


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Diff output, unwanted characters
# 1  
Old 10-16-2007
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  
Old 10-16-2007
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  
Old 10-16-2007
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  
Old 10-17-2007
Code:
diff file1 file2 | sed 's/^> //g' > filename

# 5  
Old 10-17-2007
With diff on IRIX, if the line is blank then only ">" is printed, not "> ".
# 6  
Old 10-17-2007
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 06:29 PM..
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #773
Difficulty: Medium
A finite-state machine (FSM) is a specialized computer developed by AT&T.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Stripping unwanted characters in field

I wrote myself a small little shell script to clean up a file I have issues with. In particular, I am stripping down a fully qualified host/domain name to just the hostname itself. The script works, but from a performance standpoint, it's not very fast and I will be working with large data sets. ... (4 Replies)
Discussion started by: dagamier
4 Replies

2. Shell Programming and Scripting

sed to get rid of unwanted characters

so i have strings such as this: 'postfix/local#2,5#|CRON.*12062.*root.*CMD#2,5#|roice.*NQN1#1,2#|toysprc#1,4#' i need to get rid of the "#" and the numbers between them for each of the strings above. so the desired output should be: ... (1 Reply)
Discussion started by: SkySmart
1 Replies

3. Shell Programming and Scripting

Help with diff output

I am running diff between two directories dir1 and dir2. diff --exclude --recursive --brief -b dir1 dir2 The output of the above command is Files dir1/java/abc/bcd/abc9991.java and dir2/java/abc/bcd/abc9991.java differ Files dir1/java/abc/bcd/abc9933.java and... (11 Replies)
Discussion started by: gaurav99
11 Replies

4. IP Networking

How do delete unwanted characters from lsof?

Hi. I need to trace on Unix level number of connections to an Oracle database. The listener runs on port 1521. The following is run: oracle@server03 >lsof -Pni |grep ".1521" |grep IPv4 | awk {'print $5'}|cut -d: -f 1|sort|uniq -c|sort -nk 1 87 IPv4 oracle@server03 > I need to append... (2 Replies)
Discussion started by: grigorianvlad
2 Replies

5. Shell Programming and Scripting

FTP stores unwanted chars to output file

I want to only store date of all files. However, the below script also store unwanted FTP related lines to output file. Could someone tell me how to ignore them (see output first 6 lines)? Thanks #Defining variables and assigning values USR='admin' PASSWD='abc'... (3 Replies)
Discussion started by: dipeshvshah
3 Replies

6. Shell Programming and Scripting

removing unwanted characters from a file

i have a file like this 1111_2222#$#$dudgfdk 11111111_343434#$#$334 1111_22222#43445667 i want to remove all those charachetrs from # how can i do this Thank in advance Saravanan (4 Replies)
Discussion started by: saravanan71184
4 Replies

7. UNIX for Dummies Questions & Answers

Diff creating special characters in output file

Hi I am comparing 2 files (using diff command) with numerical data in them. In the output file I want only the differences which are in file2 but not in file1. Although I am getting the diffences i am also getting special characters in the output file which i do not want. Can somebody help... (2 Replies)
Discussion started by: ashu_r2001
2 Replies

8. Shell Programming and Scripting

to replace unwanted new line characters

Hi how to replace un wanted new line characters. my file contains data like. ramu,sony,"raju \n ravi \n ramya" \n ravi,sarah,"sowmya \n sorry s\ sangam" \n i want replace new line characters in between double coats with single space. for example $ cat input_file ramu,sony,"raju... (3 Replies)
Discussion started by: Raghava
3 Replies

9. UNIX for Dummies Questions & Answers

diff output

I have two CSV files and I would like to create a third CSV file containing the differences between the two. I understand the diff command can be used to list differences between two files. My problem is that when I pipe the output into a third CSV file, the line numbers and other formatting... (3 Replies)
Discussion started by: paulp
3 Replies

10. Shell Programming and Scripting

diff 2 files; output diff's to 3rd file

Hello, I want to compare two files. All records in file 2 that are not in file 1 should be output to file 3. For example: file 1 123 1234 123456 file 2 123 2345 23456 file 3 should have 2345 23456 I have looked at diff, bdiff, cmp, comm, diff3 without any luck! (2 Replies)
Discussion started by: blt123
2 Replies

Featured Tech Videos