09月22日, 2014 2144次
1 bat
@for /f "tokens=*" %%i in (a.txt) do (if not defined %%i (echo %%i>>b.txt&set "%%i=ok")) 或者 for /f "tokens=* delims=" %%i in (test.txt) do (findstr /i "%%i" new.txt 2>NUL|| >>new.txt echo.%%i)
2 c++
#include <stdio.h> #include <string.h> #define MAX 1000 int main() { FILE *in, *out; char str[MAX]; int i=0, j=0, k=0, m=0; char **file=new char*[MAX]; in=fopen( "a.txt ", "rb "); fgets(str, MAX, in); while(!feof(in)) { file[i] = new char[strlen(str)+1]; strcpy(file[i], str); fgets(str, MAX, in); i++; } fclose(in); i--; for(j=0; j <i; j++) { if(strcmpi(file[j], file[j+1])==0) { for(k=j; k <i; k++) { file[k]=file[k+1]; } i--; j--; } } out=fopen( "b.txt ", "wb "); for(m=0;m <=i; m++) fputs(file[m], out); fclose(out); delete [] file; return 0; }
3 vbs
'删除文本文件中重复的行! Const adOpenStatic = 3 Const adLockOptimistic = 3 Const adCmdText = &H0001 Const ForReading = 1, ForWriting = 2, ForAppending = 8 Set objConnection = CreateObject("ADODB.Connection") Set objRecordSet = CreateObject("ADODB.Recordset") Set fso = CreateObject("Scripting.FileSystemObject") strPathToTextFile = "e:\vbs\" '文件所在目录 strFile = "t.txt" '输入文件名 outFile = "out.txt" '输出文件名 objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strPathtoTextFile & "; Extended Properties=""text;HDR=NO;FMT=Delimited""" objRecordSet.Open "Select DISTINCT * FROM "& strFile, objConnection, adOpenStatic, adLockOptimistic, adCmdText set file=fso.OpenTextFile("\\192.168.0.250\日志$\正常.log", ForWriting, True) Do Until objRecordSet.EOF 'Wscript.Echo objRecordSet.Fields.Item(0).Value file.WriteLine objRecordSet.Fields.Item(0).Value objRecordSet.MoveNext Loop
4js
有一组字符串比如 北京,北京,上海,上海,上海,武汉-------->要得到 北京,上海,武汉 怎么去掉里面重复的字符串? function DelRepetStr(String str) { var result; var list = str.split(","); for(var i=0; i<list.length; i++) { if(result.indexOf(list(i)) == -1 ) result = result + list(i) + ","; } return result; } 一个ArrayList里放的是字符串,数字型的,如"111","122","111","233"...如何把重复的去掉: public class testStr { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ArrayList tmp = new ArrayList(); tmp.add("111"); tmp.add("112"); tmp.add("111"); tmp.add("111"); tmp.add("114"); String t1, t2; /**********方法一 start************/ // for (int i = 0; i < tmp.size(); i++) { // t1 = (String) tmp.get(i); // for (int j = i + 1; j < tmp.size(); j++) { // t2 = (String) tmp.get(j); // if (t1.equals(t2)) { // tmp.remove(j); // j--; // } // } // } /**********方法一 end*********××***/ /**********方法二 start************/ // for (int i = 0; i < tmp.size(); i++) { // t1 = (String) tmp.get(i); // for (int j = tmp.size() - 1; j > i; j--) { // t2 = (String) tmp.get(j); // if (t1.equals(t2)) { // tmp.remove(j); // } // } // } /**********方法二 end**************/ /**********方法三 start************/ // tmp = (ArrayList) removeDuplicateObj(tmp); /**********方法三 end**************/ /**********方法四 start************/ TreeSet set=new TreeSet(tmp); Iterator it=set.iterator(); int[] arr=new int[set.size()]; int i=0; while(it.hasNext()){ arr[i]=Integer.parseInt(it.next().toString()); i++; } Arrays.sort(arr); for (int j = 0; j < arr.length; j++) { System.out.println(arr[j]); } /**********方法四 end**************/ System.out.println("tmp--"+tmp.size()); } /** * @param list */ private static List removeDuplicateObj(List list) { // ................ Set someSet = new HashSet(list); // 将Set中的集合,放到一个临时的链表中(tempList) Iterator iterator = someSet.iterator(); List tempList = new ArrayList(); int i = 0; while (iterator.hasNext()) { tempList.add(iterator.next().toString()); i++; } return tempList; } }
5 shell
sort -u 就搞定了
暂无留言,赶快评论吧