我正在寻找如果文件具有Windows行尾将其更改为linux行尾的if语句,否则什么也不做
这是带有Windows行尾^M
或\r\n
的文件
$ cat -v file.csv | head
"Rec_Open_Date","MSISDN","IMEI","Data_Volume_Bytes","Device_Manufacturer","Device_Model","Product_Description"^M
"2015-10-06","427","060","137765","Samsung Korea","Samsung SM-G900I","$39 Plan"^M
"2015-10-06","592","620","0","Apple Inc","Apple iPhone 6 (A1586)","PREPAY STD - TRIAL - #16"^M
"2015-10-06","007","290","0","Apple Inc","Apple iPhone 6 (A1586)","PREPAY PLUS - $0 -"^M
"2015-10-06","592","050","48836832","Apple Inc","Apple iPhone 5S (A1530)","Talk and Text Connect Flexi Plan"^M
"2015-10-06","409","720","113755347","Samsung Korea","Samsung SM-G360G","$29 CARRYOVER PLAN"^M
"2015-10-06","742","620","19840943","Apple Inc","Apple iPhone S (A1530)","PREPAY STD - $0 - #2"^M
"2015-10-06","387","180","0","HUAWEI Technologies Co Ltd","HUAWEI HUAWEI G526-L11","PREPAY STD - $1 - #4"^M
"2015-10-06","731","570","2258243","Samsung Korea","Samsung SM-N910U","Business Freedom"^M
"2015-10-06","556","910","13332272","Samsung Korea","Samsung GT-I9505","$49 Plan"^M
这是将其转换为linux行结尾
\n
的代码: $ awk 'sub("\r$", "")' file.csv | head | cat -v
"Rec_Open_Date","MSISDN","IMEI","Data_Volume_Bytes","Device_Manufacturer","Device_Model","Product_Description"
"2015-10-06","427","060","137765","Samsung Korea","Samsung SM-G900I","$39 Plan"
"2015-10-06","592","620","0","Apple Inc","Apple iPhone 6 (A1586)","PREPAY STD - TRIAL - #16"
"2015-10-06","007","290","0","Apple Inc","Apple iPhone 6 (A1586)","PREPAY PLUS - $0 -"
"2015-10-06","592","050","48836832","Apple Inc","Apple iPhone 5S (A1530)","Talk and Text Connect Flexi Plan"
"2015-10-06","409","720","113755347","Samsung Korea","Samsung SM-G360G","$29 CARRYOVER PLAN"
"2015-10-06","742","620","19840943","Apple Inc","Apple iPhone S (A1530)","PREPAY STD - $0 - #2"
"2015-10-06","387","180","0","HUAWEI Technologies Co Ltd","HUAWEI HUAWEI G526-L11","PREPAY STD - $1 - #4"
"2015-10-06","731","570","2258243","Samsung Korea","Samsung SM-N910U","Business Freedom"
"2015-10-06","556","910","13332272","Samsung Korea","Samsung GT-I9505","$49 Plan"
这是我的基本ifstatemnt,我不确定如何从行尾得到true或false:有人可以建议我怎么做吗?
if [ $1 -gt 100 ] ## line ending =^M
then
awk 'sub("\r$", "")' file.csv ## remove the windows line endings
fi
据我所知
Windows的行尾可以是
^M
,\r\n
或CRLF
。linux行尾可以是
\n
LF
。EDIT1
认为this是我想要的。
$ eol=$(cat -v file.csv | head -n1 | grep -o '..$')
$ echo "$eol"
^M
$ if [ "$eol" = "^M" ]; then echo "true"; fi
true
$ if [ "$eol" = "^M" ]; then echo "true"; else echo "false"; fi
true
我将
awk 'sub("\r$", "")' file.csv
放在我的真实声明中 最佳答案
使用vi将文件格式更改为Unix,例如
vi file.txt <<EOF
:set fileformat=unix
ZZ
EOF
如果文件已经是Unix格式,则上面的代码将使文件保持不变。
关于linux - 如果文件具有Windows行尾,请将其更改为linux行尾,否则不执行任何操作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37287280/