awk

Why the output of array using awk is not in right order?

岁酱吖の 提交于 2021-01-20 07:28:20
问题 I have a string: Gatto piu bello anche cane in file. I am using awk to split it and to put it into array. But the output is not in the right order. My code is: while (getline < "'"$INPUTFILE"'") { text = $0; } split (text,text_arr," "); for (i in text_arr) { print text_arr[i]; } $INPUTFILE is file with that string. But the output of this code is: anche cane Gatto piu bello I have no idea what's the problem. 回答1: awk doesn't actually have indexed arrays; it only has associative arrays. This

Trying to remove non-printable charaters(junk values) from a UNIX file

蓝咒 提交于 2021-01-20 04:18:50
问题 I am trying to remove non-printable character (for e.g. ^@ ) from records in my file. Since the volume to records is too big in the file using cat is not an option as the loop is taking too much time. I tried using sed -i 's/[^@a-zA-Z 0-9`~!@#$%^&*()_+\[\]\\{}|;'\'':",.\/<>?]//g' FILENAME but still the ^@ characters are not removed. Also I tried using awk '{ sub("[^a-zA-Z0-9\"!@#$%^&*|_\[](){}", ""); print } FILENAME > NEW FILE but it also did not help. Can anybody suggest some alternative

Trying to remove non-printable charaters(junk values) from a UNIX file

一曲冷凌霜 提交于 2021-01-20 04:15:04
问题 I am trying to remove non-printable character (for e.g. ^@ ) from records in my file. Since the volume to records is too big in the file using cat is not an option as the loop is taking too much time. I tried using sed -i 's/[^@a-zA-Z 0-9`~!@#$%^&*()_+\[\]\\{}|;'\'':",.\/<>?]//g' FILENAME but still the ^@ characters are not removed. Also I tried using awk '{ sub("[^a-zA-Z0-9\"!@#$%^&*|_\[](){}", ""); print } FILENAME > NEW FILE but it also did not help. Can anybody suggest some alternative

How do I find the text that matches a pattern?

自作多情 提交于 2021-01-18 04:34:15
问题 NOTE: This is not a duplicate of any existing question, it's intended to show why such an extremely common and seemingly simple question is unanswerable and provide guidance on how people posting such questions can modify them to make them answerable so we don't have to keep providing the same guidance in comments almost every day and can just refer to this instead. Given the following input file: foo o.b bar I need to output all lines that match the pattern o.b so my expected output is: o.b

How do I find the text that matches a pattern?

家住魔仙堡 提交于 2021-01-18 04:33:17
问题 NOTE: This is not a duplicate of any existing question, it's intended to show why such an extremely common and seemingly simple question is unanswerable and provide guidance on how people posting such questions can modify them to make them answerable so we don't have to keep providing the same guidance in comments almost every day and can just refer to this instead. Given the following input file: foo o.b bar I need to output all lines that match the pattern o.b so my expected output is: o.b

Print column contents by column name

僤鯓⒐⒋嵵緔 提交于 2021-01-18 04:09:00
问题 I want to input a string name (i.e. "COL2") to an awk or cut command and print the column that matches that column header string. the datafile looks like this: COL1 COL2 COL3 COL4 COL5 COL6 a a b d c f a d g h e f c v a s g a If I pass in COL3, I want it to print the third column, etc. I'm thinking awk might be the easiest thing to use, but cut may also work. I'm just not sure how to go about doing this. 回答1: Awk 1 liner for above problem (if you are interested): awk -v col=COL2 'NR==1{for(i

Print column contents by column name

那年仲夏 提交于 2021-01-18 03:55:58
问题 I want to input a string name (i.e. "COL2") to an awk or cut command and print the column that matches that column header string. the datafile looks like this: COL1 COL2 COL3 COL4 COL5 COL6 a a b d c f a d g h e f c v a s g a If I pass in COL3, I want it to print the third column, etc. I'm thinking awk might be the easiest thing to use, but cut may also work. I'm just not sure how to go about doing this. 回答1: Awk 1 liner for above problem (if you are interested): awk -v col=COL2 'NR==1{for(i

Print column contents by column name

这一生的挚爱 提交于 2021-01-18 03:55:00
问题 I want to input a string name (i.e. "COL2") to an awk or cut command and print the column that matches that column header string. the datafile looks like this: COL1 COL2 COL3 COL4 COL5 COL6 a a b d c f a d g h e f c v a s g a If I pass in COL3, I want it to print the third column, etc. I'm thinking awk might be the easiest thing to use, but cut may also work. I'm just not sure how to go about doing this. 回答1: Awk 1 liner for above problem (if you are interested): awk -v col=COL2 'NR==1{for(i

How do I get gawk to transpose my data into a csv file

倾然丶 夕夏残阳落幕 提交于 2021-01-07 02:58:28
问题 I have a bunch of input text files that look like this: measured 5.7 0.0000 0.0000 0.0125 0.0161 0.0203 0.0230 0.0233 0.0236 0.0241 0.0243 0.0239 0.0235 0.0226 0.0207 0.0184 0.0147 0.0000 0.0000 measured 7.4 0.0000 0.0000 0.0160 0.0207 0.0260 0.0295 0.0298 0.0302 0.0308 0.0311 0.0306 0.0300 0.0289 0.0264 0.0235 0.0187 0.0000 0.0000 Each file has a couple of lines like that. I want to take all of these files, cut out the 'measured' and first number (eg. 5.7 and 7.4) and put them in a CSV file

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

青春壹個敷衍的年華 提交于 2021-01-07 02:39:17
问题 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