sorting - Merge files with sort -m and give error if files not pre-sorted? -
need out here.
i have 2 files,
file1.txt > 5555555555 1111111111 7777777777 file2.txt > 0000000000 8888888888 2222222222 4444444444 3333333333
when run, $ sort -m file1.txt file2.txt > file-c.txt
output file-c.txt merged within file1 , file2 not sorted.
file-c.txt > 0000000000 5555555555 1111111111 7777777777 8888888888 2222222222 4444444444 3333333333
when happens need error saying files (file1 , file2) not sorted , merge can't merge files before has been sorted. when run $ sort -m file1.txt file2.txt > file-c.txt
have error saying cannot merge file1 , file2 file-c because not yet sorted.
hope guys understand me :d
if understand you're asking, this:
diff1=$(diff <(cat file1.txt) <(sort file1.txt)) diff2=$(diff <(cat file2.txt) <(sort file2.txt)) if [ "$diff1" != "" ]; echo 'file1 not sorted' elif [ "$diff2" != "" ]; echo 'file2 not sorted' else sort -m file1.txt file2.txt fi
this works in bash (and other shells) , following:
- set diff1 variable output of diff of cat , sort of file1 (this empty if cat , sort same meaning if file sorted
- set diff2 variable in same manner diff1 file2
- do simple if .. elif .. else check , see whether file1 , file2 sorted, , if command line sort of two
is looking for?
edit: alternately per @twalberg if version of sort supports it, can this:
if ! sort -c file1.txt echo 'file1 not sorted' elif ! sort -c file2.txt echo 'file2 not sorted' else sort -m file1.txt file2.txt fi
Comments
Post a Comment