awk

Substitute vertical lines in Bash

人走茶凉 提交于 2021-01-27 06:46:06
问题 I'm having a hard time finishing my script since there's this part which doesn't function the way I wanted it to be. I have this line in my script: cat /home/tmp/temp1.txt | awk '{gsub("~",RS);gsub("*",RS);print}' > /home/tmp/temp.txt It works fine, yes. But when I do something like this: cat /home/tmp/temp1.txt | awk '{gsub("|",RS);print}' > /home/tmp/temp.txt It's not working at all. I wanted to change all my vertical bars into new line and yet I can't achieve it. Please help me with this.

Extra space in awk output

眉间皱痕 提交于 2021-01-27 06:26:18
问题 Why do I get the white space before and after the delimiter in the following example? awk -F'^' '{print $1,":",$2}' SERVER_2012-02-29-12-15-00 3969 : 1272 3969 : 1272 3969 : 1272 I expect the results as below without any space: 3969:1272 3969:1272 3969:1272 The text file looks like this... cat SERVER_2012-02-29-12-15-00 3969^1272^14.140.90.242^^IN^como^2012-02-29 3969^1272^14.140.90.242^^IN^como^2012-02-29 3969^1272^14.140.90.242^^IN^como^2012-02-29 回答1: This might work for you: awk -F'^' '

Extra space in awk output

倾然丶 夕夏残阳落幕 提交于 2021-01-27 06:23:07
问题 Why do I get the white space before and after the delimiter in the following example? awk -F'^' '{print $1,":",$2}' SERVER_2012-02-29-12-15-00 3969 : 1272 3969 : 1272 3969 : 1272 I expect the results as below without any space: 3969:1272 3969:1272 3969:1272 The text file looks like this... cat SERVER_2012-02-29-12-15-00 3969^1272^14.140.90.242^^IN^como^2012-02-29 3969^1272^14.140.90.242^^IN^como^2012-02-29 3969^1272^14.140.90.242^^IN^como^2012-02-29 回答1: This might work for you: awk -F'^' '

Can awk print lines that do not have a pattern?

大憨熊 提交于 2021-01-27 02:26:06
问题 Can awk print all lines that did not match one of the patterns? In other words, I want to transform some lines but leave the rest unchanged. So, if a /pattern/ matched I would provide a custom block to print the line. I just need to provide a default matcher (like an else) to print the other lines. 回答1: Yes, just use any non-zero number and awk will do its default thing which is to print the line: awk '7' file If you want it as an "else", put "next" after whatever lines you select for special

Save modifications in place with NON GNU awk

爱⌒轻易说出口 提交于 2021-01-21 14:23:30
问题 I have come across a question(on SO itself) where OP has to do edit and save operation into Input_file(s) itself. I know for a single Input_file we could do following: awk '{print "test here..new line for saving.."}' Input_file > temp && mv temp Input_file Now lets say we need to make changes in same kind of format of files(assume .txt here). What I have tried/thought for this problem: Its approach is going through a for loop of .txt files and calling single awk is a painful and NOT

Save modifications in place with NON GNU awk

南笙酒味 提交于 2021-01-21 14:21:40
问题 I have come across a question(on SO itself) where OP has to do edit and save operation into Input_file(s) itself. I know for a single Input_file we could do following: awk '{print "test here..new line for saving.."}' Input_file > temp && mv temp Input_file Now lets say we need to make changes in same kind of format of files(assume .txt here). What I have tried/thought for this problem: Its approach is going through a for loop of .txt files and calling single awk is a painful and NOT

Is there a field that stores the exact field separator FS used when in a regular expression, equivalent to RT for RS?

江枫思渺然 提交于 2021-01-20 19:58:29
问题 In GNU Awk's 4.1.2 Record Splitting with gawk we can read: When RS is a single character, RT contains the same single character. However, when RS is a regular expression, RT contains the actual input text that matched the regular expression. This variable RT is very useful in some cases. Similarly, we can set a regular expression as the field separator. For example, in here we allow it to be either ";" or "|": $ gawk -F';' '{print NF}' <<< "hello;how|are you" 2 # there are 2 fields, since ";"

Is there a field that stores the exact field separator FS used when in a regular expression, equivalent to RT for RS?

荒凉一梦 提交于 2021-01-20 19:55:47
问题 In GNU Awk's 4.1.2 Record Splitting with gawk we can read: When RS is a single character, RT contains the same single character. However, when RS is a regular expression, RT contains the actual input text that matched the regular expression. This variable RT is very useful in some cases. Similarly, we can set a regular expression as the field separator. For example, in here we allow it to be either ";" or "|": $ gawk -F';' '{print NF}' <<< "hello;how|are you" 2 # there are 2 fields, since ";"

Is there a field that stores the exact field separator FS used when in a regular expression, equivalent to RT for RS?

依然范特西╮ 提交于 2021-01-20 19:54:50
问题 In GNU Awk's 4.1.2 Record Splitting with gawk we can read: When RS is a single character, RT contains the same single character. However, when RS is a regular expression, RT contains the actual input text that matched the regular expression. This variable RT is very useful in some cases. Similarly, we can set a regular expression as the field separator. For example, in here we allow it to be either ";" or "|": $ gawk -F';' '{print NF}' <<< "hello;how|are you" 2 # there are 2 fields, since ";"

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

偶尔善良 提交于 2021-01-20 07:28:45
问题 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