I am using gnu diffutils to output the difference between two sql files using the following:
diff -e abc.sql abcd.sql >diff.sql
But the difference saved is also showing line number where it is found before the difference and a full stop after it.
I want to only display the difference and ignore the line number and the full stop.
diff --line-format=%L file1 file2
From man diff:
--line-format=LFMT
format all input lines with LFMT
LFMT (only) may contain:
%L contents of line
Related
I am currently trying to create a bash script that will change a text document of one line into multiple lines.
Example:
TextFile: Header~someHeaderInfo|Object~someObjectInfo|SubObject~someSubObjectInfo|Object~someObjectInfo|SubObject~someSubObjectInfo|...|Tail~someInfo
Again, this above is only a single line.
This should be called through a bash script and be converted into:
Header~someHeaderInfo
Object~someObjectInfo|SubObject~someSubObjectInfo
Object~someObjectInfo|SubObject~someSubObjectInfo
...
Tail~someInfo
In the real use case, each Object has upwards of 20 subObjects, each of which may have more subObjects themselves.
How can I go about this separation?
if textfile contains:
Header~someHeaderInfo|Object~someObjectInfo|SubObject~someSubObjectInfo|Object~someObjectInfo|SubObject~someSubObjectInfo|...|Tail~someInfo
The following bash command:
sed "s/|/\n/g" textfile
Will produce the following output:
Header~someHeaderInfo
Object~someObjectInfo
SubObject~someSubObjectInfo
Object~someObjectInfo
SubObject~someSubObjectInfo
...
Tail~someInfo
But the OP wants the SubObject on the same line (see comments), so I suggest:
sed "s/|\([^S]\)/\n\1/g" textfile
That will produce the following output:
Header~someHeaderInfo
Object~someObjectInfo|SubObject~someSubObjectInfo
Object~someObjectInfo|SubObject~someSubObjectInfo
...
Tail~someInfo
I have a task where I need to parse through files and extract information. I can do this easy using bash but I have to get it done through unix commands only.
For example, I have a file similar to the following:
Set<tab>one<tab>two<tab>three
Set<tab>four<tab>five<tab>six
ENDSET
Set<tab>four<tab>two<tab>nine
ENDSET
Set<tab>one<tab>one<tab>one
Set<tab>two<tab>two<tab>two
ENDSET
...
So on and so forth. I want to be able to extract a certain number of sets, say the first 10. Also, I want to be able to extract info from the columns.
Once again, this is a trivial thing to do using bash scripting, but I am unsure of how to do this with unix commands only. I can combine the commands together in a shell script but, once again, only unix commands.
Without an output example, it's hard to know your goal, but anyway, one UNIX command you can use is AWK.
Examples:
Extract 2 sets from your data sample (without include "ENDSET" nor blank lines):
$ awk '/ENDSET/{ if(++count==2) exit(0);next; }NF{print}' file.txt
Set one two three
Set four five six
Set four two nine
Extract 3 sets and print 2nd column only (Note 1st column is always "Set"):
$ awk '/ENDSET/{ if(++count==3) exit(0);next; }$2{print $2}' file.txt
two
five
two
one
two
And so on... (more info: $ man awk)
I have 2 files for which i want to diff side by side using the following command with the line numbers:
diff -y abc1.txt abc2.txt
The above command just prints the side by side diff but doesn't display the line numbers associated with the files.
I even tried:
diff -y --unchanged-line-format="" --new-line-format=":%dn: %L" abc1.txt abc2.txt
But it gives some format conflict exception. Is there any way to do it ? I have searched a lot but couldn't find any solutions for it. I can't use third party tools just for your information. Any brilliant ideas are highly appreciated.
NOTE:
I want the file numbers present of the file itself and not the line numbers generated by piping to cat -n etc.. Lets say, i am doing diff using "--suppress-common-lines" then the line numbers should be omitted which are not shown in the diff. I want to display the line numbers associated with the files. I don't want to generate line numbers in my diff output
I am using diff to see the differenceS between 2 files.
It generates the output like:
some numbers here
< gi|description1
< ADGCAAAGGCC
---
> gi|description3
> GGCCTAAGGGG
Can I produce the output like:
gi|description1
ADGCAAAGGCC
gi|description3
GGCCTAAGGGG
without < , > ,--- , the first numbers line ????
Thanks a lot.
Certainly you can modify the output of the diff utility to your liking. In the end it is a utility in the typical unix tradition, so one would expect it to be tweakable into all directions :-)
The "man page" explains the options and points out the ...-line-format options for this. Take a look yourself: man diff...
This leads to a command like this:
diff --unchanged-line-format="" --old-line-format="%L" --new-line-format="%L" file1 file2
It outputs only lines that have changed and for those simply the literal old and new form without any further indication marks. Which is what you want, according to your example.
I have a requirement to compare two text files and to find out the difference between them. Basically I have an input file (input.txt) which will be processed by a batch job and my batch will log the output (successful.txt) where the job has successfully ran.
In simple words, I need to find out the difference between input.txt and successful.txt (input.txt-successful.txt) and I was thinking to use findstr. It seems to be fine, BUT I don't understand one part of it. It always includes the last line of my input.txt in the output. You could see that in the example below. Please note that there is no leading space or line break after the last line of my input.txt.
In below example, you could see the line server1,db1 is present on both the files, but still listed in the output. (It is always the last line of input.txt)
D:\Scripts\dummy>type input.txt
server2,db2
server3,db3
server10,db10
server4,db4
server1,db11
server10,schema11
host1,sch2
host11,sql2
host11,sql3
server1,db1
D:\Scripts\dummy>type successful.txt
server1,db1
server2,db2
server3,db3
server4,db4
server10,db10
host1,sch2
host11,sql2
host11,sql3
D:\Scripts\dummy>findstr /vixg:successful.txt input.txt
server1,db11
server10,schema11
server1,db1
What am I doing wrong?
Cheers,
G
I could reproduce your results by removing the newline after the last line of input.txt, so solution 1 would be to add a newline to the end of input.txt. Since you appear to say that input.txt has no terminal newline, then adding one would cure the problem; findstr is acting as expected because it acts on newline-terminated lines.
Solution 2 would be
type input.txt|findstr /vixg:successful.txt