每两行组合并对齐

时间:2016-09-28 16:40:14

标签: bash awk sed

如何使用sedawkbash最简洁地将文件格式A转换为B?

A

1
blabla
2
another blabla
... (more omitted)
10
yet another blabla
...
100
final blabla

1   blabla
2   another blabla
...
10  yet another blabla
...
100 final blabla

3 个答案:

答案 0 :(得分:1)

有很多不同的方式,这里有一个使用paste

$ cat ip.txt 
1
blabla
2
another blabla
10
yet another blabla
100
final blabla

$ paste - - < ip.txt 
1   blabla
2   another blabla
10  yet another blabla
100 final blabla

有关更多方法,请参阅How to process a multi column text file to get another multi column text file?

答案 1 :(得分:0)

在一个bash行中:

while read line1; do if read line2; then echo "$line1" "$line2"; fi; done < file.txt

答案 2 :(得分:0)

Bash中使用$ pr -2 -a -s\ -t foo2 1 blabla 2 another blabla 10 yet another blabla 100 final blabla

$job['description'] = ltrim($job['description']);
$job['description'] = preg_replace('/^\s+/S', " ", $job['description']);