Combining two files into one in a specific format - Bash [closed]

痞子三分冷 提交于 2020-06-17 09:09:42

问题


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

(file1,file2)

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

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 )

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!!!


回答1:


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

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)




回答2:


One sed solution:

$ paste -d, name.txt price.txt | sed "/^,/d;/,$/d;s/,/, /;s/^/(/;s/$/)\n/"
(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)

Where:

  • /^,/d - delete lines that start with a comma (ie, no matching row from first file)
  • /,$/d - delete lines that end with a comma (ie, no matching row from second file)
  • s/,/, / - add space after comma
  • s/^/(/ - add left paren to beginning of line
  • s/$/)\n/ - add right paren and extra linefeed (\n) to end of line

One awk solution:

$ paste -d, name.txt price.txt | awk -F"," '$1=="" { next } $2=="" { next } { printf "(%s, %s)\n\n",$1,$2 }'
(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)

Where:

  • -F "," - use comma as input field delimiter
  • $1=="" { next } - if first field is blank then skip line (ie, no matching row from first file)
  • $2== "" { next } - if second field is blank then skip line (ie, no matching row from second file)
  • printf ... - print line with parenthesis wrappers, comma+space delimiter, and extra linefeed on the end


来源:https://stackoverflow.com/questions/62247461/combining-two-files-into-one-in-a-specific-format-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!