awk

How to delete all the lines after the last occurence of pattern?

醉酒当歌 提交于 2021-01-07 02:36:00
问题 i want to delete all the lines after the last occurence of pattern except the pattern itself file.txt honor apple redmi nokia apple samsung lg htc file.txt what i want honor apple redmi nokia apple what i have tried sed -i '/apple/q' file.txt this deletes all the line after the first occurence of pattern - honor 回答1: Simple, robust 2-pass approach using almost no memory: $ awk 'NR==FNR{if (/apple/) hit=NR; next} {print} FNR==hit{exit}' file file honor apple redmi nokia apple If that doesn't

Find nearest point from file1 in file2, shell skript

百般思念 提交于 2021-01-05 08:56:23
问题 I have 2 files: file1 -3241.42 633.261 1210.53 -1110.89 735.349 836.635 (this is the points I am looking for, with coordinates x,y,z) file2 2014124 -2277.576 742.75 962.5816 0 0 2036599 -3236.882 638.748 1207.804 0 0 2036600 -3242.417 635.2612 1212.527 0 0 2036601 -3248.006 631.6553 1217.297 0 0 2095885 -1141.905 737.7666 843.3465 0 0 2095886 -1111.889 738.3486 833.6354 0 0 2095887 -1172.227 737.4004 853.9965 0 0 2477149 -3060.679 488.6802 1367.816 0 0 2477150 -3068.369 489.6621 1365.769 0 0

Find nearest point from file1 in file2, shell skript

心不动则不痛 提交于 2021-01-05 08:55:31
问题 I have 2 files: file1 -3241.42 633.261 1210.53 -1110.89 735.349 836.635 (this is the points I am looking for, with coordinates x,y,z) file2 2014124 -2277.576 742.75 962.5816 0 0 2036599 -3236.882 638.748 1207.804 0 0 2036600 -3242.417 635.2612 1212.527 0 0 2036601 -3248.006 631.6553 1217.297 0 0 2095885 -1141.905 737.7666 843.3465 0 0 2095886 -1111.889 738.3486 833.6354 0 0 2095887 -1172.227 737.4004 853.9965 0 0 2477149 -3060.679 488.6802 1367.816 0 0 2477150 -3068.369 489.6621 1365.769 0 0

Remove lines after first match

喜夏-厌秋 提交于 2021-01-04 05:36:12
问题 I need to remove all lines after the first line that contains the word "fox" Say for example for the following input file "in.txt" The quick brown fox jumps over the lazy dog the second fox is hunting The result will be The quick brown fox jumps I prefer a script made in awk or sed but any other command line tools are good, like perl or php or python etc. For searching purposes I will add this line: Remove lines after occurrence Later edit: I am using gnuwin32 tools in Windows, and the

Convert Rows to Columns Shell Script

♀尐吖头ヾ 提交于 2021-01-04 05:32:47
问题 I have data in the below format APP_OWNER : hari APP_AREA : Work:Business Area:AUS APP_ID : 124080 I want the data to be converted to below format. APP_OWNER,APP_AREA,APP_ID hari,Work:Business Area:AUS,124080 I can convert one line but how to do it with 3 lines at the same time? My Attempt with one line sed '0,/: /s//\n/' test.txt Regards 回答1: You may try this awk solution: awk -F '[[:blank:]]+:[[:blank:]]+' '{ v1 = (v1 == "" ? "" : v1 ",") $1 v2 = (v2 == "" ? "" : v2 ",") $2 } END { print v1

Adding columns to a csv table with AWK from multiple files

此生再无相见时 提交于 2021-01-03 08:40:43
问题 I'm looking to build a csv table by getting values from several files with AWK. I have it working with two files, but I can't scale it beyond that. I'm currently taking the output of the second file, and appending the third, and so on. Here are example files: #file1 #file2 #file3 #file4 100 45 1 5 200 23 1 2 300 29 2 1 400 0 1 2 500 74 4 5 This is the goal: #data.csv 1,100,45,1,5 2,200,23,1,2 3,300,29,2,1 4,400,0,1,2 5,500,74,4,5 This is what I have working: awk 'FNR==NR { a[FNR""] = NR", "

Count number of line in txt file when new line is inside data

时光怂恿深爱的人放手 提交于 2021-01-02 18:08:25
问题 I have one txt file which has below data Name mobile url message text test11 1234567890 www.google.com "Data Test New Date:27/02/2020 Items: 1 Total: 3 Regards ABC DATa Ph:091 : 123456789" test12 1234567891 www.google.com "Data Test New one Date:17/02/2020 Items: 26 Total: 5 Regards user test Ph:091 : 433333333" Now you can see my last column data has new line character. so when I use below command awk 'END{print NR}' file.txt it is giving my length is 15 but actually line length is 3 .

Count number of line in txt file when new line is inside data

[亡魂溺海] 提交于 2021-01-02 18:05:29
问题 I have one txt file which has below data Name mobile url message text test11 1234567890 www.google.com "Data Test New Date:27/02/2020 Items: 1 Total: 3 Regards ABC DATa Ph:091 : 123456789" test12 1234567891 www.google.com "Data Test New one Date:17/02/2020 Items: 26 Total: 5 Regards user test Ph:091 : 433333333" Now you can see my last column data has new line character. so when I use below command awk 'END{print NR}' file.txt it is giving my length is 15 but actually line length is 3 .

Count number of line in txt file when new line is inside data

烈酒焚心 提交于 2021-01-02 18:03:45
问题 I have one txt file which has below data Name mobile url message text test11 1234567890 www.google.com "Data Test New Date:27/02/2020 Items: 1 Total: 3 Regards ABC DATa Ph:091 : 123456789" test12 1234567891 www.google.com "Data Test New one Date:17/02/2020 Items: 26 Total: 5 Regards user test Ph:091 : 433333333" Now you can see my last column data has new line character. so when I use below command awk 'END{print NR}' file.txt it is giving my length is 15 but actually line length is 3 .

Count number of line in txt file when new line is inside data

不打扰是莪最后的温柔 提交于 2021-01-02 18:03:15
问题 I have one txt file which has below data Name mobile url message text test11 1234567890 www.google.com "Data Test New Date:27/02/2020 Items: 1 Total: 3 Regards ABC DATa Ph:091 : 123456789" test12 1234567891 www.google.com "Data Test New one Date:17/02/2020 Items: 26 Total: 5 Regards user test Ph:091 : 433333333" Now you can see my last column data has new line character. so when I use below command awk 'END{print NR}' file.txt it is giving my length is 15 but actually line length is 3 .