In Linux, Merge multiple lines of files into one row and assemble them into SQL statements.

Source: Internet
Author: User

Let's talk about the applicable scenarios. The marketing department wants to see the activity of "scalper" users in some products and the use of coupons for "scalper", and then sends an Excel file, copy the file to notepad ++ in the following format:

ID name
Price
1 name1
Price1
2 name2
Price2
3 name3
Price3
4 name4
Price4
5 name5
Price5
6 name6
Price6
7 name7
Price7
8 name8
Price8
9 name9
Price9

During query, all IDs are organized into one row and written as (1, 2..., 8, 9). This requires the following:

First, delete the first line title, sed '1d 'ids.txt;

Second, only the first column is displayed. awk-F' \ t' {print $1 }';

Third, separate the first column with commas (,), and then use the commas (,) to separate them '',';

Fourth, add the Left and Right brackets and SED's/^/(/'before and after the result in step 3 /(/'.

Group these four steps together and execute: sed '1d 'ids.txt | awk-F' \ t' {print $1} '| tr' \ n '', '| SED's/, $/);/' | SED's/^/(/'> OK .txt

(, 9 );

Then write an SQL statement and splice it with the file. When the data volume is small, the effect is not obvious. When processing tens of millions of data and one text has several GB,

The powerful text processing function of Linux is displayed.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.