簡體   English   中英

將帶有 awk 命令的 bash 腳本應用於文件

[英]Apply bash script with awk-commands to file

我目前正在使用 bash 腳本來自動執行正則表達式列表以獲得清理文件的鏈接列表。 目前我在 kate 上使用查找/替換手動完成所有操作,但將其作為腳本會更舒服。 由於我對 bash 腳本編寫還很陌生,所以我向您尋求幫助。

網址列表示例:

0: "/suburl0"
​
1: "/suburl1"
​
2: "/suburl2"
​
3: "/suburl3"
​
4: "/suburl4"

目前我有腳本:

#!/bin/bash
awk '[^\x00-\x7F]+' $1 #there are non-ascii chars in the file, so clean it out
awk 'NF' $1 # remove non-character lines
awk '^[0-900]{0,3}: ' $1 #delete all those number infront of the link
awk '"' $1 # remove those quotation marks
awk '!seen[$0]++' $1 #remove duplicate lines
awk '{print "http://example.com/" $0}' $1 #prepend the full url to the suburl

目標是將所有這些正則表達式應用於文件,因此文件最終會被清理干凈

我的猜測是,我沒有正確重定向 awk 的輸出,但是當我試圖將其通過管道傳輸到文件中時,該文件只是空行。

你想要的或多或少的翻譯,不限於 awk:

cat $1 \
        | tr -cd '[:print:][:space:]' \
        | grep . \
        | sed -r 's/^[0-9]{1,3}: //' \
        | tr -d '"' \
        | sort -u \
        | awk '{print "http://example.com" $0}'

請注意, sort會改變順序,我假設順序無關緊要。

還要注意sed -r是 GNU。

稍微簡化和更便攜的版本:

cat $1 \
        | tr -cd '[:graph:]\n' \
        | grep . \
        | tr -d '"' \
        | sort -u \
        | sed 's,^[0-9]*:,http://example.com,'

輸出:

http://example.com/suburl0
http://example.com/suburl1
http://example.com/suburl2
http://example.com/suburl3
http://example.com/suburl4

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM