簡體   English   中英

bash 當你找到兩列時刪除 bash 中的第一列

[英]bash when you find two columns delete the first column in bash

我有兩列如下

0:0:36 hostname-x.y.com:1212/dbname
server.y.com:1012/bname
0:0:9 commom.y.com:9312/xname
windows.y.com:1052/wname

期望輸出應如下

hostname-x.y.com:1212/dbname
server.y.com:1012/bname
commom.y.com:9312/xname
windows.y.com:1052/wname

基本上,當它找到兩列時,它應該跳過第一列。 我有將近 10k 行。 任何建議都會有很大幫助。

這可以用awk簡單地完成:

awk '{print $NF}' filename

這將簡單地打印每一行的最后一個字段。

示例使用/輸出

使用 file 中的輸入file

$ awk '{print $NF}' file
hostname-x.y.com:1212/dbname
server.y.com:1012/bname
commom.y.com:9312/xname
windows.y.com:1052/wname

通過查看您的預期輸出樣本(添加更通用的解決方案),如果我正確理解(您想在數字后打印數據),請嘗試以下操作。

awk 'match($0,/[0-9]+:[0-9]+:[0-9]+ +/){print substr($0,RSTART+RLENGTH);next} 1' Input_file

說明:為上述代碼添加詳細說明。

awk '                                    ##Starting awk program from here.
match($0,/[0-9]+:[0-9]+:[0-9]+ +/){      ##Using match function which matches regex of digits colon digits colon digits then space in line.
  print substr($0,RSTART+RLENGTH)        ##If a match is found then it should print sub-string of line starting from RSTART till RLENGTH.
  next                                   ##next will skip all further statements from here.
}
1                                        ##1 will print edited/non-edited lines here.
'  Input_file                            ##Mentioning Input_file name here.
sed 's/.* //g' < input_file

演示


:>cat test.txt
0:0:36 hostname-x.y.com:1212/dbname
server.y.com:1012/bname
0:0:9 commom.y.com:9312/xname
windows.y.com:1052/wname
:>sed 's/.* //g' test.txt
hostname-x.y.com:1212/dbname
server.y.com:1012/bname
commom.y.com:9312/xname
windows.y.com:1052/wname
:>



使用 cut 取第二個字段,以“ ”作為分隔符。 如果未找到分隔符,則在輸出中給出整行,如 -f 選項幫助部分 (cut --help) 所指定

-f, --fields=LIST       select only these fields;  also print any line
                            that contains no delimiter character, unless
                            the -s option is specified

命令應該是

cut -f2 -d" " yourfile

演示:

root@a036fb1c94fa:~# cat yourfile
0:0:36 hostname-x.y.com:1212/dbname
server.y.com:1012/bname
0:0:9 commom.y.com:9312/xname
windows.y.com:1052/wname 

root@a036fb1c94fa:~# cut -f2 -d" " yourfile
hostname-x.y.com:1212/dbname
server.y.com:1012/bname
commom.y.com:9312/xname
windows.y.com:1052/wname

暫無
暫無

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

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