簡體   English   中英

在bash中轉置文本文件

[英]Transposing a text file in bash

我有一個文件名為

Input1 file1:45764 | file1:878755 | file1: 899787 
Input2 file1: 45676 | file1:769678 | file1: 6454764 

現在我想這樣做

Input1 file1:45764
Input1 file1:878755
Input1 file1: 899787
Input2 file1: 45676
Input2 file1:769678
Input2 file1: 6454764

有猜到嗎? 我試過sed "s/s/n/g"sed "s/s+/n/g"但沒有成功嗎?

您可以執行以下操作:

$ awk -F' [|] ' '{split($1,fld,/ /);print $1;for(i=2;i<=NF;i++)print fld[1],$i}' file
Input1 file1:45764
Input1 file1:878755
Input1 file1: 899787
Input2 file1: 45676
Input2 file1:769678
Input2 file1: 6454764

或者習慣上,您可以:

$ awk 'gsub(/[|]/,ORS $1)' file
Input1 file1:45764
Input1 file1:878755
Input1 file1: 899787
Input2 file1: 45676
Input2 file1:769678
Input2 file1: 6454764

只是猛擊:

while read input line; do
    IFS="|" read -a words <<< "$line"
    printf "$input %s\n" "${words[@]}"
done << END
Input1 file1:45764 | file1:878755 | file1: 899787 
Input2 file1: 45676 | file1:769678 | file1: 6454764
END
Input1 file1:45764 
Input1  file1:878755 
Input1  file1: 899787
Input2 file1: 45676 
Input2  file1:769678 
Input2  file1: 6454764

暫無
暫無

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

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