这篇文章是我之前提到的问题的修正案here。
说我有下面的示例文件:
cat sample2.txt
HOST dev@opulex.com
PORT 1066
DATABASE ORACLE_1
SCHEMA DEPT.*;
SCHEMA EMP.*;
DATABASE ORACLE_2
SCHEMA JOB.*;
SCHEMA SALARY.*;
HOST orl@opulex.com
PORT 89
DATABASE MYSQL_1
SCHEMA PURCHASE.*;
DATABASE MYSQL_2
SCHEMA PRICE.*;
SCHEMA PRODUCT.*;
对于上述文件中的内容,我想只打印HOST / PORT / DATABASE / SCHEMA旁边的列,同时假设每行中的最后一列以分号结尾,我想替换缺少的列值以及上一行的列值。
@anubhava帮助我在我的previous post中获得了接近它的内容。
cat sample2.txt | awk 'tolower($0)~/^host|^port|^database|^schema/{printf "%s",$2 OFS;}' | awk -v RS=';' -v ORS=';\n' 'NF' | awk 'NF==1{print c1, c2, c3, $1; next} NF==2{print c1, c2, $1, $2; next} {c1=$1; c2=$2; c3=$3} 1' | sed 's|^[[:blank:]]*||g; s|\;$||g'
dev@opulex.com 1066 ORACLE_1 DEPT.*
dev@opulex.com 1066 ORACLE_1 EMP.*
dev@opulex.com 1066 ORACLE_2 JOB.*
dev@opulex.com 1066 ORACLE_1 SALARY.*
orl@opulex.com 89 MYSQL_1 PURCHASE.*
orl@opulex.com 89 MYSQL_2 PRICE.*
orl@opulex.com 89 MYSQL_1 PRODUCT.*
但我期待它如下:
dev@opulex.com 1066 ORACLE_1 DEPT.*
dev@opulex.com 1066 ORACLE_1 EMP.*
dev@opulex.com 1066 ORACLE_2 JOB.*
dev@opulex.com 1066 ORACLE_2 SALARY.*
orl@opulex.com 89 MYSQL_1 PURCHASE.*
orl@opulex.com 89 MYSQL_2 PRICE.*
orl@opulex.com 89 MYSQL_2 PRODUCT.*
由于
答案 0 :(得分:2)
您可以使用此单一awk 命令获取输出:
awk '{sub(/;$/, "")} $1=="HOST"{host=$2} $1=="PORT"{port=$2} $1=="DATABASE"{db=$2}
$1=="SCHEMA"{print host, port, db, $2}' sample2.txt
dev@opulex.com 1066 ORACLE_1 DEPT.*
dev@opulex.com 1066 ORACLE_1 EMP.*
dev@opulex.com 1066 ORACLE_2 JOB.*
dev@opulex.com 1066 ORACLE_2 SALARY.*
orl@opulex.com 89 MYSQL_1 PURCHASE.*
orl@opulex.com 89 MYSQL_2 PRICE.*
orl@opulex.com 89 MYSQL_2 PRODUCT.*
<强>解释强>
sub
函数正在从每行删除尾随;
$1=="HOST"
我们将第二列存储在变量host
$1=="PORT"
我们将第二列存储在变量port
$1=="DATABASE"
我们将第二列存储在变量db
$1=="SCHEMA"
打印host, port, db, 2nd column
答案 1 :(得分:2)
import SpriteKit
class GameScene: SKScene {
//global declarations
let player = SKSpriteNode(imageNamed: "mage")
let fireball = SKSpriteNode(imageNamed: "fireball")
override func didMoveToView(view: SKView) {
/* Setup your scene here */
createScene()
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
spawnFireball(location)
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
self.enumerateChildNodesWithName("fireball", usingBlock: ({
(node,error) in
if (self.fireball.position.x < -self.fireball.size.width/2.0 || self.fireball.position.x > self.size.width+self.fireball.size.width/2.0
|| self.fireball.position.y < -self.fireball.size.height/2.0 || self.fireball.position.y > self.size.height+self.fireball.size.height/2.0) {
self.fireball.removeFromParent()
self.fireball.removeAllChildren()
self.fireball.removeAllActions()
}
}))
}
func createScene() {
//player
player.size = CGSizeMake(100, 100)
player.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2 + 50)
player.zPosition = 2.0
self.addChild(player)
}
func spawnFireball(point: CGPoint) {
//setup
fireball.name = "fireball"
fireball.size = CGSizeMake(100, 50)
let fireballCenter = CGPointMake(fireball.size.width / 4 * 3, fireball.size.height / 2)
fireball.position = player.position
fireball.physicsBody = SKPhysicsBody(circleOfRadius: fireball.size.height/2, center: fireballCenter)
fireball.physicsBody?.affectedByGravity = false
//action
var dx = CGFloat(point.x - player.position.x)
var dy = CGFloat(point.y - player.position.y)
let magnitude = sqrt(dx * dx + dy * dy)
dx /= magnitude
dy /= magnitude
let vector = CGVector(dx: 32.0 * dx, dy: 32.0 * dy)
var rad = atan2(dy,dx)
fireball.runAction(SKAction.rotateToAngle(rad, duration: 0.0))
self.addChild(fireball)
fireball.physicsBody?.applyImpulse(vector)
}
}