awk

Using awk to put a header in a text file

青春壹個敷衍的年華 提交于 2021-02-19 08:48:05
问题 I have lots of text files and need to put a header on each one of them depending of the data on each file. This awk command accomplishes the task: awk 'NR==1{first=$1}{sum+=$1;}END{last=$1;print NR,last,"L";}' my_text.file But this prints it on the screen and I want to put this output in the header of each of my file, and saving the modifications with the same file name. Here is what I've tried: for i in *.txt do echo Processing ${i} cat awk 'NR==1{first=$1}{sum+=$1;}END{last=$1;print NR,last

How to rename duplicate lines with awk?

本小妞迷上赌 提交于 2021-02-19 07:01:52
问题 I have a file with 1 million lines and some lines are duplicate. I would like to rename the duplicate lines by appending "variant" + a number. The file is formatted as follows: I am a test line She is beautiful need for speed Nice day today I am a test line stack overflow is fun I am a test line stack overflow is fun I have more sentences I am a test line She is beautiful Speed for need stack overflow is fun Let's stop here Desired results: I am a test line She is beautiful need for speed

How to rename duplicate lines with awk?

北战南征 提交于 2021-02-19 07:01:51
问题 I have a file with 1 million lines and some lines are duplicate. I would like to rename the duplicate lines by appending "variant" + a number. The file is formatted as follows: I am a test line She is beautiful need for speed Nice day today I am a test line stack overflow is fun I am a test line stack overflow is fun I have more sentences I am a test line She is beautiful Speed for need stack overflow is fun Let's stop here Desired results: I am a test line She is beautiful need for speed

How to get lines from the last match to the end of file?

邮差的信 提交于 2021-02-19 06:39:10
问题 Need to print lines after the last match to the end of file. The number of matches could be anything and not definite. I have some text as shown below. MARKER aaa bbb ccc MARKER ddd eee fff MARKER ggg hhh iii MARKER jjj kkk lll Output desired is jjj kkk lll Do I use awk with RS and FS to get the desired output? 回答1: You can actually do it with awk (gawk) without using any pipe. $ awk -v RS='(^|\n)MARKER\n' 'END{printf "%s", $0}' file jjj kkk lll Explanations: You define your record separator

GREP by result of awk

有些话、适合烂在心里 提交于 2021-02-19 06:22:44
问题 Output of awk '{print $4}' is b05808aa-c6ad-4d30-a334-198ff5726f7c 59996d37-9008-4b3b-ab22-340955cb6019 2b41f358-ff6d-418c-a0d3-ac7151c03b78 7ac4995c-ff2c-4717-a2ac-e6870a5670f0 I need to grep file st.log by these records. Something like awk '{print $4}' |xargs -i grep -w "pattern from awk" st.log I dont know how to pass pattern correctly? 回答1: What about awk '{print $4}' | grep -F -f - st.log Credits to Eric Renouf, who noticed that -f - can be used for standard input instead -f <(cat) ,

GREP by result of awk

China☆狼群 提交于 2021-02-19 06:22:42
问题 Output of awk '{print $4}' is b05808aa-c6ad-4d30-a334-198ff5726f7c 59996d37-9008-4b3b-ab22-340955cb6019 2b41f358-ff6d-418c-a0d3-ac7151c03b78 7ac4995c-ff2c-4717-a2ac-e6870a5670f0 I need to grep file st.log by these records. Something like awk '{print $4}' |xargs -i grep -w "pattern from awk" st.log I dont know how to pass pattern correctly? 回答1: What about awk '{print $4}' | grep -F -f - st.log Credits to Eric Renouf, who noticed that -f - can be used for standard input instead -f <(cat) ,

quickest way to select/copy lines containing string from huge txt.gz file

喜你入骨 提交于 2021-02-19 04:21:49
问题 So I have the following sed one liner: sed -e '/^S|/d' -e '/^T|/d' -e '/^#D=/d' -e '/^##/d' -e 's/H|/,H|/g' -e 's/Q|/,,Q|/g' -e '1 i\,,,' sample_1.txt > sample_2.txt I have many lines that start with either: S| T| #D= ## H| Q| The idea is to not copy the lines starting with one of the first fours and to replace H| (at the beginning of lines) by ,H| and Q| (at the beginning of lines) by ,,Q| But now I would need to: use the fastest way possible (internet suggests (m)awk is faster than sed)

quickest way to select/copy lines containing string from huge txt.gz file

随声附和 提交于 2021-02-19 04:21:25
问题 So I have the following sed one liner: sed -e '/^S|/d' -e '/^T|/d' -e '/^#D=/d' -e '/^##/d' -e 's/H|/,H|/g' -e 's/Q|/,,Q|/g' -e '1 i\,,,' sample_1.txt > sample_2.txt I have many lines that start with either: S| T| #D= ## H| Q| The idea is to not copy the lines starting with one of the first fours and to replace H| (at the beginning of lines) by ,H| and Q| (at the beginning of lines) by ,,Q| But now I would need to: use the fastest way possible (internet suggests (m)awk is faster than sed)

How to print the next word after a found pattern with grep,sed and awk?

北城余情 提交于 2021-02-18 22:26:28
问题 for example, suppose I have logfile.txt which contains "Here is a sample text file" My pattern is "sample" How can I get the word next to sample in my logfile.txt. 回答1: Here is one way to do it with awk: $ awk '{for(i=1;i<=NF;i++)if($i=="sample")print $(i+1)}' file text Explained: $ awk '{ for(i=1;i<=NF;i++) # process every word if($i=="sample") # if word is sample print $(i+1) # print the next }' file and sed: $ sed -n 's/.* sample \([^ ]*\).*/\1/p' file text ie. after sample next space

Parsing HTML on the command line; How to capture text in <strong></strong>?

假如想象 提交于 2021-02-18 06:47:06
问题 I'm trying to grab data from HTML output that looks like this: <strong>Target1NoSpaces</strong><span class="creator"> .... <strong>Target2 With Spaces</strong><span class="creator"> .... I'm using a pipe train to whittle down the data to the targets I'm trying to hit. Here's my approach so far: grep "/strong" output.html | awk '{print $1}' Grep on "/strong" to get the lines with the targets; that works fine. Pipe to 'awk '{print $1}'. That works in case #1 when the target has no spaces, but