awk

Renumbering duplicate lines with counter awk

跟風遠走 提交于 2021-02-05 08:38:08
问题 I have duplicate words in csv. And i need to count it in such way: jsmith jsmith kgonzales shouston dgenesy kgonzales jsmith to this: jsmith@email.com jsmith1@email.com kgonzales@email.com shouston@email.com dgenesy@email.com kgonzales1@email.com jsmith2@email.com I have smth like that, but it doesn't work properly for me..or i cant do it enter link description here 回答1: A simple way to do it is maintain an array using the username as the index and increment it each time you read a user, e.g.

How to move first column to last column in unix?

淺唱寂寞╮ 提交于 2021-02-05 06:37:26
问题 This is the data in a text file. 0.354167 male atyp_angina 0.066038 0.1621 t normal 0.648855 no 0 up 0 reversable_defect <50 0.625 male typ_angina 0.792453 0.328767 f left_vent_hyper 0.564885 no 0.677419 down 0 reversable_defect <50 0.645833 male non_anginal 0.433962 0.134703 f left_vent_hyper 0.641221 no 0.483871 flat 0 normal >50_1 0.666667 female asympt 0.481132 0.413242 f left_vent_hyper 0.572519 yes 0.16129 flat 0 reversable_defect >50_1 0.270833 male typ_angina 0.509434 0.269406 f left

Is there a way to avoid “newline in string” errors in awk?

China☆狼群 提交于 2021-02-05 06:21:50
问题 How are cmdline args containing newline chars passed to awk ? See two examples below: awk -v s='text' 'BEGIN { print(s) }' text awk -v s=$'\n''text' 'BEGIN { print(s) }' awk: newline in string text... at source line 1 回答1: What you have would work as-is with gawk. With OSX (BSD) awk like you're using, either don't put a linefeed character in the string. $ awk -v s='\ntext' 'BEGIN{ print s }' text or escape it: awk -v s='\ text' 'BEGIN{ print s }' text or (portably with any awk) don't pass it

Is there a way to avoid “newline in string” errors in awk?

六眼飞鱼酱① 提交于 2021-02-05 06:21:48
问题 How are cmdline args containing newline chars passed to awk ? See two examples below: awk -v s='text' 'BEGIN { print(s) }' text awk -v s=$'\n''text' 'BEGIN { print(s) }' awk: newline in string text... at source line 1 回答1: What you have would work as-is with gawk. With OSX (BSD) awk like you're using, either don't put a linefeed character in the string. $ awk -v s='\ntext' 'BEGIN{ print s }' text or escape it: awk -v s='\ text' 'BEGIN{ print s }' text or (portably with any awk) don't pass it

grep lines that start with a specific string

你。 提交于 2021-02-05 00:59:27
问题 I want to find all the lines in a file that start with a specific string. The problem is, I don't know what's in the string beforehand. The value is stored in a variable. The naïve solution would be the following: grep "^${my_string}" file.txt; Because if the Bash variable my_string contains ANY regular expression special characters, grep will cry, and everyone will have a bad day. You don't want to make grep cry, do you? 回答1: You should use awk instead of grep for non-regex search using

grep lines that start with a specific string

こ雲淡風輕ζ 提交于 2021-02-05 00:56:30
问题 I want to find all the lines in a file that start with a specific string. The problem is, I don't know what's in the string beforehand. The value is stored in a variable. The naïve solution would be the following: grep "^${my_string}" file.txt; Because if the Bash variable my_string contains ANY regular expression special characters, grep will cry, and everyone will have a bad day. You don't want to make grep cry, do you? 回答1: You should use awk instead of grep for non-regex search using

Awk argument too long when merging csv files

房东的猫 提交于 2021-02-04 21:07:00
问题 I have more than 10000 csv files in a folder and I'm trying to merge them by line using awk but if I run this command: printf '%s\n' *.csv | xargs cat | awk 'FNR==1 && NR!=1{next;}{print}' *.csv > master.csv I get the following errors: /usr/bin/awk: Argument list too long and printf: write error: Broken pipe 回答1: With the printf and xargs parts, you are sending the contents of the csv files into awk, but you also provide the filenames to awk. Pick one or the other: I'd suggest: { printf '%s\n

awk - Variable expansion in regex

瘦欲@ 提交于 2021-02-04 19:39:40
问题 Why doesn't something like this work: echo 4 | awk --assign=abc=4 '/$abc/' The actual example is much more complicated. Basically I have a regex I need repeated several times so I'm storing it in abc . Is there any way to expand an awk variable in /<regex>/ ? I've tried single and double quotes, every combination. I really need that line to be single quoted because I have several double quotes, it actually looks more like awk --assign=test=something '/$test/ { a lot of stuff here inc $test

awk - Variable expansion in regex

北城以北 提交于 2021-02-04 19:39:19
问题 Why doesn't something like this work: echo 4 | awk --assign=abc=4 '/$abc/' The actual example is much more complicated. Basically I have a regex I need repeated several times so I'm storing it in abc . Is there any way to expand an awk variable in /<regex>/ ? I've tried single and double quotes, every combination. I really need that line to be single quoted because I have several double quotes, it actually looks more like awk --assign=test=something '/$test/ { a lot of stuff here inc $test

Replacing string in linux using sed/awk based

血红的双手。 提交于 2021-02-04 18:16:13
问题 i want to replace this #!/usr/bin/env bash with this #!/bin/bash i have tried two approaches Approach 1 original_str="#!/usr/bin/env bash" replace_str="#!/bin/bash" sed s~${original_str}~${replace_str}~ filename Approach 2 line=`grep -n "/usr/bin" filename` awk NR==${line} {sub("#!/usr/bin/env bash"," #!/bin/bash")} But both of them are not working. 回答1: You cannot use ! inside a double quotes in BASH otherwise history expansion will take place. You can just do: original_str='/usr/bin/env