使用sed

时间:2018-04-17 02:24:24

标签: bash unix sed

我有一个逗号分隔的文件,如此

info,someinfo,moreinfo,123,

我想删除每行中的最后一个逗号。

我尝试了很多变种:

sed -i 's/.$//' filename

以上命令似乎是互联网的主流观点,但它并没有以任何方式改变我的文件。

4 个答案:

答案 0 :(得分:0)

如果每行包含,作为最后一个字母,您可以像这样删除

str="info,someinfo,moreinfo,123,"
echo ${str::-1}

<强>输出:

 info,someinfo,moreinfo,123

希望这有助于你

答案 1 :(得分:0)

最后一个逗号后面可能有尾随空格。您可以使用此命令,以便它可以很好地处理可选空格:

sed -i -E 's/,[[:space:]]*$//' file

我不确定你的意图是删除最后一个字符,无论它是否是逗号。

答案 2 :(得分:0)

如果是DOS let groundTexture = SKTexture(imageNamed: "Ground") groundTexture.filteringMode = .nearest for i in stride(from: 0, to: 2 + self.frame.size.width / groundTexture.size().width, by: 1) { let ground = SKSpriteNode(texture: groundTexture) ground.zPosition = 1.0 //-10 //ground.position = CGPoint(x: (groundTexture.size().width / 2.0 + (groundTexture.size().width * CGFloat(i))), y: groundTexture.size().height / 4.0) //original position ground.position = CGPoint(x: 0, y: groundTexture.size().height / +0) //tried this from a tutorial ground.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: self.frame.size.width * 2.0, height: groundTexture.size().height / 4.0)) //erase * 4 test ground.setScale(1.2) ground.physicsBody?.isDynamic = false ground.physicsBody?.allowsRotation = false ground.physicsBody?.affectedByGravity = false ground.physicsBody?.categoryBitMask = groundCategory //contact and collision bitmask ground.physicsBody?.contactTestBitMask = playerCategory | enemy1Category | enemy2Category | enemy3Category | enemy4Category | obstacleCategory | coinCatergory ground.physicsBody?.collisionBitMask = playerCategory | enemy1Category | enemy2Category | enemy3Category | enemy4Category | obstacleCategory | coinCatergory ground.physicsBody?.restitution = 0.0 self.addChild(ground) let moveLeft = SKAction.moveBy(x: -groundTexture.size().width, y: 0, duration: 5) let moveReset = SKAction.moveBy(x: groundTexture.size().width, y: 0, duration: 0) let moveLoop = SKAction.sequence([moveLeft, moveReset]) let moveForever = SKAction.repeatForever(moveLoop) ground.run(moveForever) } 行终止文件,请同时删除\r\n

\r

在awk中重新定义记录分隔符$ cat file info,someinfo,moreinfo,123, $ unix2dos file unix2dos: converting file file to DOS format... $ sed 's/.\r$//' file info,someinfo,moreinfo,123 以有条件地接受RS。它将在输出中删除:

\r

如果您使用的是GNU awk,则可以在$ awk 'BEGIN{RS="\r?\n"}{sub(/.$/,"")}1' file info,someinfo,moreinfo,123 之前使用\rORS=RT;(有条件地)添加到输出中,即:

print

答案 3 :(得分:0)

你能尝试这个: sed 's/\(.\)$//' file_name > filename_output

只需将输出重定向到新文件filename_output