awk

Use awk to find first occurrence only of string after a delimiter

半城伤御伤魂 提交于 2021-02-17 18:47:00
问题 I have a bunch of documents that all have the line, Account number: 123456789 in various locations. What I need to do is be able to parse through the files, and find the account number itself. So, awk needs to look for Account number: and return the string immediately following. For example, if it was: Account number: 1234567 awk should return: 1234567 Once it's found the first occurrence it can stop looking. But, I'm stumped. What's the right way to do this using awk ? 回答1: One way: awk -F:

Use awk to find first occurrence only of string after a delimiter

荒凉一梦 提交于 2021-02-17 18:45:37
问题 I have a bunch of documents that all have the line, Account number: 123456789 in various locations. What I need to do is be able to parse through the files, and find the account number itself. So, awk needs to look for Account number: and return the string immediately following. For example, if it was: Account number: 1234567 awk should return: 1234567 Once it's found the first occurrence it can stop looking. But, I'm stumped. What's the right way to do this using awk ? 回答1: One way: awk -F:

Use awk to find first occurrence only of string after a delimiter

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-17 18:45:16
问题 I have a bunch of documents that all have the line, Account number: 123456789 in various locations. What I need to do is be able to parse through the files, and find the account number itself. So, awk needs to look for Account number: and return the string immediately following. For example, if it was: Account number: 1234567 awk should return: 1234567 Once it's found the first occurrence it can stop looking. But, I'm stumped. What's the right way to do this using awk ? 回答1: One way: awk -F:

Use awk to find first occurrence only of string after a delimiter

北战南征 提交于 2021-02-17 18:44:19
问题 I have a bunch of documents that all have the line, Account number: 123456789 in various locations. What I need to do is be able to parse through the files, and find the account number itself. So, awk needs to look for Account number: and return the string immediately following. For example, if it was: Account number: 1234567 awk should return: 1234567 Once it's found the first occurrence it can stop looking. But, I'm stumped. What's the right way to do this using awk ? 回答1: One way: awk -F:

Use awk to find first occurrence only of string after a delimiter

╄→尐↘猪︶ㄣ 提交于 2021-02-17 18:43:20
问题 I have a bunch of documents that all have the line, Account number: 123456789 in various locations. What I need to do is be able to parse through the files, and find the account number itself. So, awk needs to look for Account number: and return the string immediately following. For example, if it was: Account number: 1234567 awk should return: 1234567 Once it's found the first occurrence it can stop looking. But, I'm stumped. What's the right way to do this using awk ? 回答1: One way: awk -F:

awk

半城伤御伤魂 提交于 2021-02-17 09:53:35
awk工具 截取文档中的某个段 [root@localhost ~]# mkdir awk [root@localhost ~]# cp /etc/passwd awk/test.txt [root@localhost ~]# cd awk [root@localhost awk]# ls test.txt [root@localhost awk]# awk -F ':' '{print $1}' test.txt root bin daemon adm lp sync shutdown halt mail operator games ftp nobody systemd-bus-proxy systemd-network dbus polkitd tss postfix sshd user1 user2 test -F 指定分隔符 。 print为打印的动作,用来打印某个字段。$1为第1个字段。 $0比较特殊,表示整行。 [root@localhost awk]# head -n2 test.txt |awk -F ':' '{print $0}' root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin 匹配字符或者字符串(实现grep的功能,但没有颜色显示,肯定没有grep用起来方便) [root

How to align columns with awk

浪子不回头ぞ 提交于 2021-02-17 07:07:58
问题 I tried to execute below command: ls -ltr | awk 'BEGIN { print "File\t\t\tOwner"} { print $9,"\t",$3} END {print "-DONE \n"}' and getting following o/p File Owner inventory-shipped root BBB_list root marks root test_file root awkvars.out root 1 root t12 root -DONE How i can get o/p like File Owner inventory-shipped root BBB_list root marks root test_file root awkvars.out root 1 root t12 root -DONE 回答1: You need to use padding except for the last column. Since you have only 2 columns, the

Dynamic regular expressions in awk

ぃ、小莉子 提交于 2021-02-17 05:46:29
问题 I have text files like 1.txt AA;00000; BB;11111; GG;22222; 2.txt KK;WW;55555;11111; KK;FF;ZZ;11111; KK;RR;YY;11111; I generate this 3.txt output AA;00000; BB;11111;KK;WW;55555;FF;ZZ;RR;YY GG;22222; with this .awk script (I use it in Windows with cmd) #!/usr/bin/awk -f NR != FNR { exit } { printf "%s", $0 } /^BB/ { o = "" while (getline tmp < ARGV[2]) { n = split (tmp,arr,";") for (i=1; i<=n; i++) if(!match($0,arr[i]) && !match(o,arr[i])) o=o arr[i]";" } printf "%s", o } { print "" } Usage is

Awk asking combine two files

纵饮孤独 提交于 2021-02-16 20:17:37
问题 I have combined two different files with Same Key by AWK command. And In case there is no key match compare to File1 and File2 then just put "\t\t\t" instead. I have below AWK command. awk -F"\t" ' {key = $1} NR == 1 {header = key} !(key in result) {result[key] = $0 ; next} { for (i=2; i <= NF; i++) result[key] = result[key] FS $i } END { print result[header],"\tValue2","\tValue3","\tValue4" delete result[header] PROCINFO["sorted_in"] = "@ind_str_asc" # if using GNU awk for (key in result)

Split file by vector of line numbers

爱⌒轻易说出口 提交于 2021-02-16 20:10:35
问题 I have a large file, about 10GB. I have a vector of line numbers which I would like to use to split the file. Ideally I would like to accomplish this using command-line utilities. As a regex: File: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Vector of line numbers: 2 5 Desired output: File 1: 1 2 3 File 2: 4 5 6 7 8 9 10 11 12 File 3: 13 14 15 16 17 18 回答1: This might work for you: csplit -z file 2 5 or if you want regexp: csplit -z file /2/ /5/ With the default values, the output files will