All Questions

139
votes
6answers
285521 views

How do I use shell variables in an awk script?

I found some ways to pass external shell variables to an awk script, but I'm confused about ' and ". First, I tried with a shell script: $ v=123test $ echo $v 123test $ echo "$v" 123test Then trie...
17
votes
6answers
10609 views

How to select lines between two patterns?

I have a file like the following and I would like to print the lines between two given patterns PAT1 and PAT2. 1 2 PAT1 3 - first block 4 PAT2 5 6 PAT1 7 - second block PAT2 8 9 PAT1 10 - th...
76
votes
7answers
45953 views

awk save modifications in place

I am learning awk and I would like to know if there is an option to write changes to file, similar to sed where I would use -i option to save modifications to a file. I do understand that I could us...
37
votes
7answers
74463 views

Printing with sed or awk a line following a matching pattern

I'd like to print a single line directly following a line that contains a matching pattern. My version of sed will not take the following syntax (it bombs out on +1p.) which would seem like a simple ...
70
votes
8answers
88434 views

How to select lines between two marker patterns which may occur multiple times with awk/sed

Using awk or sed how can I select lines which are occurring between two different marker patterns? There may be multiple sections marked with these patterns. For example: Suppose the file contains: ...
462
votes
29answers
330654 views

How to do a recursive find/replace of a string with awk of sed?

How do I find and replace every occurrence of: subdomainA.example.com with subdomainB.example.com in every text file under the /home/www/ directory tree (recursive find/replace)?
26
votes
3answers
32919 views

Filter log file entries based on date range

My server is having unusually high CPU usage, and I can see Apache is using way too much memory. I have a feeling, I'm being DOS'd by a single IP - maybe you can help me find him? I've used the follo...
96
votes
5answers
72136 views

Using awk to remove the Byte-order mark

How would an awk script (presumably a one-liner) for removing a BOM look like? Specification: print every line after the first (NR > 1) for the first line: If it starts with #FE #FF or #FF #FE, ...
87
votes
20answers
148488 views

How to merge every two lines into one from the command line?

I have a text file with the following format. The first line is the "KEY" and the second line is the "VALUE". KEY 4048:1736 string 3 KEY 0:1772 string 1 KEY 4192:1349 string 1 KEY 7329:2407 string 2...
93
votes
9answers
26791 views

How to use multiple arguments for awk with a shebang (i.e. #!)?

I'd like to execute an gawk script with --re-interval using a shebang. The "naive" approach of #!/usr/bin/gawk --re-interval -f ... awk script goes here does not work, since gawk is called with th...

Previous Next