Recombine data with multiple different delimiters [closed]

There is an array of data:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>https://example.com:description of the site/application:category
http://example.com:description of the site/application:category
android://package name:description of the site/application:category
android://package name|description of the site/application|category
</code>
<code>https://example.com:description of the site/application:category http://example.com:description of the site/application:category android://package name:description of the site/application:category android://package name|description of the site/application|category </code>
https://example.com:description of the site/application:category
http://example.com:description of the site/application:category
android://package name:description of the site/application:category
android://package name|description of the site/application|category

I want to split the data into 3 columns:

URL Description Category
https://example.com description of the site/application category
http://example.com description of the site/application category
android://package name description of the site/application category
android://package name description of the site/application category

As I understand it, it is necessary to add a regEx to ignore the first “:” and also 2 argument for the divisor “|”

I tried this expression, but the output is incorrect

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>cat * | awk -F["|"][:] '{print $1,$2, $3}'
</code>
<code>cat * | awk -F["|"][:] '{print $1,$2, $3}' </code>
cat * | awk -F["|"][:] '{print $1,$2, $3}'

New contributor

Fresh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

6

Using any awk:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$ awk -F'[:|]' -v OFS='t' '{sub(/:/,RS); sub(RS,":",$1)} 1' file
https://example.com description of the site/application category
http://example.com description of the site/application category
android://package name description of the site/application category
android://package name description of the site/application category
</code>
<code>$ awk -F'[:|]' -v OFS='t' '{sub(/:/,RS); sub(RS,":",$1)} 1' file https://example.com description of the site/application category http://example.com description of the site/application category android://package name description of the site/application category android://package name description of the site/application category </code>
$ awk -F'[:|]' -v OFS='t' '{sub(/:/,RS); sub(RS,":",$1)} 1' file
https://example.com     description of the site/application     category
http://example.com      description of the site/application     category
android://package name  description of the site/application     category
android://package name  description of the site/application     category

or, if the OFS character can’t be present in the URL in the input:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$ awk -F'[:|]' -v OFS='t' '{$1=$1; sub(OFS,":")} 1' file
https://example.com description of the site/application category
http://example.com description of the site/application category
android://package name description of the site/application category
android://package name description of the site/application category
</code>
<code>$ awk -F'[:|]' -v OFS='t' '{$1=$1; sub(OFS,":")} 1' file https://example.com description of the site/application category http://example.com description of the site/application category android://package name description of the site/application category android://package name description of the site/application category </code>
$ awk -F'[:|]' -v OFS='t' '{$1=$1; sub(OFS,":")} 1' file
https://example.com     description of the site/application     category
http://example.com      description of the site/application     category
android://package name  description of the site/application     category
android://package name  description of the site/application     category

Set OFS to something other than t as you see fit.

Please read the POSIX spec to learn what bracket expression such as the ones you used, ["|"][:], and the one I used, [:|], mean.

Having said that, I suspect the OPs real input probably looks something like this (where additional :s or |s can appear in the URL and/or description, but no literal blanks can be in the URL):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$ cat file
https://example.com:description of : the site/application:category
http://example.com:description: of the site/application:category
android://package%20name:description of the site/application:category
android://package%20name|description of the site/application|category
android://package_name:17:something:description of the :huge: site/application:category
</code>
<code>$ cat file https://example.com:description of : the site/application:category http://example.com:description: of the site/application:category android://package%20name:description of the site/application:category android://package%20name|description of the site/application|category android://package_name:17:something:description of the :huge: site/application:category </code>
$ cat file
https://example.com:description of : the site/application:category
http://example.com:description: of the site/application:category
android://package%20name:description of the site/application:category
android://package%20name|description of the site/application|category
android://package_name:17:something:description of the :huge: site/application:category

and then you can get the output you want using the following sed script (using a sed that has -E to enable EREs, e.g. GNU and BSD seds):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$ sed -E 's/([^ ]+)[:|]([^ ].*)[:|]/1t2t/' file
https://example.com description of : the site/application category
http://example.com description: of the site/application category
android://package%20name description of the site/application category
android://package%20name description of the site/application category
android://package_name:17:something description of the :huge: site/application category
</code>
<code>$ sed -E 's/([^ ]+)[:|]([^ ].*)[:|]/1t2t/' file https://example.com description of : the site/application category http://example.com description: of the site/application category android://package%20name description of the site/application category android://package%20name description of the site/application category android://package_name:17:something description of the :huge: site/application category </code>
$ sed -E 's/([^ ]+)[:|]([^ ].*)[:|]/1t2t/' file
https://example.com     description of : the site/application   category
http://example.com      description: of the site/application    category
android://package%20name        description of the site/application     category
android://package%20name        description of the site/application     category
android://package_name:17:something     description of the :huge: site/application      category

or using any sed:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$ sed 's/([^ ]*)[:|]([^ ].*)[:|]/1t2t/' file
https://example.com description of : the site/application category
http://example.com description: of the site/application category
android://package%20name description of the site/application category
android://package%20name description of the site/application category
android://package_name:17:something description of the :huge: site/application category
</code>
<code>$ sed 's/([^ ]*)[:|]([^ ].*)[:|]/1t2t/' file https://example.com description of : the site/application category http://example.com description: of the site/application category android://package%20name description of the site/application category android://package%20name description of the site/application category android://package_name:17:something description of the :huge: site/application category </code>
$ sed 's/([^ ]*)[:|]([^ ].*)[:|]/1t2t/' file
https://example.com     description of : the site/application   category
http://example.com      description: of the site/application    category
android://package%20name        description of the site/application     category
android://package%20name        description of the site/application     category
android://package_name:17:something     description of the :huge: site/application      category

Those sed commands assume a description contains at least 1 blank and doesn’t start with a : or word:word – if that’s not the case then there is no way to separate a description from a URL given what we know so far about the input.

2

If format can be one of:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>url:description:category
</code>
<code>url:description:category </code>
url:description:category
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>url|description|category
</code>
<code>url|description|category </code>
url|description|category

and only url can contain extra : or |, then to change to tab separators with sed:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>sed -E 's/(.*)([:|])(.*)2(.*)/1t3t4/' file
</code>
<code>sed -E 's/(.*)([:|])(.*)2(.*)/1t3t4/' file </code>
sed -E 's/(.*)([:|])(.*)2(.*)/1t3t4/' file

or slightly more efficiently:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>sed -E 's/(.*)([:|])(.*)2/1t3t/' file
</code>
<code>sed -E 's/(.*)([:|])(.*)2/1t3t/' file </code>
sed -E 's/(.*)([:|])(.*)2/1t3t/' file

The first .* consumes as much as possible, so also consumes any surplus : and |

Change t if tabs are not the desired new separators.

You may notice that you have 4 fields per line when splitting with [:|]:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>awk -F '[:|]' '{ print NF }' infile
</code>
<code>awk -F '[:|]' '{ print NF }' infile </code>
awk -F '[:|]' '{ print NF }' infile

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>4
4
4
4
</code>
<code>4 4 4 4 </code>
4
4
4
4

So, assuming all your lines are formatted in this way, you can get 3 columns by joining fields 1 and 2, e.g.:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>awk -F '[:|]' -v OFS='t' '{ print $1":"$2, $3, $4 }' infile
</code>
<code>awk -F '[:|]' -v OFS='t' '{ print $1":"$2, $3, $4 }' infile </code>
awk -F '[:|]' -v OFS='t' '{ print $1":"$2, $3, $4 }' infile

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>https://example.com description of the site/application category
http://example.com description of the site/application category
android://package name description of the site/application category
android://package name description of the site/application category
</code>
<code>https://example.com description of the site/application category http://example.com description of the site/application category android://package name description of the site/application category android://package name description of the site/application category </code>
https://example.com     description of the site/application     category
http://example.com      description of the site/application     category
android://package name  description of the site/application     category
android://package name  description of the site/application     category

9

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật