用java实现"awk -d"功能(保留多行重复)

一般用过linux脚本的都知道"awk -d"的用法: 只显示有重复数据行,每种重复行只显示其中一行.

而我的需求是希望显示所有的重复行, 而不是只是一行. 因为目前对shell脚本不是很熟练, 下面是java代码的实现,感觉比想象的复杂, 备忘一下:

public class ReadCardCode {
public static void main(String[] args) throws Exception {
BufferedReader reader =
new BufferedReader(new FileReader("sort.log"));
BufferedWriter writer1 =
new BufferedWriter(new FileWriter("result.log"));
BufferedWriter writer2 =
new BufferedWriter(new FileWriter("result-2.log"));
int count = 6;
int i = 0;
String current = null;
String curItemId = null;
// 将同一商品的所有记录取出放到一边, 如果这些记录大于1则说明有重复, 输出. 否则抛弃
List<String> lineList = new ArrayList<String>(10);
try {
while ((current = reader.readLine()) != null) {
String[] curArray = current.split(" ");
if (curItemId == null) { // 首行
lineList.add(current);
} else { // 下一行
if (curArray[2].equals(curItemId)) { // 相同行加入列表
lineList.add(current);
} else {
writeLineList(writer1, writer2, lineList);
// 下一轮首行
lineList.clear();
lineList.add(current);
}
}

curItemId = curArray[2];

// if (i++ > 6) {
// break;
// }
}
writeLineList(writer1, writer2, lineList);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
writer2.close();
writer1.close();
} catch (Exception ee) {
ee.printStackTrace();
}
}
}

private static void writeLineList(BufferedWriter writer, BufferedWriter writer2, List<String> lineList)
throws IOException {
if (lineList.size() > 1) { // 输出前面相同行
for (String line : lineList) {
write(writer, writer2, line);
}
}
}

private static void write(BufferedWriter writer, BufferedWriter writer2, String str) throws IOException {
BufferedWriter w = writer;
// String[] curArray = str.split(" ");
// String itemId = curArray[2].replace("itemId=", "");
// long route = Long.valueOf(itemId) % 2;
// if (route == 1) {
// w = writer;
// } else {
// w = writer2;
// }
w.write(str);
w.newLine();
w.flush();
}
}
#!/bin/bash ###############parameter #安装包所在路径 pack_dir=/iflytek/install #文件安装路径 install_dir=/iflytek/server #fastdfs配置文件所在路径 conf_dir=/iflytek/conf #shell脚本所在路径 sh_dir=/iflytek/install/shell #获取本机ip地址 ip=`ip addr | grep 'state UP' -A2 | grep inet | egrep -v '(127.0.0.1|inet6|docker)' | awk '{print $2}' | tr -d "addr:" | head -n 1 | cut -d / -f1` #主机名 HostName=skynet #服务器密码 Host_Passwd=iflytek #mysql密码 PASSWORD=123456 ###################################################################### # # ###################################################################### echo '0.创建所需目录' ##创建目录 mkdir -p /iflytek/{engine,conf,bak,server} mkdir -p /iflytek/data/zookeeper mkdir -p /usr/java/ ###################################################################### # # 1.操作系统设置 # ###################################################################### echo '1.操作系统设置' ##关闭防火墙 systemctl stop firewalld.service > /dev/null 2>&1 systemctl disable firewalld.service > /dev/null 2>&1 ##关闭selinux setenforce 0 sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config ##系统设置 ulimit -n 655360 ulimit -u 655360 echo '* soft noproc 655360 ' >>/etc/security/limits.conf echo '* hard noproc 655360 ' >>/etc/security/limits.conf echo '* soft nofile 655360 ' >>/etc/security/limits.conf echo '* hard nofile 655360 ' >>/etc/security/limits.conf echo 'vm.max_map_count=655360' >>/etc/sysctl.conf sleep 1 sysctl -p >/dev/null 2>&1 #Hypercnn版本必须关闭服务器的大页面 #echo never > /sys/kernel/mm/transparent_hugepage/enabled #echo never > /sys/kernel/mm/transparent_hugepage/defrag ###################################################################### # # 2.配置java环境 # ###################################################################### echo "2.配置java环境" rpm -e --nodeps `rpm -qa|grep -i jdk` sleep 4 tar zxf $pack_dir/jdk-8u144-linux-x64.tar.gz -C /usr/java/ echo 'export JAVA_HOME=/usr/java/jdk1.8.0_144' >> /etc/profile echo 'export JRE_HOME=$JAVA_HOME/jre' >>/etc/profile echo 'export PATH=$PATH:$JAVA_HOME/bin' >>/etc/profile echo 'export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar' >>/etc/profile sleep 2 source /etc/profile >/dev/null 2>&1 sleep 2 ###################################################################### # # 3.设置本地yum源 # ###################################################################### echo "3.设置本地yum源" mkdir -p /etc/yum.repos.d/bak mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak mkdir -p /iflytek/web sleep 2 mount -o loop $pack_dir/CentOS-7-x86_64-DVD-2009.iso /tmp >/dev/null 2>&1 sleep 2 cp -a /tmp /iflytek/web/centos sleep 2 umount /tmp >/dev/null 2>&1 ###################配置本地yun源 cp $shell/conf/my-z-centos.repo /etc/yum.repos.d/ ######################加载yum源 yum clean all > /dev/null 2>&1 yum makecache > /dev/null 2>&1 yum repolist > /dev/null 2>&1 #####################network yum #echo "http install " #yum -y install openssh-clients httpd #systemctl enable httpd #systemctl start httpd #ln -s /iflytek/web/centos /var/www/html/ ########################################package #yum-slve #cp /iflytek/shell/conf/my-c-centos.repo /etc/yum.repos.d/ #sleep 5 ###################################################################### # # 4.安装所需依赖包 # ###################################################################### #echo "4.安装所需依赖包" #yum -y install openssl openssl-devel gcc* &> /dev/null #已经安装开发工具时执行 #yum -y install openssl openssl-devel expect gcc* vim net-tools.x86_64 zip unzip wget gzip lrzsz dos2unix sysstat-11.7.3-1.x86_64 >/dev/null 2>&1 ##解压安装包 echo '4.1 解压文件' for i in /iflytek/server/*.zip; do unzip -o $i -d /iflytek/server/ >/dev/null 2>&1 done chmod -R 775 /iflytek/ #解压部署包 for tar in $pack_dir/*.tar.gz; do tar -xf $tar -C $install_dir/; done chmod -R 775 /iflytek sleep 5 mv $install_dir/*.zip $pack_dir/ ###################################################################### # # 5.配置主机名 # ###################################################################### echo "5.配置主机名" hostname $HostName> /etc/hosts echo "$ip $HostName" >> /etc/hosts echo "HOSTNAME=$HostName" >> /etc/sysconfig/network echo "skynet" >/etc/hostname ###################################################################### # # 6.配置验证 # ###################################################################### echo "6.验证" echo "6.1java环境" java -version echo "6.2文件限制" grep "vm.max_map_count" /etc/sysctl.conf | tail -1 cat /etc/security/limits.conf | grep -i "65536" echo "6.3查看主机名" cat /etc/hosts 优化shell脚本
06-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值