以特定格式将两个文件合并为一个-Bash [英] Combining two files into one in a specific format - Bash

查看:95
本文介绍了以特定格式将两个文件合并为一个-Bash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将3个文件合并为1个文件,其格式类似于

I want to combine 3 files into 1 file in such a way format that it resembles

(file1,file2)

我有2个文件,即name.txt,price.txt

I have 2 files name.txt, price.txt

Name.txt

Cappuccino with Milk Chocolate Cookie
Cappuccino with Double Chocolate Cookie
Latte with Milk Chocolate Cookie
Latte with Double Chocolate Cookie

price.txt

Rs 288
Rs 288
Rs 288
Rs 288
Rs 159

我真正想要的是一个新文件(deals.txt)应该包含以下格式的这两个文件中的数据

What I actually want is that a new file (deals.txt) should contain the data from these 2 files in the following format

(Cappuccino with Milk Chocolate Cookie, Rs 288)
#there should be a space inbetween 
(Cappuccino with Double Chocolate Cookie, Rs 288)
#there should be a space inbetween 
(Latte with Milk Chocolate Cookie, Rs 288 )

我正在使用此逻辑,但是它不符合我想要的格式 paste name.txt price.txt >> price+name.txt 它给出如下输出:

I am using this logic but it doesn't fulfill the format that i want paste name.txt price.txt >> price+name.txt it gives an output like:

Cappuccino with Milk Chocolate Cookie   Rs 288
Cappuccino with Double Chocolate Cookie Rs 288

行之间没有空格,名称和价格之间没有逗号.请帮忙!!!

There is no space between the lines and no comma between the name and price. Please help!!!

推荐答案

此基于循环的解决方案:

This loop-based solution:

#!/bin/bash

for LINE in $(nl Name.txt |awk '{ print $1 }'); do
  echo "($(sed -n "${LINE}p" Name.txt), $(sed -n "${LINE}p" price.txt))"
  echo -en '\n'
done >> deals.txt

将创建一个文件deals.txt,其中包含:

Will create a file deals.txt containing:

(Cappuccino with Milk Chocolate Cookie, Rs 288)

(Cappuccino with Double Chocolate Cookie, Rs 288)

(Latte with Milk Chocolate Cookie, Rs 288)

(Latte with Double Chocolate Cookie, Rs 288)

这篇关于以特定格式将两个文件合并为一个-Bash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆