我过去做的没问题,但是这次我做不到,我也不明白为什么.....
我的原始文件是
1002 10214
1002 10220
1002 10222
1002 10248
1002 10256
我需要制作一个新文件,将上面的2列合并,并添加第二个列,其值为1
所需的输出应如下所示
100210214 1
100210220 1
100210222 1
100210248 1
100210256 1
我尝试了以下awk命令,首先将2列打印为1到tmp文件中,然后将多余的列添加为“ 1”
cat input.txt | awk '{ print ($1$2)}' > tmp1.txt
cat tmp1.txt | awk ' {print $0, (1) }' > output.txt
虽然第一个命令似乎可以正常运行,但第二个命令却没有运行
tmp1.txt(确定)
100210214
100210220
100210222
100210248
100210256
output.txt(不好)
10210214
10210220
10210222
10210248
10210256
“ 1”出现在第一列的前面,不知道为什么,甚至替换了前2个字符。是因为原始输入文件不同(可能是使用“空格”代替了制表符)?
答案 0 :(得分:1)
请您尝试以下。
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<!------ Begin Description Section ------->
<div id="Description_details" class="container">
<div class="row">
<div class="step body current w-100" id="trade-steps-p-2" role="tabpanel" aria-labelledby="" aria-hidden="false" style="display: block;">
<fieldset class="trade">
<legend>What are you Buying?</legend>
<div class="row">
<div class="form-group col-12 col-md-6 required" aria-required="true">
<label for="ContractName">Give Your Trade a Name
<a href="#" data-toggle="popover" data-trigger="hover" data-content=" data-original-title="" title="">
<i class=" fa fa-question-circle"></i>
</a>
</label>
<input name="data[Contract][name]" class="form-control" maxlength="50" type="text" id="ContractName" required="required" aria-required="true" aria-invalid="false">
</div>
<div class="form-group col-12 col-md-6 required" aria-required="true">
<label for="">Industry Classification</label>
<select name="data[Contract][industry]" class="form-control" id="" required="required" aria-required="true" aria-invalid="false">
<option value="GENERAL_GOODS_SERVICES" selected="selected">General Goods & Services</option>
<option value="AGRICULTURE_LIVESTOCK_GAME">Agriculture, Livestock & Game</option>
<option value="ART_ANTIQUES_COLLECTIBLES">Art, Antiques & Collectibles</option>
<option value="BUSINESS_SALE_BROKING">Business Sale & Broking</option>
<option value="VEHICLES_WATERCRAFT">Cars, Bikes & Watercraft</option>
<option value="CONSTRUCTION">Construction</option>
</select>
</div>
</div>
</div>
</div>
答案 1 :(得分:0)
当输入文件具有Windows行尾(即\r\n
)时,会发生这种情况。您可以使用以下命令对其进行修复:
dos2unix file
,然后通过此命令获得所需的输出:
awk '{$1=$1$2;$2=1}1' file