awk:从A复制到B并输出..?

时间:2017-06-24 16:13:17

标签: bash awk sed

我的文件是书签,backup-6.session

里面的文件是长字母,我需要复制所有网址(很多)看到这里的例子

......"charset":"UTF-8","ID":3602197775,"docshellID":0,"originalURI":"https://www.youtube.com/watch?v=axxxxxxxxsxsx","docIdentifier":470,"structuredCloneState":"AAAAA.....

结果输出text.txt

https://www.youtube.com/watch?v=axxxxxxxxsxsx
https://www.youtube.com/watch?v=bxxxxxxxxsxsx
https://www.youtube.com/watch?v=cxxxxxxxxsxsx
https://www.youtube.com/watch?v=dxxxxxxxxsxsx
....
....

比A "originalURI":"开始以结束"

命令:AWK,SED ..(我不知道对我来说什么是最好的命令) 谢谢

3 个答案:

答案 0 :(得分:1)

使用GNU awk进行多字符RS和RT:

$ awk -v RS='"originalURI":"[^"]+' 'sub(/.*"/,"",RT){print RT}' file
https://www.youtube.com/watch?v=axxxxxxxxsxsx

答案 1 :(得分:0)

你也可以使用grep,例如:

import SpriteKit
import GameplayKit

// MARK: - GameScene

class GameScene: SKScene {

// MARK: - Allows me to work with vectors. Derived from https://www.raywenderlich.com/145318/spritekit-swift-3-tutorial-beginners

    func subtract(point: CGPoint, fromPoint: CGPoint) -> CGPoint {

        return CGPoint(x: point.x - fromPoint.x, y: point.y - fromPoint.y) //Returns a the first vector minus the second

    }

    func add(point: CGPoint, toPoint: CGPoint) -> CGPoint {

        return CGPoint(x: point.x + toPoint.x, y: point.y + toPoint.y) //Returns a the first vector minus the second

    }

    func multiply(point: CGPoint, by scalar: CGFloat) -> CGPoint {

        return CGPoint(x: point.x * scalar, y: point.y * scalar)

    }

    func divide(point: CGPoint, by scalar: CGFloat) -> CGPoint {

        return CGPoint(x: point.x / scalar, y: point.y / scalar)

    }

    func magnitude(point: CGPoint) -> CGFloat {

        return sqrt(point.x*point.x + point.y*point.y)

    }

    func normalize(aPoint: CGPoint) -> CGPoint {

        return divide(point: aPoint, by: magnitude(point: aPoint))

    }

    // MARK: - Properties

    let minDist: CGFloat = 60

    var userPath: [CGPoint] = [] //Holds the coordinates collected when the user drags their finger accross the screen

    override func didMove(to view: SKView) {



    }

    // MARK: - Helper methods

    func getDistance (fromPoint: CGPoint, toPoint: CGPoint) -> CGFloat
    {

        let deltaX = fromPoint.x - toPoint.x
        let deltaY = fromPoint.y - toPoint.y

        let deltaXSquared = deltaX*deltaX
        let deltaYSquared = deltaY*deltaY

        return sqrt(deltaXSquared + deltaYSquared) //Return the distance

    }

    func touchDown(atPoint pos : CGPoint) {

        userPath = []
        self.removeAllChildren()

        //Get the first point the user makes
        userPath.append(pos)

    }

    func touchMoved(toPoint pos : CGPoint) {

        //Get every point the user makes as they drag their finger across the screen
        userPath.append(pos)

    }

    func touchUp(atPoint pos : CGPoint) {

        //Get the last position the user was left touching when they've completed the motion
        userPath.append(pos)

        //Print the entire path:
        print(userPath)
        print(userPath.count)

        plotNodesAlongPath()

    }

    /**
     Puts nodes equidistance from each other along the path that the user placed
     */
    func plotNodesAlongPath() {

        //Start at the first point
        var currentPoint = userPath[0]

        var circleNodePoints = [currentPoint] //Holds the points that I will then use to generate circle nodes

        for i in 1..<userPath.count {

            let distance = getDistance(fromPoint: currentPoint, toPoint: userPath[i]) //The distance between the point and the next

            if distance >= minDist { //If userPath[i] is at least minDist pixels away

                //Then we can make a vector that points from currentPoint to userPath[i]
                var newNodePoint = subtract(point: userPath[i], fromPoint: currentPoint)

                newNodePoint = normalize(aPoint: newNodePoint) //Normalize the vector so that we have only the direction and a magnitude of 1

                newNodePoint = multiply(point: newNodePoint, by: minDist) //Stretch the vector to a length of minDist so that we now have a point for the next node to be drawn on

                newNodePoint = add(point: currentPoint, toPoint: newNodePoint) //Now add the vector to the currentPoint so that we get a point in the correct position

                currentPoint = newNodePoint //Update the current point. Next we want to draw a point minDist away from the new current point

                circleNodePoints.append(newNodePoint) //Add the new node

            }
            //If distance was less than minDist, then we want to move on to the next point in line

        }

        generateNodesFromPoints(positions: circleNodePoints)

    }

    func generateNodesFromPoints(positions: [CGPoint]) {
        print("generateNodesFromPoints")
        for pos in positions {

            let firstCircleNode = SKShapeNode(circleOfRadius: 5.0)

            firstCircleNode.fillColor = UIColor.blue

            firstCircleNode.strokeColor = UIColor.blue

            firstCircleNode.position = pos //Put the node in the correct position

            self.addChild(firstCircleNode)

        }

    }

    // MARK: - Touch responders

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for t in touches { self.touchDown(atPoint: t.location(in: self)) }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        for t in touches { self.touchMoved(toPoint: t.location(in: self)) }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        for t in touches { self.touchUp(atPoint: t.location(in: self)) }
    }

    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        for t in touches { self.touchUp(atPoint: t.location(in: self)) }
    }


    override func update(_ currentTime: TimeInterval) {
        // Called before each frame is rendered
    }
}

即如果grep -oh "https://www\.youtube\.com/watch?v=[A-Za-z0-9]*" backup-6.session > text.txt 部分仅包含来自A-Z,a-z或数字0-9的字母,并且后面没有任何字母。

注意grep的标志:

axxxxxxxxsxsx

答案 2 :(得分:0)

awk解决方案如下:

awk -F, '{ for (i=1;i<=NF;i++) { if ( $i ~ "originalURI") { spit($i,add,":");print gensub("\"","","g",add[2])":"gensub("\"","","g",add[3])} } }' filename

我们循环遍历用“,”分隔的每个字段,然后对“originalURI”进行模式匹配然后我们使用“:”拆分该字符串,并使用函数gensub拆分和删除引号。

sed解决方案如下:

sed -rn 's/^.*originalURI":"(.*)","docIdentifier.*$/\1/p' filename

使用扩展正则表达式(-r)运行sed并禁止输出(-n)用括号(/ 1)括起来的正则表达式替换字符串,打印结果。