DOS格式结尾有隐藏字符

Wayne posted @ Wed, 25 Aug 2010 10:40:22 +0000 in Experience , 2846 readers

 最近想完成一个脚本,实现不通过数据库而直接把号码按地区分类的功能.脚本用bash写的,内容如下:

#!/bin/bash
LIST=`grep $1 head|awk '{print $2}'`
for i in $LIST;
do 
grep $i $2 
done

 但是,"grep $1 $2"确始终不能正常执行.我打印出$LIST,一切正常,打印出$i,也一切正常.

后来有人提示,DOS格式的文本会以'\n\r"结尾,而我的awk并不能识别这一点,会把末尾一并截取,使得grep时错误.改成

awk -v RS='(\n|\r)+' '{print $2}'

即可.

我的文本文件的确是从windows传过去的,于是修改后试验了下.果然问题解决了.

最终脚本如下:

 

#!/bin/bash
LIST=`grep $1 head|awk -v RS='(\n|\r)+' '{print $2}'`
for i in $LIST;
do 
grep $i $2 
done
JAC Model Paper Clas said:
Thu, 01 Sep 2022 10:14:48 +0000

Jharkhand Board Model Paper 2023 Class 1 Pdf Download with Answers for English Medium, Hindi Medium, Urdu Medium & Students for Small Answers, Long Answer, Very Long Answer Questions, and Essay Type Questions to Term1 & Term2 Exams at official website. JAC Model Paper Class 1 New Exam Scheme or Question Pattern for Sammittive Assignment Exams (SA1 & SA2): Very Long Answer (VLA), Long Answer (LA), Small Answer (SA), Very Small Answer (VSA), Single Answer, Multiple Choice and etc.


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter